Debian Codegraph @

test-data/unit/check-type-object-type-inference.test

raw
1 [case testInferTupleType]
2 from typing import TypeVar, Generic, Type
3 from abc import abstractmethod
4 import types # Explicitly bring in stubs for 'types'
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] # False negative
41 [builtins fixtures/tuple.pyi]