|
| 1 | +[case testInferTupleType] |
| 2 | +# flags: --python-version 3.9 |
| 3 | +from typing import TypeVar, Generic, Type |
| 4 | +from abc import abstractmethod |
| 5 | + |
| 6 | +T = TypeVar('T') |
| 7 | +class E(Generic[T]): |
| 8 | + @abstractmethod |
| 9 | + def e(self, t: T) -> str: |
| 10 | + ... |
| 11 | + |
| 12 | +class F: |
| 13 | + @abstractmethod |
| 14 | + def f(self, tp: Type[T]) -> E[T]: |
| 15 | + ... |
| 16 | + |
| 17 | +def g(f: F): |
| 18 | + f.f(int).e(7) |
| 19 | + f.f(tuple[int,str]) |
| 20 | + f.f(tuple[int,str]).e('x') # E: Argument 1 to "e" of "E" has incompatible type "str"; expected "Tuple[int, str]" |
| 21 | + f.f(tuple[int,str]).e( (7,8) ) # E: Argument 1 to "e" of "E" has incompatible type "Tuple[int, int]"; expected "Tuple[int, str]" |
| 22 | + f.f(tuple[int,str]).e( (7,'x') ) # OK |
| 23 | + reveal_type(f.f(tuple[int,str]).e) # N: Revealed type is "def (t: Tuple[builtins.int, builtins.str]) -> builtins.str" |
| 24 | + |
| 25 | +def h(f: F): |
| 26 | + f.f(int).e(7) |
| 27 | + f.f(tuple) |
| 28 | + f.f(tuple).e('y') # E: Argument 1 to "e" of "E" has incompatible type "str"; expected "Tuple[Any, ...]" |
| 29 | + f.f(tuple).e( (8,'y') ) # OK |
| 30 | + reveal_type(f.f(tuple).e) # N: Revealed type is "def (t: builtins.tuple[Any, ...]) -> builtins.str" |
| 31 | + |
| 32 | +def i(f: F): |
| 33 | + f.f(tuple[int,tuple[int,str]]) |
| 34 | + f.f(tuple[int,tuple[int,str]]).e('z') # E: Argument 1 to "e" of "E" has incompatible type "str"; expected "Tuple[int, Tuple[int, str]]" |
| 35 | + f.f(tuple[int,tuple[int,str]]).e( (8,9) ) # E: Argument 1 to "e" of "E" has incompatible type "Tuple[int, int]"; expected "Tuple[int, Tuple[int, str]]" |
| 36 | + f.f(tuple[int,tuple[int,str]]).e( (17, (28, 29)) ) # E: Argument 1 to "e" of "E" has incompatible type "Tuple[int, Tuple[int, int]]"; expected "Tuple[int, Tuple[int, str]]" |
| 37 | + f.f(tuple[int,tuple[int,str]]).e( (27,(28,'z')) ) # OK |
| 38 | + reveal_type(f.f(tuple[int,tuple[int,str]]).e) # N: Revealed type is "def (t: Tuple[builtins.int, Tuple[builtins.int, builtins.str]]) -> builtins.str" |
| 39 | + |
| 40 | +x = tuple[int,str][str] # E: The type "Type[Tuple[Any, ...]]" is not generic and not indexable |
| 41 | +[builtins fixtures/tuple.pyi] |
0 commit comments