Skip to content

Commit 294a354

Browse files
Conformance suite: Minor tweaks to several assertions for better compatibility with ty (#2178)
* Conformance suite: Minor tweaks to several assertions for better compatibility with ty * update results for other type checkers * revert some questionable changes to tests around type-parameter defaults * Apply suggestion from @rchen152 Co-authored-by: Rebecca Chen <rchen152@gmail.com> * Mark Zuban as partially conformant with `generics_scoping.toml` * rename tag --------- Co-authored-by: Rebecca Chen <rchen152@gmail.com>
1 parent a26f345 commit 294a354

File tree

9 files changed

+124
-93
lines changed

9 files changed

+124
-93
lines changed

conformance/results/mypy/generics_scoping.toml

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
output = """
2-
generics_scoping.py:29: error: Argument 1 to "meth_2" of "MyClass" has incompatible type "str"; expected "int" [arg-type]
3-
generics_scoping.py:50: error: Type variable "generics_scoping.S" is unbound [valid-type]
4-
generics_scoping.py:50: note: (Hint: Use "Generic[S]" or "Protocol[S]" base class to bind "S" inside a class)
5-
generics_scoping.py:50: note: (Hint: Use "S" in function signature to bind "S" inside a function)
6-
generics_scoping.py:54: error: Type variable "generics_scoping.S" is unbound [valid-type]
7-
generics_scoping.py:54: note: (Hint: Use "Generic[S]" or "Protocol[S]" base class to bind "S" inside a class)
8-
generics_scoping.py:54: note: (Hint: Use "S" in function signature to bind "S" inside a function)
9-
generics_scoping.py:65: error: Free type variable expected in Generic[...] [misc]
10-
generics_scoping.py:75: error: Type variable "T" is bound by an outer class [valid-type]
11-
generics_scoping.py:78: error: Type variable "generics_scoping.T" is unbound [valid-type]
12-
generics_scoping.py:78: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
13-
generics_scoping.py:78: note: (Hint: Use "T" in function signature to bind "T" inside a function)
14-
generics_scoping.py:87: error: Can't use bound type variable "T" to define generic alias [valid-type]
15-
generics_scoping.py:94: error: Type variable "generics_scoping.T" is unbound [valid-type]
16-
generics_scoping.py:94: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
17-
generics_scoping.py:94: note: (Hint: Use "T" in function signature to bind "T" inside a function)
18-
generics_scoping.py:95: error: Type variable "generics_scoping.T" is unbound [valid-type]
19-
generics_scoping.py:95: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
20-
generics_scoping.py:95: note: (Hint: Use "T" in function signature to bind "T" inside a function)
21-
generics_scoping.py:96: error: Type variable "generics_scoping.T" is unbound [valid-type]
22-
generics_scoping.py:96: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
23-
generics_scoping.py:96: note: (Hint: Use "T" in function signature to bind "T" inside a function)
2+
generics_scoping.py:16: error: Expression is of type "int", not "Literal[1]" [assert-type]
3+
generics_scoping.py:20: error: Expression is of type "str", not "Literal['a']" [assert-type]
4+
generics_scoping.py:34: error: Argument 1 to "meth_2" of "MyClass" has incompatible type "str"; expected "int" [arg-type]
5+
generics_scoping.py:50: error: Expression is of type "str", not "Literal['abc']" [assert-type]
6+
generics_scoping.py:54: error: Expression is of type "bytes", not "Literal[b'abc']" [assert-type]
7+
generics_scoping.py:61: error: Type variable "generics_scoping.S" is unbound [valid-type]
8+
generics_scoping.py:61: note: (Hint: Use "Generic[S]" or "Protocol[S]" base class to bind "S" inside a class)
9+
generics_scoping.py:61: note: (Hint: Use "S" in function signature to bind "S" inside a function)
10+
generics_scoping.py:65: error: Type variable "generics_scoping.S" is unbound [valid-type]
11+
generics_scoping.py:65: note: (Hint: Use "Generic[S]" or "Protocol[S]" base class to bind "S" inside a class)
12+
generics_scoping.py:65: note: (Hint: Use "S" in function signature to bind "S" inside a function)
13+
generics_scoping.py:76: error: Free type variable expected in Generic[...] [misc]
14+
generics_scoping.py:86: error: Type variable "T" is bound by an outer class [valid-type]
15+
generics_scoping.py:89: error: Type variable "generics_scoping.T" is unbound [valid-type]
16+
generics_scoping.py:89: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
17+
generics_scoping.py:89: note: (Hint: Use "T" in function signature to bind "T" inside a function)
18+
generics_scoping.py:98: error: Can't use bound type variable "T" to define generic alias [valid-type]
19+
generics_scoping.py:105: error: Type variable "generics_scoping.T" is unbound [valid-type]
20+
generics_scoping.py:105: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
21+
generics_scoping.py:105: note: (Hint: Use "T" in function signature to bind "T" inside a function)
22+
generics_scoping.py:106: error: Type variable "generics_scoping.T" is unbound [valid-type]
23+
generics_scoping.py:106: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
24+
generics_scoping.py:106: note: (Hint: Use "T" in function signature to bind "T" inside a function)
25+
generics_scoping.py:107: error: Type variable "generics_scoping.T" is unbound [valid-type]
26+
generics_scoping.py:107: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
27+
generics_scoping.py:107: note: (Hint: Use "T" in function signature to bind "T" inside a function)
2428
"""
2529
conformance_automated = "Pass"
2630
errors_diff = """

conformance/results/mypy/generics_syntax_scoping.toml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ generics_syntax_scoping.py:95: error: "T" already defined as a type parameter [
1919
generics_syntax_scoping.py:98: error: "T" already defined as a type parameter [misc]
2020
generics_syntax_scoping.py:98: error: Variable "generics_syntax_scoping.ClassE.T" is not valid as a type [valid-type]
2121
generics_syntax_scoping.py:98: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
22-
generics_syntax_scoping.py:116: error: Expression is of type "Any", not "TypeVar" [assert-type]
23-
generics_syntax_scoping.py:116: note: "assert_type" expects everything to be "Any" in unchecked functions
24-
generics_syntax_scoping.py:121: error: Expression is of type "Any", not "complex" [assert-type]
25-
generics_syntax_scoping.py:121: note: "assert_type" expects everything to be "Any" in unchecked functions
26-
generics_syntax_scoping.py:124: error: Expression is of type "Any", not "complex" [assert-type]
27-
generics_syntax_scoping.py:124: note: "assert_type" expects everything to be "Any" in unchecked functions
22+
generics_syntax_scoping.py:117: error: Expression is of type "Any", not "TypeVar" [assert-type]
23+
generics_syntax_scoping.py:117: note: "assert_type" expects everything to be "Any" in unchecked functions
24+
generics_syntax_scoping.py:122: error: Expression is of type "Any", not "complex" [assert-type]
25+
generics_syntax_scoping.py:122: note: "assert_type" expects everything to be "Any" in unchecked functions
26+
generics_syntax_scoping.py:125: error: Expression is of type "Any", not "complex" [assert-type]
27+
generics_syntax_scoping.py:125: note: "assert_type" expects everything to be "Any" in unchecked functions
2828
"""
2929
conformance_automated = "Fail"
3030
errors_diff = """
3131
Line 62: Unexpected errors ['generics_syntax_scoping.py:62: error: Expression is of type "Any", not "str" [assert-type]']
3232
Line 67: Unexpected errors ['generics_syntax_scoping.py:67: error: Expression is of type "Any", not "int" [assert-type]']
33-
Line 116: Unexpected errors ['generics_syntax_scoping.py:116: error: Expression is of type "Any", not "TypeVar" [assert-type]']
34-
Line 121: Unexpected errors ['generics_syntax_scoping.py:121: error: Expression is of type "Any", not "complex" [assert-type]']
35-
Line 124: Unexpected errors ['generics_syntax_scoping.py:124: error: Expression is of type "Any", not "complex" [assert-type]']
33+
Line 117: Unexpected errors ['generics_syntax_scoping.py:117: error: Expression is of type "Any", not "TypeVar" [assert-type]']
34+
Line 122: Unexpected errors ['generics_syntax_scoping.py:122: error: Expression is of type "Any", not "complex" [assert-type]']
35+
Line 125: Unexpected errors ['generics_syntax_scoping.py:125: error: Expression is of type "Any", not "complex" [assert-type]']
3636
"""

conformance/results/pyrefly/generics_scoping.toml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ Does not implement several scoping checks/restrictions for generics
44
"""
55
conformance_automated = "Fail"
66
errors_diff = """
7-
Line 50: Expected 1 errors
8-
Line 54: Expected 1 errors
9-
Line 75: Expected 1 errors
10-
Line 78: Expected 1 errors
11-
Line 87: Expected 1 errors
12-
Line 94: Expected 1 errors
13-
Line 95: Expected 1 errors
14-
Line 96: Expected 1 errors
7+
Line 61: Expected 1 errors
8+
Line 65: Expected 1 errors
9+
Line 86: Expected 1 errors
10+
Line 89: Expected 1 errors
11+
Line 98: Expected 1 errors
12+
Line 105: Expected 1 errors
13+
Line 106: Expected 1 errors
14+
Line 107: Expected 1 errors
1515
"""
1616
output = """
17-
ERROR generics_scoping.py:29:10-13: Argument `Literal['a']` is not assignable to parameter `x` with type `int` in function `MyClass.meth_2` [bad-argument-type]
18-
ERROR generics_scoping.py:65:11-20: Redundant type parameter declaration [invalid-type-var]
17+
ERROR generics_scoping.py:16:12-34: assert_type(int, Literal[1]) failed [assert-type]
18+
ERROR generics_scoping.py:20:12-38: assert_type(str, Literal['a']) failed [assert-type]
19+
ERROR generics_scoping.py:34:10-13: Argument `Literal['a']` is not assignable to parameter `x` with type `int` in function `MyClass.meth_2` [bad-argument-type]
20+
ERROR generics_scoping.py:50:12-48: assert_type(str, Literal['abc']) failed [assert-type]
21+
ERROR generics_scoping.py:54:12-50: assert_type(bytes, Literal[b'abc']) failed [assert-type]
22+
ERROR generics_scoping.py:76:11-20: Redundant type parameter declaration [invalid-type-var]
1923
"""

conformance/results/pyright/generics_scoping.toml

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
conformant = "Pass"
22
output = """
3-
generics_scoping.py:29:10 - error: Argument of type "Literal['a']" cannot be assigned to parameter "x" of type "int" in function "meth_2"
3+
generics_scoping.py:16:13 - error: "assert_type" mismatch: expected "Literal[1]" but received "int" (reportAssertTypeFailure)
4+
generics_scoping.py:20:13 - error: "assert_type" mismatch: expected "Literal['a']" but received "str" (reportAssertTypeFailure)
5+
generics_scoping.py:34:10 - error: Argument of type "Literal['a']" cannot be assigned to parameter "x" of type "int" in function "meth_2"
46
  "Literal['a']" is not assignable to "int" (reportArgumentType)
5-
generics_scoping.py:50:13 - error: Type variable "S" has no meaning in this context (reportGeneralTypeIssues)
6-
generics_scoping.py:54:19 - error: Type variable "S" has no meaning in this context (reportGeneralTypeIssues)
7-
generics_scoping.py:65:29 - error: TypeVar "T" is already in use by an outer scope (reportGeneralTypeIssues)
8-
generics_scoping.py:75:24 - error: TypeVar "T" is already in use by an outer scope (reportGeneralTypeIssues)
9-
generics_scoping.py:78:17 - error: Type variable "T" has no meaning in this context (reportGeneralTypeIssues)
10-
generics_scoping.py:87:5 - error: Generic type alias within class cannot use bound type variables T (reportInvalidTypeForm)
11-
generics_scoping.py:94:14 - error: Type variable "T" has no meaning in this context (reportGeneralTypeIssues)
12-
generics_scoping.py:95:19 - error: Type variable "T" has no meaning in this context (reportGeneralTypeIssues)
13-
generics_scoping.py:96:6 - error: Type variable "T" has no meaning in this context (reportGeneralTypeIssues)
7+
generics_scoping.py:50:13 - error: "assert_type" mismatch: expected "Literal['abc']" but received "str" (reportAssertTypeFailure)
8+
generics_scoping.py:54:13 - error: "assert_type" mismatch: expected "Literal[b"abc"]" but received "bytes" (reportAssertTypeFailure)
9+
generics_scoping.py:61:13 - error: Type variable "S" has no meaning in this context (reportGeneralTypeIssues)
10+
generics_scoping.py:65:19 - error: Type variable "S" has no meaning in this context (reportGeneralTypeIssues)
11+
generics_scoping.py:76:29 - error: TypeVar "T" is already in use by an outer scope (reportGeneralTypeIssues)
12+
generics_scoping.py:86:24 - error: TypeVar "T" is already in use by an outer scope (reportGeneralTypeIssues)
13+
generics_scoping.py:89:17 - error: Type variable "T" has no meaning in this context (reportGeneralTypeIssues)
14+
generics_scoping.py:98:5 - error: Generic type alias within class cannot use bound type variables T (reportInvalidTypeForm)
15+
generics_scoping.py:105:14 - error: Type variable "T" has no meaning in this context (reportGeneralTypeIssues)
16+
generics_scoping.py:106:19 - error: Type variable "T" has no meaning in this context (reportGeneralTypeIssues)
17+
generics_scoping.py:107:6 - error: Type variable "T" has no meaning in this context (reportGeneralTypeIssues)
1418
"""
1519
conformance_automated = "Pass"
1620
errors_diff = """

conformance/results/results.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ <h3>Python Type System Conformance Test Results</h3>
307307
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generics_scoping</th>
308308
<th class="column col2 conformant">Pass</th>
309309
<th class="column col2 conformant">Pass</th>
310-
<th class="column col2 conformant">Pass</th>
310+
<th class="column col2 partially-conformant">Partial</th>
311311
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not implement several scoping checks/restrictions for generics</p></span></div></th>
312312
</tr>
313313
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;generics_self_advanced</th>
Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
conformance_automated = "Pass"
1+
conformance_automated = "Fail"
2+
conformant = "Partial"
23
errors_diff = """
4+
Lines 15, 16: Expected error (tag 'fun1')
5+
Lines 19, 20: Expected error (tag 'fun2')
6+
Lines 49, 50: Expected error (tag 'method-str')
7+
Lines 53, 54: Expected error (tag 'method-bytes')
38
"""
49
output = """
5-
generics_scoping.py:29: error: Argument 1 to "meth_2" of "MyClass" has incompatible type "str"; expected "int" [arg-type]
6-
generics_scoping.py:50: error: Type variable "generics_scoping.S" is unbound [misc]
7-
generics_scoping.py:50: note: (Hint: Use "Generic[S]" or "Protocol[S]" base class to bind "S" inside a class)
8-
generics_scoping.py:50: note: (Hint: Use "S" in function signature to bind "S" inside a function)
9-
generics_scoping.py:54: error: Type variable "generics_scoping.S" is unbound [misc]
10-
generics_scoping.py:54: note: (Hint: Use "Generic[S]" or "Protocol[S]" base class to bind "S" inside a class)
11-
generics_scoping.py:54: note: (Hint: Use "S" in function signature to bind "S" inside a function)
12-
generics_scoping.py:65: error: Free type variable expected in Generic[...] [misc]
13-
generics_scoping.py:75: error: Type variable "T" is bound by an outer class [misc]
14-
generics_scoping.py:78: error: Type variable "T" is bound by an outer class [misc]
15-
generics_scoping.py:80: error: Type variable "T" is bound by an outer class [misc]
16-
generics_scoping.py:87: error: Can't use bound type variable "T" to define generic alias [misc]
17-
generics_scoping.py:94: error: Type variable "generics_scoping.T" is unbound [misc]
18-
generics_scoping.py:94: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
19-
generics_scoping.py:94: note: (Hint: Use "T" in function signature to bind "T" inside a function)
20-
generics_scoping.py:95: error: Type variable "generics_scoping.T" is unbound [misc]
21-
generics_scoping.py:95: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
22-
generics_scoping.py:95: note: (Hint: Use "T" in function signature to bind "T" inside a function)
23-
generics_scoping.py:96: error: Type variable "generics_scoping.T" is unbound [misc]
24-
generics_scoping.py:96: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
25-
generics_scoping.py:96: note: (Hint: Use "T" in function signature to bind "T" inside a function)
10+
generics_scoping.py:34: error: Argument 1 to "meth_2" of "MyClass" has incompatible type "str"; expected "int" [arg-type]
11+
generics_scoping.py:61: error: Type variable "generics_scoping.S" is unbound [misc]
12+
generics_scoping.py:61: note: (Hint: Use "Generic[S]" or "Protocol[S]" base class to bind "S" inside a class)
13+
generics_scoping.py:61: note: (Hint: Use "S" in function signature to bind "S" inside a function)
14+
generics_scoping.py:65: error: Type variable "generics_scoping.S" is unbound [misc]
15+
generics_scoping.py:65: note: (Hint: Use "Generic[S]" or "Protocol[S]" base class to bind "S" inside a class)
16+
generics_scoping.py:65: note: (Hint: Use "S" in function signature to bind "S" inside a function)
17+
generics_scoping.py:76: error: Free type variable expected in Generic[...] [misc]
18+
generics_scoping.py:86: error: Type variable "T" is bound by an outer class [misc]
19+
generics_scoping.py:89: error: Type variable "T" is bound by an outer class [misc]
20+
generics_scoping.py:91: error: Type variable "T" is bound by an outer class [misc]
21+
generics_scoping.py:98: error: Can't use bound type variable "T" to define generic alias [misc]
22+
generics_scoping.py:105: error: Type variable "generics_scoping.T" is unbound [misc]
23+
generics_scoping.py:105: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
24+
generics_scoping.py:105: note: (Hint: Use "T" in function signature to bind "T" inside a function)
25+
generics_scoping.py:106: error: Type variable "generics_scoping.T" is unbound [misc]
26+
generics_scoping.py:106: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
27+
generics_scoping.py:106: note: (Hint: Use "T" in function signature to bind "T" inside a function)
28+
generics_scoping.py:107: error: Type variable "generics_scoping.T" is unbound [misc]
29+
generics_scoping.py:107: note: (Hint: Use "Generic[T]" or "Protocol[T]" base class to bind "T" inside a class)
30+
generics_scoping.py:107: note: (Hint: Use "T" in function signature to bind "T" inside a function)
2631
"""

conformance/tests/annotations_generators.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,6 @@ async def generator30() -> AsyncIterator[int]:
190190
yield
191191

192192

193-
assert_type(generator30, Callable[[], AsyncIterator[int]])
193+
async def uses_generator30() -> None:
194+
async for x in generator30():
195+
assert_type(x, int)

conformance/tests/generics_scoping.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Specification: https://typing.readthedocs.io/en/latest/spec/generics.html#scoping-rules-for-type-variables
22

3-
from typing import TypeVar, Generic, Iterable, TypeAlias, assert_type
3+
from typing import TypeVar, Generic, Iterable, TypeAlias, assert_type, Literal
44

55
# > A type variable used in a generic function could be inferred to represent
66
# > different types in the same code block.
@@ -11,8 +11,13 @@ def fun_1(x: T) -> T: # T here
1111
def fun_2(x: T) -> T: # and here could be different
1212
return x
1313

14-
assert_type(fun_1(1), int)
15-
assert_type(fun_2('a'), str)
14+
# One of these two should pass; either is acceptable:
15+
assert_type(fun_1(1), int) # E[fun1]
16+
assert_type(fun_1(1), Literal[1]) # E[fun1]
17+
18+
# One of these two should pass; either is acceptable:
19+
assert_type(fun_2("a"), str) # E[fun2]
20+
assert_type(fun_2("a"), Literal["a"]) # E[fun2]
1621

1722
# > A type variable used in a method of a generic class that coincides
1823
# > with one of the variables that parameterize this class is always bound
@@ -39,8 +44,14 @@ def method(self, x: T, y: S) -> S:
3944
return y
4045

4146
x: Foo[int] = Foo()
42-
assert_type(x.method(0, "abc"), str)
43-
assert_type(x.method(0, b"abc"), bytes)
47+
48+
# Either of these is acceptable; one of the two should pass:
49+
assert_type(x.method(0, "abc"), str) # E[method-str]
50+
assert_type(x.method(0, "abc"), Literal["abc"]) # E[method-str]
51+
52+
# Either of these is acceptable; one of the two should pass:
53+
assert_type(x.method(0, b"abc"), bytes) # E[method-bytes]
54+
assert_type(x.method(0, b"abc"), Literal[b"abc"]) # E[method-bytes]
4455

4556
# > Unbound type variables should not appear in the bodies of generic functions,
4657
# > or in the class bodies apart from method definitions.

conformance/tests/generics_syntax_scoping.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,24 @@ def method3[T](self, x: T): # E
102102
T = int(0)
103103

104104

105-
class Outer2[T]:
106-
T = int(1)
107-
108-
assert_type(T, int)
105+
def f(a: int, b: str, c: complex):
106+
class Outer2[T]:
107+
T = a
109108

110-
class Inner1:
111-
T = str("")
109+
assert_type(T, int)
112110

113-
assert_type(T, str)
111+
class Inner1:
112+
T = b
114113

115-
def inner_method(self):
116-
assert_type(T, TypeVar)
114+
assert_type(T, str)
117115

118-
def outer_method(self):
119-
T = 3j
116+
def inner_method(self):
117+
assert_type(T, TypeVar)
120118

121-
assert_type(T, complex)
119+
def outer_method(self):
120+
T = c
122121

123-
def inner_func():
124122
assert_type(T, complex)
123+
124+
def inner_func():
125+
assert_type(T, complex)

0 commit comments

Comments
 (0)