Skip to content

Commit 11875d9

Browse files
committed
Conformance suite: Minor tweaks to several assertions for better compatibility with ty
1 parent ffd520a commit 11875d9

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

conformance/tests/annotations_generators.py

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

192192

193-
assert_type(generator30, Callable[[], AsyncIterator[int]])
193+
assert_type(generator30(), AsyncIterator[int])

conformance/tests/generics_defaults.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ class OneDefault(Generic[T, DefaultBoolT]): ...
4141

4242
class AllTheDefaults(Generic[T1, T2, DefaultStrT, DefaultIntT, DefaultBoolT]): ...
4343

44+
def needs_allthedefaults_or_subclass(x: type[AllTheDefaults[Any, Any, str, int, bool]]) -> None: ...
45+
46+
# do not use `assert_type` here: some type checkers infer a more precise type
47+
# than `type[]` for class objects
48+
needs_allthedefaults_or_subclass(AllTheDefaults) # OK
4449

45-
assert_type(AllTheDefaults, type[AllTheDefaults[Any, Any, str, int, bool]])
4650
assert_type(
4751
AllTheDefaults[int, complex], type[AllTheDefaults[int, complex, str, int, bool]]
4852
)
@@ -91,7 +95,12 @@ class Class_ParamSpec(Generic[DefaultP]): ...
9195
class Class_TypeVarTuple(Generic[*DefaultTs]): ...
9296

9397

94-
assert_type(Class_TypeVarTuple, type[Class_TypeVarTuple[*tuple[str, int]]])
98+
def needs_classtypevartuple_or_subclass(x: type[Class_TypeVarTuple[Any, Any]]) -> None: ...
99+
100+
# do not use `assert_type` here: some type checkers infer a more precise type
101+
# than `type[]` for class objects
102+
needs_classtypevartuple_or_subclass(Class_TypeVarTuple) # OK
103+
95104
assert_type(Class_TypeVarTuple(), Class_TypeVarTuple[str, int])
96105
assert_type(Class_TypeVarTuple[int, bool](), Class_TypeVarTuple[int, bool])
97106

conformance/tests/generics_defaults_referential.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@ class Bar(Generic[Z1, ListDefaultT]): # OK
9191
def __init__(self, x: Z1, y: ListDefaultT): ...
9292

9393

94-
assert_type(Bar, type[Bar[Any, list[Any]]])
94+
def requires_bar_or_subclass(x: type[Bar[Any, list[Any]]]) -> None: ...
95+
96+
# Don't use `assert_type(Bar, type[Bar[Any, list[Any]]])` here,
97+
# since some type checkers infer a more precise type than `type[]` for
98+
# class objects
99+
requires_bar_or_subclass(Bar) # OK
100+
95101
assert_type(Bar[int], type[Bar[int, list[int]]])
96102
assert_type(Bar[int](0, []), Bar[int, list[int]])
97103
assert_type(Bar[int, list[str]](0, []), Bar[int, list[str]])

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-int]
16+
assert_type(fun_1(1), Literal[1]) # E[fun1-int]
17+
18+
# One of these two should pass; either is acceptable:
19+
assert_type(fun_1("a"), str) # E[fun1-str]
20+
assert_type(fun_1("a"), Literal["a"]) # E[fun1-str]
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: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Specification: https://peps.python.org/pep-0695/#type-parameter-scopes
66

7-
from typing import Callable, Mapping, Sequence, TypeVar, assert_type
7+
from typing import Callable, Mapping, Literal, Sequence, TypeVar, assert_type
88

99
# > A compiler error or runtime exception is generated if the definition
1010
# > of an earlier type parameter references a later type parameter even
@@ -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)