Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Update tests
  • Loading branch information
ilevkivskyi committed Jun 9, 2024
commit 95c6421b64047b23d4e30cd711464cc5c530c868
2 changes: 2 additions & 0 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,8 @@ def unify_generic_callable(
import mypy.solve

if set(type.type_var_ids()) & {v.id for v in mypy.typeops.get_all_type_vars(target)}:
# Overload overlap check does nasty things like unifying in opposite direction.
# This can easily create type variable clashes, so we need to refresh.
type = freshen_function_type_vars(type)

if return_constraint_direction is None:
Expand Down
32 changes: 16 additions & 16 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -2927,8 +2927,8 @@ def mix(fs: List[Callable[[S], T]]) -> Callable[[S], List[T]]:
def id(__x: U) -> U:
...
fs = [id, id, id]
reveal_type(mix(fs)) # N: Revealed type is "def [S] (S`7) -> builtins.list[S`7]"
reveal_type(mix([id, id, id])) # N: Revealed type is "def [S] (S`9) -> builtins.list[S`9]"
reveal_type(mix(fs)) # N: Revealed type is "def [S] (S`11) -> builtins.list[S`11]"
reveal_type(mix([id, id, id])) # N: Revealed type is "def [S] (S`13) -> builtins.list[S`13]"
[builtins fixtures/list.pyi]

[case testInferenceAgainstGenericCurry]
Expand Down Expand Up @@ -3027,7 +3027,7 @@ def dec(f: Callable[[T], S], g: Callable[[T], U]) -> Callable[[T], Tuple[S, U]]:
def id(x: V) -> V:
...

reveal_type(dec(id, id)) # N: Revealed type is "def [T] (T`1) -> Tuple[T`1, T`1]"
reveal_type(dec(id, id)) # N: Revealed type is "def [T] (T`7) -> Tuple[T`7, T`7]"
[builtins fixtures/tuple.pyi]

[case testInferenceAgainstGenericEllipsisSelfSpecialCase]
Expand Down Expand Up @@ -3099,13 +3099,13 @@ def dec4_bound(f: Callable[[I], List[T]]) -> Callable[[I], T]:
...

reveal_type(dec1(lambda x: x)) # N: Revealed type is "def [T] (T`3) -> builtins.list[T`3]"
reveal_type(dec2(lambda x: x)) # N: Revealed type is "def [S] (S`4) -> builtins.list[S`4]"
reveal_type(dec3(lambda x: x[0])) # N: Revealed type is "def [S] (S`6) -> S`6"
reveal_type(dec4(lambda x: [x])) # N: Revealed type is "def [S] (S`9) -> S`9"
reveal_type(dec2(lambda x: x)) # N: Revealed type is "def [S] (S`5) -> builtins.list[S`5]"
reveal_type(dec3(lambda x: x[0])) # N: Revealed type is "def [S] (S`8) -> S`8"
reveal_type(dec4(lambda x: [x])) # N: Revealed type is "def [S] (S`12) -> S`12"
reveal_type(dec1(lambda x: 1)) # N: Revealed type is "def (builtins.int) -> builtins.list[builtins.int]"
reveal_type(dec5(lambda x: x)) # N: Revealed type is "def (builtins.int) -> builtins.list[builtins.int]"
reveal_type(dec3(lambda x: x)) # N: Revealed type is "def [S] (S`16) -> builtins.list[S`16]"
reveal_type(dec4(lambda x: x)) # N: Revealed type is "def [T] (builtins.list[T`19]) -> T`19"
reveal_type(dec3(lambda x: x)) # N: Revealed type is "def [S] (S`20) -> builtins.list[S`20]"
reveal_type(dec4(lambda x: x)) # N: Revealed type is "def [T] (builtins.list[T`24]) -> T`24"
dec4_bound(lambda x: x) # E: Value of type variable "I" of "dec4_bound" cannot be "List[T]"
[builtins fixtures/list.pyi]

Expand Down Expand Up @@ -3185,7 +3185,7 @@ reveal_type(dec(id)) # N: Revealed type is "def [T] (T`3) -> T`3"
reveal_type(dec(either)) # N: Revealed type is "def [T] (T`6, x: T`6) -> T`6"
reveal_type(dec(pair)) # N: Revealed type is "def [T, U] (T`9, x: U`-1) -> Tuple[T`9, U`-1]"
# This is counter-intuitive but looks correct, dec matches itself only if P can be empty
reveal_type(dec(dec)) # N: Revealed type is "def [T, S] (T`12, f: def () -> def (T`12) -> S`13) -> S`13"
reveal_type(dec(dec)) # N: Revealed type is "def [T, S] (T`13, f: def () -> def (T`13) -> S`14) -> S`14"
[builtins fixtures/list.pyi]

[case testInferenceAgainstGenericParamSpecVsParamSpec]
Expand Down Expand Up @@ -3263,8 +3263,8 @@ def transform(

def dec(f: Callable[W, U]) -> Callable[W, U]: ...
def dec2(f: Callable[Concatenate[str, W], U]) -> Callable[Concatenate[bytes, W], U]: ...
reveal_type(transform(dec)) # N: Revealed type is "def [P, T] (def (builtins.int, *P.args, **P.kwargs) -> T`3) -> def (builtins.int, *P.args, **P.kwargs) -> T`3"
reveal_type(transform(dec2)) # N: Revealed type is "def [W, T] (def (builtins.int, builtins.str, *W.args, **W.kwargs) -> T`7) -> def (builtins.int, builtins.bytes, *W.args, **W.kwargs) -> T`7"
reveal_type(transform(dec)) # N: Revealed type is "def [P, T] (def (builtins.int, *P.args, **P.kwargs) -> T`9) -> def (builtins.int, *P.args, **P.kwargs) -> T`9"
reveal_type(transform(dec2)) # N: Revealed type is "def [W, T] (def (builtins.int, builtins.str, *W.args, **W.kwargs) -> T`13) -> def (builtins.int, builtins.bytes, *W.args, **W.kwargs) -> T`13"
[builtins fixtures/tuple.pyi]

[case testNoAccidentalVariableClashInNestedGeneric]
Expand Down Expand Up @@ -3318,8 +3318,8 @@ def id(x: U) -> U: ...
def either(x: U, y: U) -> U: ...
def pair(x: U, y: V) -> Tuple[U, V]: ...

reveal_type(dec(id)) # N: Revealed type is "def [T] (T`3) -> builtins.list[T`3]"
reveal_type(dec(either)) # N: Revealed type is "def [T] (T`5, T`5) -> builtins.list[T`5]"
reveal_type(dec(id)) # N: Revealed type is "def [T] (T`9) -> builtins.list[T`9]"
reveal_type(dec(either)) # N: Revealed type is "def [T] (T`11, T`11) -> builtins.list[T`11]"
reveal_type(dec(pair)) # N: Revealed type is "def [U, V] (U`-1, V`-2) -> builtins.list[Tuple[U`-1, V`-2]]"
[builtins fixtures/tuple.pyi]

Expand All @@ -3337,8 +3337,8 @@ V = TypeVar("V")
def id(x: U) -> U: ...
def either(x: U, y: U) -> U: ...

reveal_type(dec(id)) # N: Revealed type is "def [T] (builtins.list[T`3]) -> T`3"
reveal_type(dec(either)) # N: Revealed type is "def [T] (builtins.list[T`5], builtins.list[T`5]) -> T`5"
reveal_type(dec(id)) # N: Revealed type is "def [T] (builtins.list[T`9]) -> T`9"
reveal_type(dec(either)) # N: Revealed type is "def [T] (builtins.list[T`11], builtins.list[T`11]) -> T`11"
[builtins fixtures/tuple.pyi]

[case testInferenceAgainstGenericVariadicPopOff]
Expand Down Expand Up @@ -3383,7 +3383,7 @@ reveal_type(dec(id)) # N: Revealed type is "def [T] (T`3) -> T`3"
reveal_type(dec(either)) # N: Revealed type is "def [T] (T`6, T`6) -> T`6"
reveal_type(dec(pair)) # N: Revealed type is "def [T, U] (T`9, U`-1) -> Tuple[T`9, U`-1]"
# This is counter-intuitive but looks correct, dec matches itself only if Ts is empty
reveal_type(dec(dec)) # N: Revealed type is "def [T, S] (T`12, def () -> def (T`12) -> S`13) -> S`13"
reveal_type(dec(dec)) # N: Revealed type is "def [T, S] (T`13, def () -> def (T`13) -> S`14) -> S`14"
[builtins fixtures/list.pyi]

[case testInferenceAgainstGenericVariadicVsVariadic]
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 @@ -2869,7 +2869,7 @@ class Wrapper(Generic[T]):
# Related to testTypeCheckOverloadImplementationTypeVarDifferingUsage2?
# See https://github.com/python/mypy/issues/5510
@overload
def g(self, x: int) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
def g(self, x: int) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
@overload
def g(self, x: T) -> T: ...
def g(self, x): ...
Expand All @@ -2895,13 +2895,13 @@ class Wrapper(Generic[T]):
# TODO: This shouldn't trigger an error message?
# See https://github.com/python/mypy/issues/5510
@overload
def g1(self, x: List[int]) -> int: ... # E: # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
def g1(self, x: List[int]) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
@overload
def g1(self, x: List[T]) -> T: ...
def g1(self, x): ...

@overload
def g2(self, x: List[int]) -> List[int]: ... # E: # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
def g2(self, x: List[int]) -> List[int]: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
@overload
def g2(self, x: List[T]) -> List[T]: ...
def g2(self, x): ...
Expand Down
18 changes: 9 additions & 9 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -901,8 +901,8 @@ class A:
def func(self, action: Callable[_P, _R], *args: _P.args, **kwargs: _P.kwargs) -> _R:
...

reveal_type(A.func) # N: Revealed type is "def [_P, _R] (self: __main__.A, action: def (*_P.args, **_P.kwargs) -> _R`4, *_P.args, **_P.kwargs) -> _R`4"
reveal_type(A().func) # N: Revealed type is "def [_P, _R] (action: def (*_P.args, **_P.kwargs) -> _R`8, *_P.args, **_P.kwargs) -> _R`8"
reveal_type(A.func) # N: Revealed type is "def [_P, _R] (self: __main__.A, action: def (*_P.args, **_P.kwargs) -> _R`15, *_P.args, **_P.kwargs) -> _R`15"
reveal_type(A().func) # N: Revealed type is "def [_P, _R] (action: def (*_P.args, **_P.kwargs) -> _R`19, *_P.args, **_P.kwargs) -> _R`19"

def f(x: int) -> int:
...
Expand Down Expand Up @@ -933,8 +933,8 @@ class A:
def func(self, action: Job[_P, None]) -> Job[_P, None]:
...

reveal_type(A.func) # N: Revealed type is "def [_P] (self: __main__.A, action: __main__.Job[_P`3, None]) -> __main__.Job[_P`3, None]"
reveal_type(A().func) # N: Revealed type is "def [_P] (action: __main__.Job[_P`5, None]) -> __main__.Job[_P`5, None]"
reveal_type(A.func) # N: Revealed type is "def [_P] (self: __main__.A, action: __main__.Job[_P`13, None]) -> __main__.Job[_P`13, None]"
reveal_type(A().func) # N: Revealed type is "def [_P] (action: __main__.Job[_P`15, None]) -> __main__.Job[_P`15, None]"
reveal_type(A().func(Job(lambda x: x))) # N: Revealed type is "__main__.Job[[x: Any], None]"

def f(x: int, y: int) -> None: ...
Expand Down Expand Up @@ -1096,7 +1096,7 @@ j = Job(generic_f)
reveal_type(j) # N: Revealed type is "__main__.Job[[x: _T`-1]]"

jf = j.into_callable()
reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`3)"
reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`13)"
reveal_type(jf(1)) # N: Revealed type is "None"
[builtins fixtures/paramspec.pyi]

Expand All @@ -1115,10 +1115,10 @@ class Job(Generic[_P, _T]):
def generic_f(x: _T) -> _T: ...

j = Job(generic_f)
reveal_type(j) # N: Revealed type is "__main__.Job[[x: _T`3], _T`3]"
reveal_type(j) # N: Revealed type is "__main__.Job[[x: _T`12], _T`12]"

jf = j.into_callable()
reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`4) -> _T`4"
reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`13) -> _T`13"
reveal_type(jf(1)) # N: Revealed type is "builtins.int"
[builtins fixtures/paramspec.pyi]

Expand Down Expand Up @@ -1640,13 +1640,13 @@ U = TypeVar("U")
def dec(f: Callable[P, T]) -> Callable[P, List[T]]: ...
def test(x: U) -> U: ...
reveal_type(dec) # N: Revealed type is "def [P, T] (f: def (*P.args, **P.kwargs) -> T`-2) -> def (*P.args, **P.kwargs) -> builtins.list[T`-2]"
reveal_type(dec(test)) # N: Revealed type is "def [T] (x: T`3) -> builtins.list[T`3]"
reveal_type(dec(test)) # N: Revealed type is "def [T] (x: T`12) -> builtins.list[T`12]"

class A: ...
TA = TypeVar("TA", bound=A)

def test_with_bound(x: TA) -> TA: ...
reveal_type(dec(test_with_bound)) # N: Revealed type is "def [T <: __main__.A] (x: T`5) -> builtins.list[T`5]"
reveal_type(dec(test_with_bound)) # N: Revealed type is "def [T <: __main__.A] (x: T`14) -> builtins.list[T`14]"
dec(test_with_bound)(0) # E: Value of type variable "T" of function cannot be "int"
dec(test_with_bound)(A()) # OK
[builtins fixtures/paramspec.pyi]
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -1793,7 +1793,7 @@ class C:
def bar(self) -> Self: ...
def foo(self, x: S) -> Tuple[Self, S]: ...

reveal_type(C.foo) # N: Revealed type is "def [Self <: __main__.C, S] (self: Self`1, x: S`2) -> Tuple[Self`1, S`2]"
reveal_type(C.foo) # N: Revealed type is "def [Self <: __main__.C, S] (self: Self`7, x: S`8) -> Tuple[Self`7, S`8]"
reveal_type(C().foo(42)) # N: Revealed type is "Tuple[__main__.C, builtins.int]"
[builtins fixtures/tuple.pyi]

Expand All @@ -1807,7 +1807,7 @@ class C:
def bar(self) -> Self: ...
foo: Callable[[S, Self], Tuple[Self, S]]

reveal_type(C().foo) # N: Revealed type is "def [S] (S`1, __main__.C) -> Tuple[__main__.C, S`1]"
reveal_type(C().foo) # N: Revealed type is "def [S] (S`7, __main__.C) -> Tuple[__main__.C, S`7]"
reveal_type(C().foo(42, C())) # N: Revealed type is "Tuple[__main__.C, builtins.int]"
class This: ...
[builtins fixtures/tuple.pyi]
Expand Down Expand Up @@ -2032,7 +2032,7 @@ class Ben(Object):
}
@classmethod
def doit(cls) -> Foo:
reveal_type(cls.MY_MAP) # N: Revealed type is "builtins.dict[builtins.str, def [Self <: __main__.Foo] (self: Self`4) -> Self`4]"
reveal_type(cls.MY_MAP) # N: Revealed type is "builtins.dict[builtins.str, def [Self <: __main__.Foo] (self: Self`10) -> Self`10]"
foo_method = cls.MY_MAP["foo"]
return foo_method(Foo())
[builtins fixtures/isinstancelist.pyi]
Expand Down