| 1 |
-- Enforcement of upper bounds |
| 2 |
-- --------------------------- |
| 3 |
|
| 4 |
|
| 5 |
[case testBoundOnGenericFunction] |
| 6 |
from typing import TypeVar |
| 7 |
|
| 8 |
class A: pass |
| 9 |
class B(A): pass |
| 10 |
class C(A): pass |
| 11 |
class D: pass |
| 12 |
|
| 13 |
T = TypeVar('T', bound=A) |
| 14 |
U = TypeVar('U') |
| 15 |
def f(x: T) -> T: pass |
| 16 |
def g(x: U) -> U: |
| 17 |
return f(x) # E: Value of type variable "T" of "f" cannot be "U" |
| 18 |
|
| 19 |
f(A()) |
| 20 |
f(B()) |
| 21 |
f(D()) # E: Value of type variable "T" of "f" cannot be "D" |
| 22 |
|
| 23 |
b = B() |
| 24 |
if int(): |
| 25 |
b = f(b) |
| 26 |
if int(): |
| 27 |
b = f(C()) # E: Incompatible types in assignment (expression has type "C", variable has type "B") |
| 28 |
|
| 29 |
|
| 30 |
[case testBoundOnGenericClass] |
| 31 |
from typing import TypeVar, Generic |
| 32 |
|
| 33 |
class A: pass |
| 34 |
class B(A): pass |
| 35 |
T = TypeVar('T', bound=A) |
| 36 |
|
| 37 |
class G(Generic[T]): |
| 38 |
def __init__(self, x: T) -> None: pass |
| 39 |
|
| 40 |
v: G[A] |
| 41 |
w: G[B] |
| 42 |
x: G[str] # E: Type argument "str" of "G" must be a subtype of "A" |
| 43 |
y = G('a') # E: Value of type variable "T" of "G" cannot be "str" |
| 44 |
z = G(A()) |
| 45 |
z = G(B()) |
| 46 |
|
| 47 |
|
| 48 |
[case testBoundVoid] |
| 49 |
# flags: --no-strict-optional --no-local-partial-types |
| 50 |
from typing import TypeVar, Generic |
| 51 |
T = TypeVar('T', bound=int) |
| 52 |
class C(Generic[T]): |
| 53 |
t = None # type: T |
| 54 |
def get(self) -> T: |
| 55 |
return self.t |
| 56 |
c1 = None # type: C[None] |
| 57 |
c1.get() |
| 58 |
d = c1.get() |
| 59 |
reveal_type(d) # N: Revealed type is "None" |
| 60 |
|
| 61 |
|
| 62 |
[case testBoundAny] |
| 63 |
from typing import TypeVar, Generic |
| 64 |
T = TypeVar('T', bound=int) |
| 65 |
class C(Generic[T]): |
| 66 |
def __init__(self, x: T) -> None: pass |
| 67 |
def f(x: T) -> T: |
| 68 |
return x |
| 69 |
|
| 70 |
def g(): pass |
| 71 |
|
| 72 |
f(g()) |
| 73 |
C(g()) |
| 74 |
z: C |
| 75 |
|
| 76 |
|
| 77 |
[case testBoundHigherOrderWithVoid] |
| 78 |
# flags: --no-strict-optional --no-local-partial-types |
| 79 |
from typing import TypeVar, Callable |
| 80 |
class A: pass |
| 81 |
T = TypeVar('T', bound=A) |
| 82 |
def f(g: Callable[[], T]) -> T: |
| 83 |
return g() |
| 84 |
def h() -> None: pass |
| 85 |
f(h) |
| 86 |
a = f(h) |
| 87 |
reveal_type(a) # N: Revealed type is "None" |
| 88 |
|
| 89 |
|
| 90 |
[case testBoundInheritance] |
| 91 |
from typing import TypeVar, Generic |
| 92 |
class A: pass |
| 93 |
T = TypeVar('T') |
| 94 |
TA = TypeVar('TA', bound=A) |
| 95 |
|
| 96 |
class C(Generic[TA]): pass |
| 97 |
class D0(C[TA], Generic[TA]): pass |
| 98 |
class D1(C[T], Generic[T]): pass # E: Type argument "T" of "C" must be a subtype of "A" |
| 99 |
class D2(C[A]): pass |
| 100 |
class D3(C[str]): pass # E: Type argument "str" of "C" must be a subtype of "A" |
| 101 |
|
| 102 |
|
| 103 |
-- Using information from upper bounds |
| 104 |
-- ----------------------------------- |
| 105 |
|
| 106 |
|
| 107 |
[case testBoundGenericFunctions] |
| 108 |
from typing import TypeVar |
| 109 |
class A: pass |
| 110 |
class B(A): pass |
| 111 |
|
| 112 |
T = TypeVar('T') |
| 113 |
TA = TypeVar('TA', bound=A) |
| 114 |
TB = TypeVar('TB', bound=B) |
| 115 |
|
| 116 |
def f(x: T) -> T: |
| 117 |
return x |
| 118 |
def g(x: TA) -> TA: |
| 119 |
return f(x) |
| 120 |
def h(x: TB) -> TB: |
| 121 |
return g(x) |
| 122 |
def g2(x: TA) -> TA: |
| 123 |
return h(x) # Fail |
| 124 |
|
| 125 |
def j(x: TA) -> A: |
| 126 |
return x |
| 127 |
def k(x: TA) -> B: |
| 128 |
return x # Fail |
| 129 |
[out] |
| 130 |
main:16: error: Value of type variable "TB" of "h" cannot be "TA" |
| 131 |
main:21: error: Incompatible return value type (got "TA", expected "B") |
| 132 |
|
| 133 |
|
| 134 |
[case testBoundMethodUsage] |
| 135 |
from typing import TypeVar |
| 136 |
class A0: |
| 137 |
def foo(self) -> None: pass |
| 138 |
class A(A0): |
| 139 |
def bar(self) -> None: pass |
| 140 |
a = 1 |
| 141 |
@property |
| 142 |
def b(self) -> int: |
| 143 |
return self.a |
| 144 |
class B(A): |
| 145 |
def baz(self) -> None: pass |
| 146 |
|
| 147 |
T = TypeVar('T', bound=A) |
| 148 |
|
| 149 |
def f(x: T) -> T: |
| 150 |
x.foo() |
| 151 |
x.bar() |
| 152 |
x.baz() # E: "T" has no attribute "baz" |
| 153 |
x.a |
| 154 |
x.b |
| 155 |
return x |
| 156 |
|
| 157 |
b = f(B()) |
| 158 |
[builtins fixtures/property.pyi] |
| 159 |
[out] |
| 160 |
|
| 161 |
[case testBoundClassMethod] |
| 162 |
from typing import TypeVar |
| 163 |
class A0: |
| 164 |
@classmethod |
| 165 |
def foo(cls, x: int) -> int: pass |
| 166 |
class A(A0): pass |
| 167 |
|
| 168 |
T = TypeVar('T', bound=A) |
| 169 |
def f(x: T) -> int: |
| 170 |
return x.foo(22) |
| 171 |
[builtins fixtures/classmethod.pyi] |
| 172 |
|
| 173 |
|
| 174 |
[case testBoundClassMethodWithNamedTupleBase] |
| 175 |
from typing import NamedTuple, Type, TypeVar |
| 176 |
class A(NamedTuple): |
| 177 |
@classmethod |
| 178 |
def foo(cls) -> None: ... |
| 179 |
|
| 180 |
T = TypeVar('T', bound=A) |
| 181 |
def f(x: Type[T]) -> None: |
| 182 |
reveal_type(x.foo) # N: Revealed type is "def ()" |
| 183 |
x.foo() |
| 184 |
[builtins fixtures/classmethod.pyi] |
| 185 |
|
| 186 |
|
| 187 |
[case testBoundStaticMethod] |
| 188 |
from typing import TypeVar |
| 189 |
class A0: |
| 190 |
@staticmethod |
| 191 |
def foo(x: int) -> int: pass |
| 192 |
class A(A0): pass |
| 193 |
|
| 194 |
T = TypeVar('T', bound=A) |
| 195 |
def f(x: T) -> int: |
| 196 |
return x.foo(22) |
| 197 |
[builtins fixtures/staticmethod.pyi] |
| 198 |
|
| 199 |
|
| 200 |
[case testBoundOnDecorator] |
| 201 |
from typing import TypeVar, Callable, Any, cast |
| 202 |
T = TypeVar('T', bound=Callable[..., Any]) |
| 203 |
|
| 204 |
def twice(f: T) -> T: |
| 205 |
def result(*args, **kwargs) -> Any: |
| 206 |
f(*args, **kwargs) |
| 207 |
return f(*args, **kwargs) |
| 208 |
return cast(T, result) |
| 209 |
|
| 210 |
@twice |
| 211 |
def foo(x: int) -> int: |
| 212 |
return x |
| 213 |
|
| 214 |
a = 1 |
| 215 |
b = foo(a) |
| 216 |
if int(): |
| 217 |
b = 'a' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 218 |
twice(a) # E: Value of type variable "T" of "twice" cannot be "int" |
| 219 |
[builtins fixtures/args.pyi] |
| 220 |
|
| 221 |
|
| 222 |
[case testIterableBoundUnpacking] |
| 223 |
from typing import Tuple, TypeVar |
| 224 |
TupleT = TypeVar("TupleT", bound=Tuple[int, ...]) |
| 225 |
def f(t: TupleT) -> None: |
| 226 |
a, *b = t |
| 227 |
reveal_type(a) # N: Revealed type is "builtins.int" |
| 228 |
reveal_type(b) # N: Revealed type is "builtins.list[builtins.int]" |
| 229 |
[builtins fixtures/tuple.pyi] |