| 1 |
-- Test cases for type variables with values restriction. |
| 2 |
|
| 3 |
|
| 4 |
[case testCallGenericFunctionWithTypeVarValueRestriction] |
| 5 |
from typing import TypeVar |
| 6 |
T = TypeVar('T', int, str) |
| 7 |
def f(x: T) -> None: pass |
| 8 |
f(1) |
| 9 |
f('x') |
| 10 |
f(object()) # E: Value of type variable "T" of "f" cannot be "object" |
| 11 |
|
| 12 |
[case testCallGenericFunctionWithTypeVarValueRestrictionUsingContext] |
| 13 |
from typing import TypeVar, List |
| 14 |
T = TypeVar('T', int, str) |
| 15 |
def f(x: T) -> List[T]: pass |
| 16 |
i = [1] |
| 17 |
s = ['x'] |
| 18 |
o = [object()] |
| 19 |
if int(): |
| 20 |
i = f(1) |
| 21 |
s = f('') |
| 22 |
o = f(1) \ |
| 23 |
# E: Incompatible types in assignment (expression has type "list[int]", variable has type "list[object]") \ |
| 24 |
# N: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ |
| 25 |
# N: Consider using "Sequence" instead, which is covariant |
| 26 |
[builtins fixtures/list.pyi] |
| 27 |
|
| 28 |
[case testCallGenericFunctionWithTypeVarValueRestrictionAndAnyArgs] |
| 29 |
from typing import TypeVar, Any, cast |
| 30 |
T = TypeVar('T', int, str) |
| 31 |
def f(x: T) -> None: pass |
| 32 |
f(cast(Any, object())) |
| 33 |
[out] |
| 34 |
|
| 35 |
[case testCallGenericFunctionWithTypeVarValueRestrictionInDynamicFunc] |
| 36 |
from typing import TypeVar, Any |
| 37 |
T = TypeVar('T', int, str) |
| 38 |
def f(x: T) -> None: pass |
| 39 |
def g(): |
| 40 |
f(object()) |
| 41 |
[out] |
| 42 |
|
| 43 |
[case testCallGenericFunctionWithTypeVarValueRestrictionUsingSubtype] |
| 44 |
from typing import TypeVar |
| 45 |
T = TypeVar('T', int, str) |
| 46 |
def f(x: T) -> None: pass |
| 47 |
class S(str): pass |
| 48 |
f(S()) |
| 49 |
[out] |
| 50 |
|
| 51 |
[case testCheckGenericFunctionBodyWithTypeVarValues] |
| 52 |
from typing import TypeVar |
| 53 |
class A: |
| 54 |
def f(self, x: int) -> A: return self |
| 55 |
class B: |
| 56 |
def f(self, x: int) -> B: return self |
| 57 |
AB = TypeVar('AB', A, B) |
| 58 |
def f(x: AB) -> AB: |
| 59 |
x = x.f(1) |
| 60 |
return x.f(1) |
| 61 |
|
| 62 |
[case testCheckGenericFunctionBodyWithTypeVarValues2] |
| 63 |
from typing import TypeVar |
| 64 |
class A: |
| 65 |
def f(self) -> A: return A() |
| 66 |
def g(self) -> B: return B() |
| 67 |
class B: |
| 68 |
def f(self) -> A: return A() |
| 69 |
def g(self) -> B: return B() |
| 70 |
AB = TypeVar('AB', A, B) |
| 71 |
def f(x: AB) -> AB: |
| 72 |
return x.f() # Error |
| 73 |
def g(x: AB) -> AB: |
| 74 |
return x.g() # Error |
| 75 |
[out] |
| 76 |
main:10: error: Incompatible return value type (got "A", expected "B") |
| 77 |
main:12: error: Incompatible return value type (got "B", expected "A") |
| 78 |
|
| 79 |
[case testTypeInferenceAndTypeVarValues] |
| 80 |
from typing import TypeVar |
| 81 |
class A: |
| 82 |
def f(self) -> A: return self |
| 83 |
def g(self) -> B: return B() |
| 84 |
class B: |
| 85 |
def f(self) -> B: return self |
| 86 |
def g(self) -> B: return B() |
| 87 |
AB = TypeVar('AB', A, B) |
| 88 |
def f(x: AB) -> AB: |
| 89 |
y = x |
| 90 |
if y: |
| 91 |
return y.f() |
| 92 |
else: |
| 93 |
return y.g() # E: Incompatible return value type (got "B", expected "A") |
| 94 |
[out] |
| 95 |
|
| 96 |
[case testTypeDeclaredBasedOnTypeVarWithValues] |
| 97 |
from typing import TypeVar |
| 98 |
T = TypeVar('T', int, str) |
| 99 |
def f(x: T) -> T: |
| 100 |
a: T |
| 101 |
b: T |
| 102 |
if 1: |
| 103 |
a = x |
| 104 |
b = x |
| 105 |
a = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 106 |
b = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 107 |
return x |
| 108 |
[out] |
| 109 |
|
| 110 |
[case testIsinstanceAndTypeVarValues] |
| 111 |
from typing import TypeVar |
| 112 |
T = TypeVar('T', int, str) |
| 113 |
def f(x: T) -> T: |
| 114 |
if isinstance(x, int): |
| 115 |
return 2 |
| 116 |
return x |
| 117 |
def g(x: T) -> T: |
| 118 |
if isinstance(x, str): |
| 119 |
return '' |
| 120 |
return x |
| 121 |
def h(x: T) -> T: |
| 122 |
if isinstance(x, int): |
| 123 |
return '' # E: Incompatible return value type (got "str", expected "int") |
| 124 |
return x |
| 125 |
[builtins fixtures/isinstance.pyi] |
| 126 |
[out] |
| 127 |
|
| 128 |
[case testIsinstanceAndTypeVarValues2] |
| 129 |
from typing import TypeVar |
| 130 |
T = TypeVar('T', int, str) |
| 131 |
def f(x: T) -> T: |
| 132 |
if isinstance(x, int): |
| 133 |
return 2 |
| 134 |
else: |
| 135 |
return '' |
| 136 |
def g(x: T) -> T: |
| 137 |
if isinstance(x, int): |
| 138 |
return '' # E: Incompatible return value type (got "str", expected "int") |
| 139 |
else: |
| 140 |
return 2 # E: Incompatible return value type (got "int", expected "str") |
| 141 |
return x |
| 142 |
[builtins fixtures/isinstance.pyi] |
| 143 |
[out] |
| 144 |
|
| 145 |
[case testIsinstanceAndTypeVarValues3] |
| 146 |
from typing import TypeVar |
| 147 |
T = TypeVar('T', int, str) |
| 148 |
def f(x: T) -> T: |
| 149 |
if isinstance(x, int): |
| 150 |
y = 1 |
| 151 |
else: |
| 152 |
y = '' |
| 153 |
return y |
| 154 |
[builtins fixtures/isinstance.pyi] |
| 155 |
|
| 156 |
[case testIsinstanceAndTypeVarValues4] |
| 157 |
from typing import TypeVar |
| 158 |
T = TypeVar('T', int, str) |
| 159 |
def f(x: T) -> T: |
| 160 |
if isinstance(x, int): |
| 161 |
y = 1 |
| 162 |
else: |
| 163 |
y = object() |
| 164 |
return y # E: Incompatible return value type (got "object", expected "str") |
| 165 |
[builtins fixtures/isinstance.pyi] |
| 166 |
[out] |
| 167 |
|
| 168 |
[case testIsinstanceAndTypeVarValues5] |
| 169 |
from typing import TypeVar |
| 170 |
T = TypeVar('T', int, str) |
| 171 |
def f(x: T) -> T: |
| 172 |
if isinstance(x, int): |
| 173 |
y = object() |
| 174 |
else: |
| 175 |
y = '' |
| 176 |
return y # E: Incompatible return value type (got "object", expected "int") |
| 177 |
[builtins fixtures/isinstance.pyi] |
| 178 |
[out] |
| 179 |
|
| 180 |
[case testIsinstanceWithUserDefinedTypeAndTypeVarValues] |
| 181 |
# flags: --warn-unreachable |
| 182 |
from typing import TypeVar |
| 183 |
class A: pass |
| 184 |
class B: pass |
| 185 |
T1 = TypeVar('T1', A, B) |
| 186 |
def f1(x: T1) -> None: |
| 187 |
y = x |
| 188 |
if isinstance(x, A): |
| 189 |
x = y |
| 190 |
x = A() # E: Incompatible types in assignment (expression has type "A", variable has type "B") |
| 191 |
else: |
| 192 |
x = B() |
| 193 |
x = y |
| 194 |
x.foo() # E: "B" has no attribute "foo" |
| 195 |
|
| 196 |
class C: |
| 197 |
field: int |
| 198 |
class D: |
| 199 |
field: str |
| 200 |
T2 = TypeVar('T2', C, D) |
| 201 |
def f2(x: T2) -> None: |
| 202 |
y = x |
| 203 |
if isinstance(x, C): |
| 204 |
# C and D are non-overlapping, so this branch is never checked |
| 205 |
x = y |
| 206 |
x = C() |
| 207 |
else: |
| 208 |
x = D() |
| 209 |
x = y |
| 210 |
x.foo() # E: "D" has no attribute "foo" |
| 211 |
|
| 212 |
S = TypeVar('S', int, str) |
| 213 |
def g(x: S) -> None: |
| 214 |
y = x |
| 215 |
if isinstance(x, int): |
| 216 |
x = y |
| 217 |
[builtins fixtures/isinstance.pyi] |
| 218 |
[out] |
| 219 |
|
| 220 |
[case testIsinstanceWithUserDefinedTypeAndTypeVarValues2] |
| 221 |
from typing import TypeVar |
| 222 |
class S(str): pass |
| 223 |
T = TypeVar('T', S, int) |
| 224 |
def f(x: T) -> None: |
| 225 |
y = x |
| 226 |
if isinstance(x, S): |
| 227 |
# This is checked only when type of x is str. |
| 228 |
x = y |
| 229 |
x = S() |
| 230 |
x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "S") |
| 231 |
else: |
| 232 |
x = y |
| 233 |
x = 1 |
| 234 |
x = S() # E: Incompatible types in assignment (expression has type "S", variable has type "int") |
| 235 |
[builtins fixtures/isinstance.pyi] |
| 236 |
[out] |
| 237 |
|
| 238 |
[case testTypeVarValuesAndNestedCalls] |
| 239 |
from typing import TypeVar |
| 240 |
T = TypeVar('T', int, str) |
| 241 |
def f(m: T) -> int: pass |
| 242 |
def h(x: int) -> int: pass |
| 243 |
def g(a: T) -> None: |
| 244 |
h(f(a)) |
| 245 |
[out] |
| 246 |
|
| 247 |
[case testGenericTypeWithTypevarValues] |
| 248 |
from typing import TypeVar, Generic, Any |
| 249 |
X = TypeVar('X', int, str) |
| 250 |
class A(Generic[X]): pass |
| 251 |
a: A[int] |
| 252 |
b: A[str] |
| 253 |
d: A[object] # E: Value of type variable "X" of "A" cannot be "object" |
| 254 |
c: A[Any] |
| 255 |
|
| 256 |
[case testConstructGenericTypeWithTypevarValuesAndTypeInference] |
| 257 |
from typing import TypeVar, Generic, Any, cast |
| 258 |
X = TypeVar('X', int, str) |
| 259 |
class A(Generic[X]): |
| 260 |
def __init__(self, x: X) -> None: pass |
| 261 |
A(1) |
| 262 |
A('x') |
| 263 |
A(cast(Any, object())) |
| 264 |
A(object()) # E: Value of type variable "X" of "A" cannot be "object" |
| 265 |
|
| 266 |
[case testGenericTypeWithTypevarValuesAndTypevarArgument] |
| 267 |
from typing import TypeVar, Generic |
| 268 |
class C: pass |
| 269 |
X = TypeVar('X', int, str) |
| 270 |
Y = TypeVar('Y', int, C) |
| 271 |
Z = TypeVar('Z') |
| 272 |
class D(Generic[X]): |
| 273 |
def __init__(self, x: X) -> None: pass |
| 274 |
def f(x: X) -> None: |
| 275 |
a: D[X] |
| 276 |
def g(x: Y) -> None: |
| 277 |
a: D[Y] |
| 278 |
def h(x: Z) -> None: |
| 279 |
a: D[Z] |
| 280 |
[out] |
| 281 |
main:11: error: Invalid type argument value for "D" |
| 282 |
main:13: error: Type variable "Z" not valid as type argument value for "D" |
| 283 |
|
| 284 |
[case testGenericTypeWithTypevarValuesAndSubtypePromotion] |
| 285 |
from typing import TypeVar, Generic |
| 286 |
X = TypeVar('X', int, str) |
| 287 |
class S(str): pass |
| 288 |
class C(Generic[X]): |
| 289 |
def __init__(self, x: X) -> None: pass |
| 290 |
x: C[str] |
| 291 |
y = C(S()) |
| 292 |
if int(): |
| 293 |
x = y |
| 294 |
y = x |
| 295 |
c_int = C(1) # type: C[int] |
| 296 |
if int(): |
| 297 |
y = c_int # E: Incompatible types in assignment (expression has type "C[int]", variable has type "C[str]") |
| 298 |
|
| 299 |
[case testGenericTypeBodyWithTypevarValues] |
| 300 |
from typing import TypeVar, Generic |
| 301 |
class A: |
| 302 |
def f(self, x: int) -> None: pass |
| 303 |
def g(self, x: int) -> None: pass |
| 304 |
def h(self, x: str) -> None: pass |
| 305 |
class B: |
| 306 |
def f(self, x: int) -> None: pass |
| 307 |
def g(self, x: str) -> None: pass |
| 308 |
def h(self, x: int) -> None: pass |
| 309 |
X = TypeVar('X', A, B) |
| 310 |
class C(Generic[X]): |
| 311 |
def f(self, x: X) -> None: |
| 312 |
x.f(1) |
| 313 |
x.g(1) # E: Argument 1 to "g" of "B" has incompatible type "int"; expected "str" |
| 314 |
x.h(1) # E: Argument 1 to "h" of "A" has incompatible type "int"; expected "str" |
| 315 |
[out] |
| 316 |
|
| 317 |
[case testAttributeInGenericTypeWithTypevarValues1] |
| 318 |
from typing import TypeVar, Generic |
| 319 |
X = TypeVar('X', int, str) |
| 320 |
class C(Generic[X]): |
| 321 |
x: X |
| 322 |
def f(self, x: X) -> None: |
| 323 |
self.x = x |
| 324 |
self.x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 325 |
[out] |
| 326 |
|
| 327 |
[case testAttributeInGenericTypeWithTypevarValues2] |
| 328 |
from typing import TypeVar, Generic |
| 329 |
X = TypeVar('X', int, str) |
| 330 |
class C(Generic[X]): |
| 331 |
x: X |
| 332 |
cn = C() # type: C[int] |
| 333 |
cn.x = 1 |
| 334 |
cn.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 335 |
cs = C() # type: C[str] |
| 336 |
cs.x = '' |
| 337 |
cs.x = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str") |
| 338 |
|
| 339 |
[case testAttributeInGenericTypeWithTypevarValues3] |
| 340 |
from typing import TypeVar, Generic |
| 341 |
X = TypeVar('X', int, str) |
| 342 |
class C(Generic[X]): |
| 343 |
def f(self, x: X) -> None: |
| 344 |
self.x = x # type: X |
| 345 |
ci: C[int] |
| 346 |
cs: C[str] |
| 347 |
reveal_type(ci.x) # N: Revealed type is "builtins.int" |
| 348 |
reveal_type(cs.x) # N: Revealed type is "builtins.str" |
| 349 |
|
| 350 |
[case testAttributeInGenericTypeWithTypevarValuesUsingInference1] |
| 351 |
from typing import TypeVar, Generic |
| 352 |
X = TypeVar('X', int, str) |
| 353 |
class C(Generic[X]): |
| 354 |
def f(self, x: X) -> None: |
| 355 |
self.x = x # E: Need type annotation for "x" |
| 356 |
ci: C[int] |
| 357 |
cs: C[str] |
| 358 |
reveal_type(ci.x) # N: Revealed type is "Any" |
| 359 |
reveal_type(cs.x) # N: Revealed type is "Any" |
| 360 |
|
| 361 |
[case testAttributeInGenericTypeWithTypevarValuesUsingInference2] |
| 362 |
from typing import TypeVar, Generic |
| 363 |
X = TypeVar('X', int, str) |
| 364 |
class C(Generic[X]): |
| 365 |
def f(self, x: X) -> None: |
| 366 |
self.x = 1 |
| 367 |
reveal_type(self.x) # N: Revealed type is "builtins.int" |
| 368 |
ci: C[int] |
| 369 |
cs: C[str] |
| 370 |
reveal_type(ci.x) # N: Revealed type is "builtins.int" |
| 371 |
reveal_type(cs.x) # N: Revealed type is "builtins.int" |
| 372 |
|
| 373 |
[case testAttributeInGenericTypeWithTypevarValuesUsingInference3] |
| 374 |
from typing import TypeVar, Generic |
| 375 |
X = TypeVar('X', int, str) |
| 376 |
class C(Generic[X]): |
| 377 |
x: X |
| 378 |
def f(self) -> None: |
| 379 |
self.y = self.x # E: Need type annotation for "y" |
| 380 |
ci: C[int] |
| 381 |
cs: C[str] |
| 382 |
reveal_type(ci.y) # N: Revealed type is "Any" |
| 383 |
reveal_type(cs.y) # N: Revealed type is "Any" |
| 384 |
|
| 385 |
[case testInferredAttributeInGenericClassBodyWithTypevarValues] |
| 386 |
from typing import TypeVar, Generic |
| 387 |
X = TypeVar('X', int, str) |
| 388 |
class C(Generic[X]): |
| 389 |
x = 1 |
| 390 |
C.x = 1 |
| 391 |
C.x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 392 |
|
| 393 |
[case testMultipleClassTypevarsWithValues1] |
| 394 |
from typing import TypeVar, Generic |
| 395 |
class A: |
| 396 |
def f(self, x: int) -> None: pass |
| 397 |
class B: |
| 398 |
def f(self, x: str) -> None: pass |
| 399 |
X = TypeVar('X', A, B) |
| 400 |
Y = TypeVar('Y', int, str) |
| 401 |
class C(Generic[X, Y]): |
| 402 |
def f(self, x: X, y: Y) -> None: |
| 403 |
x.f(y) |
| 404 |
[out] |
| 405 |
main:10: error: Argument 1 to "f" of "A" has incompatible type "str"; expected "int" |
| 406 |
main:10: error: Argument 1 to "f" of "B" has incompatible type "int"; expected "str" |
| 407 |
|
| 408 |
[case testMultipleClassTypevarsWithValues2] |
| 409 |
from typing import TypeVar, Generic |
| 410 |
class A: pass |
| 411 |
class B: pass |
| 412 |
X = TypeVar('X', A, B) |
| 413 |
Y = TypeVar('Y', int, str) |
| 414 |
class C(Generic[X, Y]): pass |
| 415 |
a: C[A, int] |
| 416 |
b: C[B, str] |
| 417 |
c: C[int, int] # E: Value of type variable "X" of "C" cannot be "int" |
| 418 |
d: C[A, A] # E: Value of type variable "Y" of "C" cannot be "A" |
| 419 |
|
| 420 |
[case testCallGenericFunctionUsingMultipleTypevarsWithValues] |
| 421 |
from typing import TypeVar |
| 422 |
class A: pass |
| 423 |
class B: pass |
| 424 |
X = TypeVar('X', A, B) |
| 425 |
Y = TypeVar('Y', int, str) |
| 426 |
def f(x: X, y: Y) -> None: pass |
| 427 |
f(A(), '') |
| 428 |
f(B(), 1) |
| 429 |
f(A(), A()) # E: Value of type variable "Y" of "f" cannot be "A" |
| 430 |
f(1, 1) # E: Value of type variable "X" of "f" cannot be "int" |
| 431 |
|
| 432 |
[case testGenericFunctionWithNormalAndRestrictedTypevar] |
| 433 |
from typing import TypeVar, Generic |
| 434 |
X = TypeVar('X') |
| 435 |
Y = TypeVar('Y', int, str) |
| 436 |
class C(Generic[Y]): |
| 437 |
def __init__(self, y: Y) -> None: pass |
| 438 |
def f(x: X, y: Y, z: int) -> None: |
| 439 |
C(y) |
| 440 |
C(x) # Error |
| 441 |
if int(): |
| 442 |
z = x # Error |
| 443 |
z = y # Error |
| 444 |
y.foo # Error |
| 445 |
[out] |
| 446 |
main:8: error: Value of type variable "Y" of "C" cannot be "X" |
| 447 |
main:10: error: Incompatible types in assignment (expression has type "X", variable has type "int") |
| 448 |
main:11: error: Incompatible types in assignment (expression has type "str", variable has type "int") |
| 449 |
main:12: error: "int" has no attribute "foo" |
| 450 |
main:12: error: "str" has no attribute "foo" |
| 451 |
|
| 452 |
[case testTypeVarWithValueInferredFromObjectReturnTypeContext] |
| 453 |
from typing import TypeVar |
| 454 |
T = TypeVar('T', int, str) |
| 455 |
def c1(x: object) -> None: pass |
| 456 |
def c2(x: int) -> None: pass |
| 457 |
def c3(x: str) -> None: pass |
| 458 |
def g(x: T) -> T: pass |
| 459 |
c1(g('')) |
| 460 |
c2(g(1)) |
| 461 |
c3(g('')) |
| 462 |
c2(g('')) # E: Argument 1 to "c2" has incompatible type "str"; expected "int" |
| 463 |
c3(g(1)) # E: Argument 1 to "c3" has incompatible type "int"; expected "str" |
| 464 |
|
| 465 |
[case testTypeVarWithValueInferredFromObjectReturnTypeContext2] |
| 466 |
from typing import TypeVar |
| 467 |
T = TypeVar('T', int, str) |
| 468 |
class ss(str): pass |
| 469 |
def c(x: ss) -> None: pass |
| 470 |
def g(x: T) -> T: pass |
| 471 |
c(g('')) |
| 472 |
c(g(1)) |
| 473 |
[out] |
| 474 |
main:6: error: Argument 1 to "c" has incompatible type "str"; expected "ss" |
| 475 |
main:7: error: Argument 1 to "c" has incompatible type "int"; expected "ss" |
| 476 |
|
| 477 |
[case testDefineAttributeInGenericMethodUsingTypeVarWithValues] |
| 478 |
from typing import TypeVar |
| 479 |
T = TypeVar('T', int, str) |
| 480 |
class A: |
| 481 |
def f(self, x: T) -> None: |
| 482 |
self.x = x # E: Need type annotation for "x" |
| 483 |
self.y = [x] # E: Need type annotation for "y" |
| 484 |
self.z = 1 |
| 485 |
reveal_type(A().x) # N: Revealed type is "Any" |
| 486 |
reveal_type(A().y) # N: Revealed type is "Any" |
| 487 |
reveal_type(A().z) # N: Revealed type is "builtins.int" |
| 488 |
[builtins fixtures/list.pyi] |
| 489 |
|
| 490 |
|
| 491 |
-- Special cases |
| 492 |
-- ------------- |
| 493 |
|
| 494 |
|
| 495 |
[case testTypevarValuesSpecialCase1] |
| 496 |
from typing import TypeVar, Generic |
| 497 |
from abc import abstractmethod |
| 498 |
T = TypeVar('T', int, str) |
| 499 |
class A(Generic[T]): |
| 500 |
@abstractmethod |
| 501 |
def f(self) -> 'A[T]': pass |
| 502 |
class B(A[str]): |
| 503 |
@abstractmethod |
| 504 |
def f(self) -> 'B': pass |
| 505 |
class C(A[str]): |
| 506 |
@abstractmethod |
| 507 |
def f(self) -> int: # E: Return type "int" of "f" incompatible with return type "A[str]" in supertype "A" |
| 508 |
pass |
| 509 |
[out] |
| 510 |
|
| 511 |
[case testDefaultArgumentValueInGenericClassWithTypevarValues] |
| 512 |
from typing import TypeVar, Generic |
| 513 |
T = TypeVar('T', int, str) |
| 514 |
class C(Generic[T]): |
| 515 |
def f(self, x: int = 2) -> None: pass |
| 516 |
|
| 517 |
[case testTypevarValuesWithOverloadedFunctionSpecialCase] |
| 518 |
from foo import * |
| 519 |
[file foo.pyi] |
| 520 |
from typing import TypeVar, overload, Callable |
| 521 |
|
| 522 |
T = TypeVar('T', int, str) |
| 523 |
def f(x: T) -> None: |
| 524 |
y = m(g, x) |
| 525 |
if int(): |
| 526 |
x = y |
| 527 |
y = object() # Error |
| 528 |
|
| 529 |
A = TypeVar('A') |
| 530 |
R = TypeVar('R') |
| 531 |
def m(f: Callable[[A], R], it: A) -> A: pass |
| 532 |
|
| 533 |
@overload |
| 534 |
def g(x: int) -> int: return x |
| 535 |
@overload |
| 536 |
def g(x: str) -> str: return x |
| 537 |
[out] |
| 538 |
tmp/foo.pyi:8: error: Incompatible types in assignment (expression has type "object", variable has type "int") |
| 539 |
tmp/foo.pyi:8: error: Incompatible types in assignment (expression has type "object", variable has type "str") |
| 540 |
|
| 541 |
[case testGenericFunctionSubtypingWithTypevarValues] |
| 542 |
from typing import TypeVar |
| 543 |
class A: pass |
| 544 |
T = TypeVar('T', int, str) |
| 545 |
U = TypeVar('U', str, A, int) |
| 546 |
def f(x: T) -> T: pass |
| 547 |
def g(x: U) -> U: pass |
| 548 |
a = f |
| 549 |
if int(): |
| 550 |
a = f |
| 551 |
if int(): |
| 552 |
a = g |
| 553 |
b = g |
| 554 |
if int(): |
| 555 |
b = g |
| 556 |
if int(): |
| 557 |
b = f # E: Incompatible types in assignment (expression has type "Callable[[T], T]", variable has type "Callable[[U], U]") |
| 558 |
|
| 559 |
[case testInnerFunctionWithTypevarValues] |
| 560 |
from typing import TypeVar |
| 561 |
T = TypeVar('T', int, str) |
| 562 |
U = TypeVar('U', int, str) |
| 563 |
def outer(x: T) -> T: |
| 564 |
def inner(y: T) -> T: |
| 565 |
return x |
| 566 |
def inner2(y: U) -> U: |
| 567 |
return y |
| 568 |
inner(x) |
| 569 |
inner(3) # E: Argument 1 to "inner" has incompatible type "int"; expected "str" |
| 570 |
inner2(x) |
| 571 |
inner2(3) |
| 572 |
outer(3) |
| 573 |
return x |
| 574 |
[out] |
| 575 |
|
| 576 |
[case testInnerFunctionMutualRecursionWithTypevarValues] |
| 577 |
from typing import TypeVar |
| 578 |
T = TypeVar('T', int, str) |
| 579 |
def outer(x: T) -> T: |
| 580 |
def inner1(y: T) -> T: |
| 581 |
return inner2(y) |
| 582 |
def inner2(y: T) -> T: |
| 583 |
return inner1('a') # E: Argument 1 to "inner1" has incompatible type "str"; expected "int" |
| 584 |
return inner1(x) |
| 585 |
[out] |
| 586 |
|
| 587 |
[case testClassMemberTypeVarInFunctionBody] |
| 588 |
from typing import TypeVar, List |
| 589 |
S = TypeVar('S') |
| 590 |
class C: |
| 591 |
T = TypeVar('T', bound=int) |
| 592 |
def f(self, x: T) -> T: |
| 593 |
L = List[S] |
| 594 |
y: L[C.T] = [x] |
| 595 |
reveal_type(C.T) # N: Revealed type is "typing.TypeVar" |
| 596 |
return y[0] |
| 597 |
[builtins fixtures/list.pyi] |
| 598 |
[typing fixtures/typing-full.pyi] |
| 599 |
|
| 600 |
[case testTypeVarWithAnyTypeBound] |
| 601 |
# flags: --follow-imports=skip |
| 602 |
from typing import Type, TypeVar |
| 603 |
from a import A |
| 604 |
T = TypeVar('T', bound=A) |
| 605 |
def method(t: Type[T]) -> None: |
| 606 |
t.a |
| 607 |
[file a.py] |
| 608 |
class A: |
| 609 |
a: int = 7 |
| 610 |
[out] |
| 611 |
|
| 612 |
[case testParameterLessGenericAsRestriction] |
| 613 |
from typing import Sequence, Iterable, TypeVar |
| 614 |
S = TypeVar('S', Sequence, Iterable) |
| 615 |
def my_len(s: S) -> None: pass |
| 616 |
def crash() -> None: my_len((0,)) |
| 617 |
[builtins fixtures/tuple.pyi] |
| 618 |
|
| 619 |
[case testReferenceToDecoratedFunctionAndTypeVarValues] |
| 620 |
from typing import TypeVar, Callable |
| 621 |
|
| 622 |
T = TypeVar('T') |
| 623 |
S = TypeVar('S', int, str) |
| 624 |
|
| 625 |
def dec(f: Callable[..., T]) -> Callable[..., T]: ... |
| 626 |
|
| 627 |
@dec |
| 628 |
def g(s: S) -> Callable[[S], None]: ... |
| 629 |
|
| 630 |
def f(x: S) -> None: |
| 631 |
h = g(x) |
| 632 |
h(x) |
| 633 |
|
| 634 |
[case testTypeVarWithTypedDictBoundInIndexExpression] |
| 635 |
from typing import TypedDict, TypeVar |
| 636 |
|
| 637 |
class Data(TypedDict): |
| 638 |
x: int |
| 639 |
|
| 640 |
|
| 641 |
T = TypeVar("T", bound=Data) |
| 642 |
|
| 643 |
|
| 644 |
def f(data: T) -> None: |
| 645 |
reveal_type(data["x"]) # N: Revealed type is "builtins.int" |
| 646 |
[builtins fixtures/dict.pyi] |
| 647 |
[typing fixtures/typing-typeddict.pyi] |
| 648 |
|
| 649 |
[case testTypeVarWithUnionTypedDictBoundInIndexExpression] |
| 650 |
from typing import TypedDict, TypeVar, Union, Dict |
| 651 |
|
| 652 |
class Data(TypedDict): |
| 653 |
x: int |
| 654 |
|
| 655 |
|
| 656 |
T = TypeVar("T", bound=Union[Data, Dict[str, str]]) |
| 657 |
|
| 658 |
|
| 659 |
def f(data: T) -> None: |
| 660 |
reveal_type(data["x"]) # N: Revealed type is "builtins.int | builtins.str" |
| 661 |
[builtins fixtures/dict.pyi] |
| 662 |
[typing fixtures/typing-typeddict.pyi] |
| 663 |
|
| 664 |
[case testTypeVarWithTypedDictValueInIndexExpression] |
| 665 |
from typing import TypedDict, TypeVar, Union, Dict |
| 666 |
|
| 667 |
class Data(TypedDict): |
| 668 |
x: int |
| 669 |
|
| 670 |
|
| 671 |
T = TypeVar("T", Data, Dict[str, str]) |
| 672 |
|
| 673 |
|
| 674 |
def f(data: T) -> None: |
| 675 |
_: Union[str, int] = data["x"] |
| 676 |
[builtins fixtures/dict.pyi] |
| 677 |
[typing fixtures/typing-typeddict.pyi] |
| 678 |
|
| 679 |
[case testSelfTypeVarIndexExpr] |
| 680 |
from typing import TypedDict, TypeVar, Union, Type |
| 681 |
|
| 682 |
T = TypeVar("T", bound="Indexable") |
| 683 |
|
| 684 |
class Indexable: |
| 685 |
def __init__(self, index: str) -> None: |
| 686 |
self.index = index |
| 687 |
|
| 688 |
def __getitem__(self: T, index: str) -> T: |
| 689 |
return self._new_instance(index) |
| 690 |
|
| 691 |
@classmethod |
| 692 |
def _new_instance(cls: Type[T], index: str) -> T: |
| 693 |
return cls("foo") |
| 694 |
|
| 695 |
def m(self: T) -> T: |
| 696 |
return self["foo"] |
| 697 |
[builtins fixtures/classmethod.pyi] |
| 698 |
[typing fixtures/typing-full.pyi] |
| 699 |
|
| 700 |
[case testTypeVarWithValueDeferral] |
| 701 |
from typing import TypeVar, Callable |
| 702 |
|
| 703 |
T = TypeVar("T", "A", "B") |
| 704 |
Func = Callable[[], T] |
| 705 |
|
| 706 |
class A: ... |
| 707 |
class B: ... |
| 708 |
|
| 709 |
[case testTypeCommentInGenericTypeWithConstrainedTypeVar] |
| 710 |
from typing import Generic, TypeVar |
| 711 |
|
| 712 |
NT = TypeVar("NT", int, float) |
| 713 |
|
| 714 |
class Foo1(Generic[NT]): |
| 715 |
p = 1 # type: int |
| 716 |
|
| 717 |
class Foo2(Generic[NT]): |
| 718 |
p, q = 1, 2.0 # type: (int, float) |
| 719 |
|
| 720 |
class Foo3(Generic[NT]): |
| 721 |
def bar(self) -> None: |
| 722 |
p = 1 # type: int |
| 723 |
|
| 724 |
class Foo4(Generic[NT]): |
| 725 |
def bar(self) -> None: |
| 726 |
p, q = 1, 2.0 # type: (int, float) |
| 727 |
|
| 728 |
def foo3(x: NT) -> None: |
| 729 |
p = 1 # type: int |
| 730 |
|
| 731 |
def foo4(x: NT) -> None: |
| 732 |
p, q = 1, 2.0 # type: (int, float) |
| 733 |
[builtins fixtures/tuple.pyi] |
| 734 |
|
| 735 |
[case testTypeVarValuesNarrowing] |
| 736 |
from typing import TypeVar |
| 737 |
|
| 738 |
W = TypeVar("W", int, str) |
| 739 |
|
| 740 |
def fn(w: W) -> W: |
| 741 |
if type(w) is str: |
| 742 |
reveal_type(w) # N: Revealed type is "builtins.str" |
| 743 |
elif type(w) is int: |
| 744 |
reveal_type(w) # N: Revealed type is "builtins.int" |
| 745 |
return w |
| 746 |
[builtins fixtures/isinstance.pyi] |
| 747 |
|
| 748 |
[case testTypeVarValuesSubtypeOfAll] |
| 749 |
from typing import TypeVar |
| 750 |
|
| 751 |
class B: ... |
| 752 |
class C(B): ... |
| 753 |
|
| 754 |
S = TypeVar("S", B, C) |
| 755 |
b = B() |
| 756 |
c = C() |
| 757 |
def g(x: S = c): # OK |
| 758 |
... |
| 759 |
def h(x: S = b): # E: Incompatible default for parameter "x" (default has type "B", parameter has type "S") |
| 760 |
... |