-
Notifications
You must be signed in to change notification settings - Fork 293
Conformance suite: Minor tweaks to several assertions for better compatibility with ty #2178
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
Changes from 3 commits
11875d9
652dde5
7b6a5d2
88bf117
9955ae1
d59f132
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,30 @@ | ||
| conformance_automated = "Pass" | ||
| conformance_automated = "Fail" | ||
| errors_diff = """ | ||
| Lines 15, 16: Expected error (tag 'fun1-int') | ||
| Lines 19, 20: Expected error (tag 'fun1-str') | ||
| Lines 49, 50: Expected error (tag 'method-str') | ||
| Lines 53, 54: Expected error (tag 'method-bytes') | ||
| """ | ||
| output = """ | ||
| generics_scoping.py:29: error: Argument 1 to "meth_2" of "MyClass" has incompatible type "str"; expected "int" [arg-type] | ||
| generics_scoping.py:50: error: Type variable "generics_scoping.S" is unbound [misc] | ||
| generics_scoping.py:50: note: (Hint: Use "Generic[S]" or "Protocol[S]" base class to bind "S" inside a class) | ||
| generics_scoping.py:50: note: (Hint: Use "S" in function signature to bind "S" inside a function) | ||
| generics_scoping.py:54: error: Type variable "generics_scoping.S" is unbound [misc] | ||
| generics_scoping.py:54: note: (Hint: Use "Generic[S]" or "Protocol[S]" base class to bind "S" inside a class) | ||
| generics_scoping.py:54: note: (Hint: Use "S" in function signature to bind "S" inside a function) | ||
| generics_scoping.py:65: error: Free type variable expected in Generic[...] [misc] | ||
| generics_scoping.py:75: error: Type variable "T" is bound by an outer class [misc] | ||
| generics_scoping.py:78: error: Type variable "T" is bound by an outer class [misc] | ||
| generics_scoping.py:80: error: Type variable "T" is bound by an outer class [misc] | ||
| generics_scoping.py:87: error: Can't use bound type variable "T" to define generic alias [misc] | ||
| generics_scoping.py:94: error: Type variable "generics_scoping.T" is unbound [misc] | ||
| generics_scoping.py:94: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class) | ||
| generics_scoping.py:94: note: (Hint: Use "T" in function signature to bind "T" inside a function) | ||
| generics_scoping.py:95: error: Type variable "generics_scoping.T" is unbound [misc] | ||
| generics_scoping.py:95: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class) | ||
| generics_scoping.py:95: note: (Hint: Use "T" in function signature to bind "T" inside a function) | ||
| generics_scoping.py:96: error: Type variable "generics_scoping.T" is unbound [misc] | ||
| generics_scoping.py:96: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class) | ||
| generics_scoping.py:96: note: (Hint: Use "T" in function signature to bind "T" inside a function) | ||
| generics_scoping.py:34: error: Argument 1 to "meth_2" of "MyClass" has incompatible type "str"; expected "int" [arg-type] | ||
| generics_scoping.py:61: error: Type variable "generics_scoping.S" is unbound [misc] | ||
| generics_scoping.py:61: note: (Hint: Use "Generic[S]" or "Protocol[S]" base class to bind "S" inside a class) | ||
| generics_scoping.py:61: note: (Hint: Use "S" in function signature to bind "S" inside a function) | ||
| generics_scoping.py:65: error: Type variable "generics_scoping.S" is unbound [misc] | ||
| generics_scoping.py:65: note: (Hint: Use "Generic[S]" or "Protocol[S]" base class to bind "S" inside a class) | ||
| generics_scoping.py:65: note: (Hint: Use "S" in function signature to bind "S" inside a function) | ||
| generics_scoping.py:76: error: Free type variable expected in Generic[...] [misc] | ||
| generics_scoping.py:86: error: Type variable "T" is bound by an outer class [misc] | ||
| generics_scoping.py:89: error: Type variable "T" is bound by an outer class [misc] | ||
| generics_scoping.py:91: error: Type variable "T" is bound by an outer class [misc] | ||
| generics_scoping.py:98: error: Can't use bound type variable "T" to define generic alias [misc] | ||
| generics_scoping.py:105: error: Type variable "generics_scoping.T" is unbound [misc] | ||
| generics_scoping.py:105: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class) | ||
| generics_scoping.py:105: note: (Hint: Use "T" in function signature to bind "T" inside a function) | ||
| generics_scoping.py:106: error: Type variable "generics_scoping.T" is unbound [misc] | ||
| generics_scoping.py:106: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class) | ||
| generics_scoping.py:106: note: (Hint: Use "T" in function signature to bind "T" inside a function) | ||
| generics_scoping.py:107: error: Type variable "generics_scoping.T" is unbound [misc] | ||
| generics_scoping.py:107: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class) | ||
| generics_scoping.py:107: note: (Hint: Use "T" in function signature to bind "T" inside a function) | ||
| """ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,4 +190,6 @@ async def generator30() -> AsyncIterator[int]: | |
| yield | ||
|
|
||
|
|
||
| assert_type(generator30, Callable[[], AsyncIterator[int]]) | ||
| async def uses_generator30() -> None: | ||
| async for x in generator30(): | ||
| assert_type(x, int) | ||
|
Comment on lines
-193
to
+195
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ty infers a "function-literal" type for I don't believe the spec states anywhere that type checkers must infer a I tried doing just |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| # Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#scoping-rules-for-type-variables | ||
|
|
||
| from typing import TypeVar, Generic, Iterable, TypeAlias, assert_type | ||
| from typing import TypeVar, Generic, Iterable, TypeAlias, assert_type, Literal | ||
|
|
||
| # > A type variable used in a generic function could be inferred to represent | ||
| # > different types in the same code block. | ||
|
|
@@ -11,8 +11,13 @@ def fun_1(x: T) -> T: # T here | |
| def fun_2(x: T) -> T: # and here could be different | ||
| return x | ||
|
|
||
| assert_type(fun_1(1), int) | ||
| assert_type(fun_2('a'), str) | ||
| # One of these two should pass; either is acceptable: | ||
| assert_type(fun_1(1), int) # E[fun1-int] | ||
| assert_type(fun_1(1), Literal[1]) # E[fun1-int] | ||
|
||
|
|
||
| # One of these two should pass; either is acceptable: | ||
| assert_type(fun_1("a"), str) # E[fun1-str] | ||
| assert_type(fun_1("a"), Literal["a"]) # E[fun1-str] | ||
AlexWaygood marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # > A type variable used in a method of a generic class that coincides | ||
| # > with one of the variables that parameterize this class is always bound | ||
|
|
@@ -39,8 +44,14 @@ def method(self, x: T, y: S) -> S: | |
| return y | ||
|
|
||
| x: Foo[int] = Foo() | ||
| assert_type(x.method(0, "abc"), str) | ||
| assert_type(x.method(0, b"abc"), bytes) | ||
|
|
||
| # Either of these is acceptable; one of the two should pass: | ||
| assert_type(x.method(0, "abc"), str) # E[method-str] | ||
| assert_type(x.method(0, "abc"), Literal["abc"]) # E[method-str] | ||
|
|
||
| # Either of these is acceptable; one of the two should pass: | ||
| assert_type(x.method(0, b"abc"), bytes) # E[method-bytes] | ||
| assert_type(x.method(0, b"abc"), Literal[b"abc"]) # E[method-bytes] | ||
|
|
||
| # > Unbound type variables should not appear in the bodies of generic functions, | ||
| # > or in the class bodies apart from method definitions. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -102,23 +102,24 @@ def method3[T](self, x: T): # E | |
| T = int(0) | ||
|
|
||
|
|
||
| class Outer2[T]: | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the assertions in this test ran into issues because ty inferred ty also rejected the |
||
| T = int(1) | ||
|
|
||
| assert_type(T, int) | ||
| def f(a: int, b: str, c: complex): | ||
| class Outer2[T]: | ||
| T = a | ||
|
|
||
| class Inner1: | ||
| T = str("") | ||
| assert_type(T, int) | ||
|
|
||
| assert_type(T, str) | ||
| class Inner1: | ||
| T = b | ||
|
|
||
| def inner_method(self): | ||
| assert_type(T, TypeVar) | ||
| assert_type(T, str) | ||
|
|
||
| def outer_method(self): | ||
| T = 3j | ||
| def inner_method(self): | ||
| assert_type(T, TypeVar) | ||
|
|
||
| assert_type(T, complex) | ||
| def outer_method(self): | ||
| T = c | ||
|
|
||
| def inner_func(): | ||
| assert_type(T, complex) | ||
|
|
||
| def inner_func(): | ||
| assert_type(T, complex) | ||
Uh oh!
There was an error while loading. Please reload this page.