Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 1 addition & 37 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,7 @@
remove_dups,
split_with_prefix_and_suffix,
)
from mypy.types_utils import (
is_generic_instance,
is_overlapping_none,
is_self_type_like,
remove_optional,
)
from mypy.types_utils import is_overlapping_none, is_self_type_like, remove_optional
from mypy.typestate import type_state
from mypy.typevars import fill_typevars
from mypy.visitor import ExpressionVisitor
Expand Down Expand Up @@ -2186,37 +2181,6 @@ def infer_function_type_arguments_using_context(
# variables in an expression are inferred at the same time.
# (And this is hard, also we need to be careful with lambdas that require
# two passes.)
proper_ret = get_proper_type(ret_type)
if (
isinstance(proper_ret, TypeVarType)
or isinstance(proper_ret, UnionType)
and all(isinstance(get_proper_type(u), TypeVarType) for u in proper_ret.items)
):
# Another special case: the return type is a type variable. If it's unrestricted,
# we could infer a too general type for the type variable if we use context,
# and this could result in confusing and spurious type errors elsewhere.
#
# So we give up and just use function arguments for type inference, with just two
# exceptions:
#
# 1. If the context is a generic instance type, actually use it as context, as
# this *seems* to usually be the reasonable thing to do.
#
# See also github issues #462 and #360.
#
# 2. If the context is some literal type, we want to "propagate" that information
# down so that we infer a more precise type for literal expressions. For example,
# the expression `3` normally has an inferred type of `builtins.int`: but if it's
# in a literal context like below, we want it to infer `Literal[3]` instead.
#
# def expects_literal(x: Literal[3]) -> None: pass
# def identity(x: T) -> T: return x
#
# expects_literal(identity(3)) # Should type-check
# TODO: we may want to add similar exception if all arguments are lambdas, since
# in this case external context is almost everything we have.
if not is_generic_instance(ctx) and not is_literal_type_like(ctx):
return callable.copy_modified(), []
args, constraints = infer_type_arguments(
callable.variables, ret_type, erased_ctx, skip_unsatisfied=True
)
Expand Down
5 changes: 0 additions & 5 deletions mypy/types_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ def is_union_with_any(tp: Type) -> bool:
return any(is_union_with_any(t) for t in get_proper_types(tp.items))


def is_generic_instance(tp: Type) -> bool:
tp = get_proper_type(tp)
return isinstance(tp, Instance) and bool(tp.args)


def is_overlapping_none(t: Type) -> bool:
t = get_proper_type(t)
return isinstance(t, NoneType) or (
Expand Down
20 changes: 5 additions & 15 deletions mypyc/test-data/irbuild-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2617,7 +2617,6 @@ def NotTransformed.__new__(cls, val):
r10 :: object[1]
r11 :: object_ptr
r12 :: object
r13 :: str
L0:
r0 = builtins :: module
r1 = 'super'
Expand All @@ -2634,8 +2633,7 @@ L0:
r11 = load_address r10
r12 = PyObject_Vectorcall(r8, r11, 1, 0)
keep_alive r9
r13 = cast(str, r12)
return r13
return r12
def NotTransformed.factory(cls, val):
cls :: object
val :: int
Expand Down Expand Up @@ -2782,7 +2780,6 @@ def NotTransformed.__new__(cls, val):
r5 :: object[2]
r6 :: object_ptr
r7 :: object
r8 :: str
L0:
r0 = builtins :: module
r1 = 'object'
Expand All @@ -2793,8 +2790,7 @@ L0:
r6 = load_address r5
r7 = PyObject_VectorcallMethod(r4, r6, 9223372036854775810, 0)
keep_alive r2, r3
r8 = cast(str, r7)
return r8
return r7
def NotTransformed.factory(cls, val):
cls :: object
val :: int
Expand Down Expand Up @@ -3000,11 +2996,9 @@ def A.__new__(cls, x):
cls :: object
x :: int
r0 :: object
r1 :: __main__.A
L0:
r0 = CPy_SetupObject(cls)
r1 = cast(__main__.A, r0)
return r1
return r0
def A.__init__(self, x):
self :: __main__.A
x :: int
Expand Down Expand Up @@ -3096,20 +3090,16 @@ def StaticBase.__new__(cls, x):
cls :: object
x :: int
r0 :: object
r1 :: __main__.StaticBase
L0:
r0 = CPy_SetupObject(cls)
r1 = cast(__main__.StaticBase, r0)
return r1
return r0
def DecoratedBase.__new__(cls, x):
cls :: object
x :: int
r0 :: object
r1 :: __main__.DecoratedBase
L0:
r0 = CPy_SetupObject(cls)
r1 = cast(__main__.DecoratedBase, r0)
return r1
return r0
def f():
r0 :: __main__.A
r1 :: object
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-bound.test
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ b = B()
if int():
b = f(b)
if int():
b = f(C()) # E: Incompatible types in assignment (expression has type "C", variable has type "B")
b = f(C()) # E: Argument 1 to "f" has incompatible type "C"; expected "B"


[case testBoundOnGenericClass]
Expand Down
3 changes: 1 addition & 2 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -3718,8 +3718,7 @@ def error(u_c: Type[U]) -> P: # Error here, see below
main:11: note: Revealed type is "__main__.WizUser"
main:12: error: A function returning TypeVar should receive at least one argument containing the same TypeVar
main:12: note: Consider using the upper bound "ProUser" instead
main:13: error: Value of type variable "P" of "new_pro" cannot be "U"
main:13: error: Incompatible return value type (got "U", expected "P")
main:13: error: Argument 1 to "new_pro" has incompatible type "type[U]"; expected "type[P]"

[case testTypeUsingTypeCCovariance]
from typing import Type, TypeVar
Expand Down
14 changes: 7 additions & 7 deletions test-data/unit/check-dataclasses.test
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ from dataclasses import dataclass, field
@dataclass
class Application:
name: str = 'Unnamed'
rating: int = field(default_factory=str) # E: Incompatible types in assignment (expression has type "str", variable has type "int")
rating: int = field(default_factory=str) # E: Argument "default_factory" to "field" has incompatible type "type[str]"; expected "Callable[[], int]"

[builtins fixtures/dataclasses.pyi]

Expand Down Expand Up @@ -2285,7 +2285,7 @@ TNone = TypeVar('TNone', bound=None)
TUnion = TypeVar('TUnion', bound=Union[str, int])

def f1(t: TInt) -> None:
_ = replace(t, x=42) # E: Value of type variable "_DataclassT" of "replace" cannot be "TInt"
replace(t, x=42) # E: Value of type variable "_DataclassT" of "replace" cannot be "TInt"

def f2(t: TAny) -> TAny:
return replace(t, x='spam') # E: Value of type variable "_DataclassT" of "replace" cannot be "TAny"
Expand Down Expand Up @@ -2351,11 +2351,11 @@ replace(None) # E: Value of type variable "_DataclassT" of "replace" cannot be
from dataclasses import is_dataclass, replace

def f(x: object) -> None:
_ = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "object"
if is_dataclass(x):
_ = replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "DataclassInstance | type[DataclassInstance]"
if not isinstance(x, type):
_ = replace(x)
replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "object"
if is_dataclass(x):
replace(x) # E: Value of type variable "_DataclassT" of "replace" cannot be "DataclassInstance | type[DataclassInstance]"
if not isinstance(x, type):
replace(x)

[builtins fixtures/tuple.pyi]

Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -1471,8 +1471,8 @@ a: A
b: B

if int():
b = f([a]) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a = f([b]) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
b = f([a]) # E: List item 0 has incompatible type "A"; expected "B"
a = f([b]) # E: List item 0 has incompatible type "B"; expected "A"
if int():
a = f(b) # E: Incompatible types in assignment (expression has type "B", variable has type "A")

Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-inference-context.test
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ class M(Generic[_KT, _VT]):
def get(self, k: _KT, default: _T) -> _T: ...

def f(d: M[_KT, _VT], k: _KT) -> _VT:
return d.get(k, None) # E: Incompatible return value type (got "None", expected "_VT")
return d.get(k, None) # E: Argument 2 to "get" of "M" has incompatible type "None"; expected "_VT"

[case testGenericMethodCalledInGenericContext2]
from typing import TypeVar, Generic, Union
Expand Down
17 changes: 8 additions & 9 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -550,10 +550,10 @@ c: Tuple[A, object]
def id(a: T) -> T: pass

if int():
b = id(a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
a = id(b) # E: Incompatible types in assignment (expression has type "B", variable has type "A")
b = id(a) # E: Argument 1 to "id" has incompatible type "A"; expected "B"
a = id(b) # E: Argument 1 to "id" has incompatible type "B"; expected "A"
if int():
a = id(c) # E: Incompatible types in assignment (expression has type "tuple[A, object]", variable has type "A")
a = id(c) # E: Argument 1 to "id" has incompatible type "tuple[A, object]"; expected "A"

if int():
a = id(a)
Expand All @@ -572,12 +572,11 @@ def f() -> None:
b: int
c: str
if int():
b = a(c) # E: Incompatible types in assignment (expression has type "str", variable has type "int")
b = a(c) # E: Argument 1 has incompatible type "str"; expected "int"
b = a(b)
c = a(c)
def id(x: T) -> T:
return x
[out]

[case testUnderspecifiedInferenceResult]
# flags: --no-strict-optional
Expand Down Expand Up @@ -611,9 +610,9 @@ b: B
def f(a: T, b: T) -> T: pass

if int():
b = f(a, b) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
b = f(a, b) # E: Argument 1 to "f" has incompatible type "A"; expected "B"
if int():
b = f(b, a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
b = f(b, a) # E: Argument 2 to "f" has incompatible type "A"; expected "B"
if int():
a = f(a, b)
if int():
Expand Down Expand Up @@ -711,9 +710,9 @@ def g(a: T, b: T = None) -> T: pass
class A: pass

if int():
a = f(o) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
a = f(o) # E: Argument 1 to "f" has incompatible type "object"; expected "A"
if int():
a = g(a, o) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
a = g(a, o) # E: Argument 2 to "g" has incompatible type "object"; expected "A"

if int():
o = f()
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2989,7 +2989,7 @@ def test_generic(x: Union[A, None]) -> None:
reveal_type(r2) # N: Revealed type is "def ()"


[case testLambdaTernaryIndirectAttribute]
[case testLambdaTernaryIndirectAttribute-xfail]
# fails due to binder issue inside `check_func_def`
# https://github.com/python/mypy/issues/19561
from typing import TypeVar, Union, Callable, reveal_type
Expand Down Expand Up @@ -3020,7 +3020,7 @@ def test_generic_with_attr(x: B) -> None:
reveal_type(l2) # N: Revealed type is "def () -> __main__.A | None"
reveal_type(r2) # N: Revealed type is "def () -> __main__.A | None"

[case testLambdaTernaryDoubleIndirectAttribute]
[case testLambdaTernaryDoubleIndirectAttribute-xfail]
# fails due to binder issue inside `check_func_def`
# https://github.com/python/mypy/issues/19561
from typing import TypeVar, Union, Callable, reveal_type
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ c: C
if int():
b = f(ab)
c = f(ac)
b = f(ac) # E: Incompatible types in assignment (expression has type "C", variable has type "B")
b = f(ac) # E: Argument 1 to "f" has incompatible type "A[C]"; expected "A[B]"
b = f(b)
c = f(b) # E: Incompatible types in assignment (expression has type "B", variable has type "C")
@overload
Expand Down Expand Up @@ -3508,8 +3508,8 @@ def wrapper() -> None:
reveal_type(foo(obj2)) # N: Revealed type is "__main__.A | __main__.B"
bar(obj2) # E: Cannot infer value of type parameter "T" of "bar"

b1_overload: A = foo(obj2) # E: Incompatible types in assignment (expression has type "A | B", variable has type "A")
b1_union: A = bar(obj2) # E: Cannot infer value of type parameter "T" of "bar"
b1_overload: A = foo(obj2) # E: Argument 1 to "foo" has incompatible type "W1[A] | W2[B]"; expected "W1[A]"
b1_union: A = bar(obj2) # E: Argument 1 to "bar" has incompatible type "W1[A] | W2[B]"; expected "W1[A] | W2[A]"

[case testOverloadingInferUnionReturnWithObjectTypevarReturn]
from typing import overload, Union, TypeVar, Generic
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-plugin-attrs.test
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ class A:
x: int = attr.ib(factory=int, default=7) # E: Can't pass both "default" and "factory".
[builtins fixtures/bool.pyi]

[case testAttrsFactoryBadReturn]
[case testAttrsFactoryBadReturn-xfail]
import attr
def my_factory() -> int:
return 7
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ f(B.copy(B()))
# f(B.copy(A()))

def g(a: B) -> None: pass
g(A.copy(A())) # E: Argument 1 to "g" has incompatible type "A"; expected "B"
g(A.copy(A())) # E: Argument 1 to "copy" of "A" has incompatible type "A"; expected "B"
g(A.copy(B()))
g(B.copy(B()))

Expand Down
7 changes: 4 additions & 3 deletions test-data/unit/check-typevar-values.test
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,8 @@ def g(x: T) -> T: pass
c1(g(''))
c2(g(1))
c3(g(''))
c2(g('')) # E: Argument 1 to "c2" has incompatible type "str"; expected "int"
c3(g(1)) # E: Argument 1 to "c3" has incompatible type "int"; expected "str"
c2(g('')) # E: Argument 1 to "g" has incompatible type "str"; expected "int"
c3(g(1)) # E: Argument 1 to "g" has incompatible type "int"; expected "str"

[case testTypeVarWithValueInferredFromObjectReturnTypeContext2]
from typing import TypeVar
Expand All @@ -472,7 +472,8 @@ c(g(''))
c(g(1))
[out]
main:6: error: Argument 1 to "c" has incompatible type "str"; expected "ss"
main:7: error: Argument 1 to "c" has incompatible type "int"; expected "ss"
main:7: error: Argument 1 to "c" has incompatible type "str"; expected "ss"
main:7: error: Argument 1 to "g" has incompatible type "int"; expected "str"

[case testDefineAttributeInGenericMethodUsingTypeVarWithValues]
from typing import TypeVar
Expand Down
16 changes: 8 additions & 8 deletions test-data/unit/check-varargs.test
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ bar(args=1) # E: Argument "args" to "bar" has incompatible type "int"; expected
from typing import TypeVar
T = TypeVar('T')

def f( *a: T) -> T:
def f(*a: T) -> T:
pass

class A: pass
Expand All @@ -177,11 +177,11 @@ c: C
o: object

if int():
a = f(o) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
a = f(o) # E: Argument 1 to "f" has incompatible type "object"; expected "A"
if int():
b = f(b, a) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
b = f(b, a) # E: Argument 2 to "f" has incompatible type "A"; expected "B"
if int():
b = f(a, b) # E: Incompatible types in assignment (expression has type "A", variable has type "B")
b = f(a, b) # E: Argument 1 to "f" has incompatible type "A"; expected "B"

if int():
o = f()
Expand Down Expand Up @@ -210,13 +210,13 @@ def f(a: T, b: T = None, *c: T) -> T:
class A: pass

if int():
a = f(o) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
a = f(o) # E: Argument 1 to "f" has incompatible type "object"; expected "A"
if int():
a = f(a, o) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
a = f(a, o) # E: Argument 2 to "f" has incompatible type "object"; expected "A"
if int():
a = f(a, a, o) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
a = f(a, a, o) # E: Argument 3 to "f" has incompatible type "object"; expected "A"
if int():
a = f(a, a, a, o) # E: Incompatible types in assignment (expression has type "object", variable has type "A")
a = f(a, a, a, o) # E: Argument 4 to "f" has incompatible type "object"; expected "A"

if int():
a = f(a)
Expand Down
Loading