File: check-type-object-type-inference.test

package info (click to toggle)
mypy 1.19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,412 kB
  • sloc: python: 114,754; ansic: 13,343; cpp: 11,380; makefile: 257; sh: 28
file content (41 lines) | stat: -rw-r--r-- 1,915 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[case testInferTupleType]
from typing import TypeVar, Generic, Type
from abc import abstractmethod
import types  # Explicitly bring in stubs for 'types'

T = TypeVar('T')
class E(Generic[T]):
    @abstractmethod
    def e(self, t: T) -> str:
        ...

class F:
    @abstractmethod
    def f(self, tp: Type[T]) -> E[T]:
        ...

def g(f: F):
    f.f(int).e(7)
    f.f(tuple[int,str])
    f.f(tuple[int,str]).e('x')  # E: Argument 1 to "e" of "E" has incompatible type "str"; expected "tuple[int, str]"
    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]"
    f.f(tuple[int,str]).e( (7,'x') )  # OK
    reveal_type(f.f(tuple[int,str]).e)  # N: Revealed type is "def (t: tuple[builtins.int, builtins.str]) -> builtins.str"

def h(f: F):
    f.f(int).e(7)
    f.f(tuple)
    f.f(tuple).e('y') # E: Argument 1 to "e" of "E" has incompatible type "str"; expected "tuple[Any, ...]"
    f.f(tuple).e( (8,'y') )  # OK
    reveal_type(f.f(tuple).e)  # N: Revealed type is "def (t: builtins.tuple[Any, ...]) -> builtins.str"

def i(f: F):
    f.f(tuple[int,tuple[int,str]])
    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]]"
    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]]"
    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]]"
    f.f(tuple[int,tuple[int,str]]).e( (27,(28,'z')) )  # OK
    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"

x = tuple[int,str][str]  # False negative
[builtins fixtures/tuple.pyi]