Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support TypeAliasType #16926

Merged
merged 18 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update tests
  • Loading branch information
hamdanal committed Feb 17, 2024
commit 9e19087fbd5d3c27a9657a9f65b508d187b14239
54 changes: 27 additions & 27 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -3088,8 +3088,8 @@ def dec(f: Callable[P, T]) -> Callable[P, List[T]]: ...
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] (x: T`2) -> builtins.list[T`2]"
reveal_type(dec(either)) # N: Revealed type is "def [T] (x: T`4, y: T`4) -> builtins.list[T`4]"
reveal_type(dec(id)) # N: Revealed type is "def [T] (x: T`3) -> builtins.list[T`3]"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why this changed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea to be honest. They changed in the original PR by sobolevn and I didn't know why. I'll do some more digging.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Not too important if it's hard to figure out, I assume these numbers can change based on all kinds of factors.

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

Expand All @@ -3106,8 +3106,8 @@ V = TypeVar('V')
def dec(f: Callable[P, List[T]]) -> Callable[P, T]: ...
def id(x: U) -> U: ...
def either(x: U, y: U) -> U: ...
reveal_type(dec(id)) # N: Revealed type is "def [T] (x: builtins.list[T`2]) -> T`2"
reveal_type(dec(either)) # N: Revealed type is "def [T] (x: builtins.list[T`4], y: builtins.list[T`4]) -> T`4"
reveal_type(dec(id)) # N: Revealed type is "def [T] (x: builtins.list[T`3]) -> T`3"
reveal_type(dec(either)) # N: Revealed type is "def [T] (x: builtins.list[T`5], y: builtins.list[T`5]) -> T`5"
[builtins fixtures/list.pyi]

[case testInferenceAgainstGenericParamSpecPopOff]
Expand All @@ -3125,9 +3125,9 @@ def dec(f: Callable[Concatenate[T, P], S]) -> Callable[P, Callable[[T], S]]: ...
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 () -> def [T] (T`1) -> T`1"
reveal_type(dec(either)) # N: Revealed type is "def [T] (y: T`4) -> def (T`4) -> T`4"
reveal_type(dec(pair)) # N: Revealed type is "def [V] (y: V`-2) -> def [T] (T`7) -> Tuple[T`7, V`-2]"
reveal_type(dec(id)) # N: Revealed type is "def () -> def [T] (T`2) -> T`2"
reveal_type(dec(either)) # N: Revealed type is "def [T] (y: T`5) -> def (T`5) -> T`5"
reveal_type(dec(pair)) # N: Revealed type is "def [V] (y: V`-2) -> def [T] (T`8) -> Tuple[T`8, V`-2]"
reveal_type(dec(dec)) # N: Revealed type is "def () -> def [T, P, S] (def (T`-1, *P.args, **P.kwargs) -> S`-3) -> def (*P.args, **P.kwargs) -> def (T`-1) -> S`-3"
[builtins fixtures/list.pyi]

Expand All @@ -3146,11 +3146,11 @@ def dec(f: Callable[P, Callable[[T], S]]) -> Callable[Concatenate[T, P], S]: ...
def id() -> Callable[[U], U]: ...
def either(x: U) -> Callable[[U], U]: ...
def pair(x: U) -> Callable[[V], Tuple[V, U]]: ...
reveal_type(dec(id)) # N: Revealed type is "def [T] (T`2) -> T`2"
reveal_type(dec(either)) # N: Revealed type is "def [T] (T`5, x: T`5) -> T`5"
reveal_type(dec(pair)) # N: Revealed type is "def [T, U] (T`8, x: U`-1) -> Tuple[T`8, U`-1]"
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`11, f: def () -> def (T`11) -> S`12) -> S`12"
reveal_type(dec(dec)) # N: Revealed type is "def [T, S] (T`12, f: def () -> def (T`12) -> S`13) -> S`13"
[builtins fixtures/list.pyi]

[case testInferenceAgainstGenericParamSpecVsParamSpec]
Expand All @@ -3167,7 +3167,7 @@ class Bar(Generic[P, T]): ...

def dec(f: Callable[P, T]) -> Callable[P, List[T]]: ...
def f(*args: Q.args, **kwargs: Q.kwargs) -> Foo[Q]: ...
reveal_type(dec(f)) # N: Revealed type is "def [P] (*P.args, **P.kwargs) -> builtins.list[__main__.Foo[P`1]]"
reveal_type(dec(f)) # N: Revealed type is "def [P] (*P.args, **P.kwargs) -> builtins.list[__main__.Foo[P`2]]"
g: Callable[Concatenate[int, Q], Foo[Q]]
reveal_type(dec(g)) # N: Revealed type is "def [Q] (builtins.int, *Q.args, **Q.kwargs) -> builtins.list[__main__.Foo[Q`-1]]"
h: Callable[Concatenate[T, Q], Bar[Q, T]]
Expand Down Expand Up @@ -3228,8 +3228,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`2) -> def (builtins.int, *P.args, **P.kwargs) -> T`2"
reveal_type(transform(dec2)) # N: Revealed type is "def [W, T] (def (builtins.int, builtins.str, *W.args, **W.kwargs) -> T`6) -> def (builtins.int, builtins.bytes, *W.args, **W.kwargs) -> T`6"
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"
[builtins fixtures/tuple.pyi]

[case testNoAccidentalVariableClashInNestedGeneric]
Expand Down Expand Up @@ -3283,8 +3283,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`2) -> builtins.list[T`2]"
reveal_type(dec(either)) # N: Revealed type is "def [T] (T`4, T`4) -> builtins.list[T`4]"
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(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 @@ -3302,8 +3302,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`2]) -> T`2"
reveal_type(dec(either)) # N: Revealed type is "def [T] (builtins.list[T`4], builtins.list[T`4]) -> T`4"
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"
[builtins fixtures/tuple.pyi]

[case testInferenceAgainstGenericVariadicPopOff]
Expand All @@ -3322,9 +3322,9 @@ 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 () -> def [T] (T`1) -> T`1"
reveal_type(dec(either)) # N: Revealed type is "def [T] (T`4) -> def (T`4) -> T`4"
reveal_type(dec(pair)) # N: Revealed type is "def [V] (V`-2) -> def [T] (T`7) -> Tuple[T`7, V`-2]"
reveal_type(dec(id)) # N: Revealed type is "def () -> def [T] (T`2) -> T`2"
reveal_type(dec(either)) # N: Revealed type is "def [T] (T`5) -> def (T`5) -> T`5"
reveal_type(dec(pair)) # N: Revealed type is "def [V] (V`-2) -> def [T] (T`8) -> Tuple[T`8, V`-2]"
reveal_type(dec(dec)) # N: Revealed type is "def () -> def [T, Ts, S] (def (T`-1, *Unpack[Ts`-2]) -> S`-3) -> def (*Unpack[Ts`-2]) -> def (T`-1) -> S`-3"
[builtins fixtures/list.pyi]

Expand All @@ -3344,11 +3344,11 @@ def id() -> Callable[[U], U]: ...
def either(x: U) -> Callable[[U], U]: ...
def pair(x: U) -> Callable[[V], Tuple[V, U]]: ...

reveal_type(dec(id)) # N: Revealed type is "def [T] (T`2) -> T`2"
reveal_type(dec(either)) # N: Revealed type is "def [T] (T`5, T`5) -> T`5"
reveal_type(dec(pair)) # N: Revealed type is "def [T, U] (T`8, U`-1) -> Tuple[T`8, U`-1]"
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`11, def () -> def (T`11) -> S`12) -> S`12"
reveal_type(dec(dec)) # N: Revealed type is "def [T, S] (T`12, def () -> def (T`12) -> S`13) -> S`13"
[builtins fixtures/list.pyi]

[case testInferenceAgainstGenericVariadicVsVariadic]
Expand All @@ -3366,9 +3366,9 @@ class Bar(Generic[Unpack[Ts], T]): ...

def dec(f: Callable[[Unpack[Ts]], T]) -> Callable[[Unpack[Ts]], List[T]]: ...
def f(*args: Unpack[Us]) -> Foo[Unpack[Us]]: ...
reveal_type(dec(f)) # N: Revealed type is "def [Ts] (*Unpack[Ts`1]) -> builtins.list[__main__.Foo[Unpack[Ts`1]]]"
reveal_type(dec(f)) # N: Revealed type is "def [Ts] (*Unpack[Ts`2]) -> builtins.list[__main__.Foo[Unpack[Ts`2]]]"
g: Callable[[Unpack[Us]], Foo[Unpack[Us]]]
reveal_type(dec(g)) # N: Revealed type is "def [Ts] (*Unpack[Ts`3]) -> builtins.list[__main__.Foo[Unpack[Ts`3]]]"
reveal_type(dec(g)) # N: Revealed type is "def [Ts] (*Unpack[Ts`4]) -> builtins.list[__main__.Foo[Unpack[Ts`4]]]"
[builtins fixtures/list.pyi]

[case testInferenceAgainstGenericVariadicVsVariadicConcatenate]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -3807,7 +3807,7 @@ def Negate(count: int, /, metric: Metric[float]) -> float: ...
def Combine(count: int, m1: Metric[T], m2: Metric[T], /, *more: Metric[T]) -> T: ...

reveal_type(Negate) # N: Revealed type is "def (metric: __main__.Metric[builtins.float]) -> builtins.float"
reveal_type(Combine) # N: Revealed type is "def [T] (def () -> T`4, def () -> T`4, *more: def () -> T`4) -> T`4"
reveal_type(Combine) # N: Revealed type is "def [T] (def () -> T`5, def () -> T`5, *more: def () -> T`5) -> T`5"

def m1() -> float: ...
def m2() -> float: ...
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 @@ -905,8 +905,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`3, *_P.args, **_P.kwargs) -> _R`3"
reveal_type(A().func) # N: Revealed type is "def [_P, _R] (action: def (*_P.args, **_P.kwargs) -> _R`7, *_P.args, **_P.kwargs) -> _R`7"
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"

def f(x: int) -> int:
...
Expand Down Expand Up @@ -937,8 +937,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`2, None]) -> __main__.Job[_P`2, None]"
reveal_type(A().func) # N: Revealed type is "def [_P] (action: __main__.Job[_P`4, None]) -> __main__.Job[_P`4, 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(Job(lambda x: x))) # N: Revealed type is "__main__.Job[[x: Any], None]"

def f(x: int, y: int) -> None: ...
Expand Down Expand Up @@ -1100,7 +1100,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`2)"
reveal_type(jf) # N: Revealed type is "def [_T] (x: _T`3)"
reveal_type(jf(1)) # N: Revealed type is "None"
[builtins fixtures/paramspec.pyi]

Expand All @@ -1119,10 +1119,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`2], _T`2]"
reveal_type(j) # N: Revealed type is "__main__.Job[[x: _T`3], _T`3]"

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

Expand Down Expand Up @@ -1622,13 +1622,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`2) -> builtins.list[T`2]"
reveal_type(dec(test)) # N: Revealed type is "def [T] (x: T`3) -> builtins.list[T`3]"

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`4) -> builtins.list[T`4]"
reveal_type(dec(test_with_bound)) # N: Revealed type is "def [T <: __main__.A] (x: T`5) -> builtins.list[T`5]"
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