| 1 |
-- Tests checking that basic functionality works |
| 2 |
|
| 3 |
[case testRecursiveAliasBasic] |
| 4 |
from typing import Dict, List, Union, TypeVar, Sequence |
| 5 |
|
| 6 |
JSON = Union[str, List[JSON], Dict[str, JSON]] |
| 7 |
|
| 8 |
x: JSON = ["foo", {"bar": "baz"}] |
| 9 |
|
| 10 |
reveal_type(x) # N: Revealed type is "builtins.str | builtins.list[...] | builtins.dict[builtins.str, ...]" |
| 11 |
if isinstance(x, list): |
| 12 |
x = x[0] |
| 13 |
|
| 14 |
class Bad: ... |
| 15 |
x = ["foo", {"bar": [Bad()]}] # E: List item 0 has incompatible type "Bad"; expected "str | list[JSON] | dict[str, JSON]" |
| 16 |
[builtins fixtures/isinstancelist.pyi] |
| 17 |
|
| 18 |
[case testRecursiveAliasBasicGenericSubtype] |
| 19 |
from typing import Union, TypeVar, Sequence, List |
| 20 |
|
| 21 |
T = TypeVar("T") |
| 22 |
|
| 23 |
Nested = Sequence[Union[T, Nested[T]]] |
| 24 |
|
| 25 |
class Bad: ... |
| 26 |
x: Nested[int] |
| 27 |
y: Nested[Bad] |
| 28 |
x = y # E: Incompatible types in assignment (expression has type "Nested[Bad]", variable has type "Nested[int]") |
| 29 |
|
| 30 |
NestedOther = Sequence[Union[T, Nested[T]]] |
| 31 |
|
| 32 |
xx: Nested[int] |
| 33 |
yy: NestedOther[bool] |
| 34 |
xx = yy # OK |
| 35 |
[builtins fixtures/isinstancelist.pyi] |
| 36 |
|
| 37 |
[case testRecursiveAliasBasicGenericInference] |
| 38 |
from typing import Union, TypeVar, Sequence, List |
| 39 |
|
| 40 |
T = TypeVar("T") |
| 41 |
|
| 42 |
Nested = Sequence[Union[T, Nested[T]]] |
| 43 |
|
| 44 |
def flatten(arg: Nested[T]) -> List[T]: |
| 45 |
res: List[T] = [] |
| 46 |
for item in arg: |
| 47 |
if isinstance(item, Sequence): |
| 48 |
res.extend(flatten(item)) |
| 49 |
else: |
| 50 |
res.append(item) |
| 51 |
return res |
| 52 |
|
| 53 |
reveal_type(flatten([1, [2, [3]]])) # N: Revealed type is "builtins.list[builtins.int]" |
| 54 |
|
| 55 |
class Bad: ... |
| 56 |
x: Nested[int] = [1, [2, [3]]] |
| 57 |
x = [1, [Bad()]] # E: List item 0 has incompatible type "Bad"; expected "int | Nested[int]" |
| 58 |
[builtins fixtures/isinstancelist.pyi] |
| 59 |
|
| 60 |
[case testRecursiveAliasGenericInferenceNested] |
| 61 |
from typing import Union, TypeVar, Sequence, List |
| 62 |
|
| 63 |
T = TypeVar("T") |
| 64 |
class A: ... |
| 65 |
class B(A): ... |
| 66 |
|
| 67 |
Nested = Sequence[Union[T, Nested[T]]] |
| 68 |
|
| 69 |
def flatten(arg: Nested[T]) -> List[T]: ... |
| 70 |
reveal_type(flatten([[B(), B()]])) # N: Revealed type is "builtins.list[__main__.B]" |
| 71 |
reveal_type(flatten([[[[B()]]]])) # N: Revealed type is "builtins.list[__main__.B]" |
| 72 |
reveal_type(flatten([[B(), [[B()]]]])) # N: Revealed type is "builtins.list[__main__.B]" |
| 73 |
[builtins fixtures/isinstancelist.pyi] |
| 74 |
|
| 75 |
[case testRecursiveAliasNewStyleSupported] |
| 76 |
from test import A |
| 77 |
|
| 78 |
x: A |
| 79 |
if isinstance(x, list): |
| 80 |
reveal_type(x[0]) # N: Revealed type is "builtins.int | builtins.list[builtins.int | builtins.list[...]]" |
| 81 |
else: |
| 82 |
reveal_type(x) # N: Revealed type is "builtins.int" |
| 83 |
|
| 84 |
[file test.pyi] |
| 85 |
A = int | list[A] |
| 86 |
[builtins fixtures/isinstancelist.pyi] |
| 87 |
|
| 88 |
-- Tests duplicating some existing type alias tests with recursive aliases enabled |
| 89 |
|
| 90 |
[case testRecursiveAliasesMutual] |
| 91 |
# flags: --disable-error-code used-before-def |
| 92 |
from typing import Type, Callable, Union |
| 93 |
|
| 94 |
A = Union[B, int] |
| 95 |
B = Callable[[C], int] |
| 96 |
C = Type[A] |
| 97 |
x: A |
| 98 |
reveal_type(x) # N: Revealed type is "(def (type[def (...) -> builtins.int] | type[builtins.int]) -> builtins.int) | builtins.int" |
| 99 |
|
| 100 |
[case testRecursiveAliasesProhibited-skip] |
| 101 |
from typing import Type, Callable, Union |
| 102 |
|
| 103 |
A = Union[B, int] |
| 104 |
B = Union[A, int] |
| 105 |
C = Type[C] |
| 106 |
|
| 107 |
[case testRecursiveAliasImported] |
| 108 |
import lib |
| 109 |
x: lib.A |
| 110 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.list[...]]" |
| 111 |
|
| 112 |
[file lib.pyi] |
| 113 |
from typing import List |
| 114 |
from other import B |
| 115 |
A = List[B] |
| 116 |
|
| 117 |
[file other.pyi] |
| 118 |
from typing import List |
| 119 |
from lib import A |
| 120 |
B = List[A] |
| 121 |
[builtins fixtures/list.pyi] |
| 122 |
|
| 123 |
[case testRecursiveAliasViaBaseClass] |
| 124 |
# flags: --disable-error-code used-before-def |
| 125 |
from typing import List |
| 126 |
|
| 127 |
x: B |
| 128 |
B = List[C] |
| 129 |
class C(B): pass |
| 130 |
|
| 131 |
reveal_type(x) # N: Revealed type is "builtins.list[__main__.C]" |
| 132 |
reveal_type(x[0][0]) # N: Revealed type is "__main__.C" |
| 133 |
[builtins fixtures/list.pyi] |
| 134 |
|
| 135 |
[case testRecursiveAliasViaBaseClass2] |
| 136 |
# flags: --disable-error-code used-before-def |
| 137 |
from typing import NewType, List |
| 138 |
|
| 139 |
x: D |
| 140 |
reveal_type(x[0][0]) # N: Revealed type is "__main__.C" |
| 141 |
|
| 142 |
D = List[C] |
| 143 |
C = NewType('C', B) |
| 144 |
|
| 145 |
class B(D): |
| 146 |
pass |
| 147 |
[builtins fixtures/list.pyi] |
| 148 |
|
| 149 |
[case testRecursiveAliasViaBaseClass3] |
| 150 |
from typing import List, Generic, TypeVar, NamedTuple |
| 151 |
T = TypeVar('T') |
| 152 |
|
| 153 |
class C(A, B): |
| 154 |
pass |
| 155 |
class G(Generic[T]): pass |
| 156 |
A = G[C] |
| 157 |
class B(NamedTuple): |
| 158 |
x: int |
| 159 |
|
| 160 |
y: C |
| 161 |
reveal_type(y.x) # N: Revealed type is "builtins.int" |
| 162 |
reveal_type(y[0]) # N: Revealed type is "builtins.int" |
| 163 |
x: A |
| 164 |
reveal_type(x) # N: Revealed type is "__main__.G[tuple[builtins.int, fallback=__main__.C]]" |
| 165 |
[builtins fixtures/list.pyi] |
| 166 |
|
| 167 |
[case testRecursiveAliasViaBaseClassImported] |
| 168 |
# flags: --disable-error-code used-before-def |
| 169 |
import a |
| 170 |
[file a.py] |
| 171 |
from typing import List |
| 172 |
from b import D |
| 173 |
|
| 174 |
def f(x: B) -> List[B]: ... |
| 175 |
B = List[C] |
| 176 |
class C(B): pass |
| 177 |
|
| 178 |
[file b.py] |
| 179 |
from a import f |
| 180 |
class D: ... |
| 181 |
reveal_type(f) # N: Revealed type is "def (x: builtins.list[a.C]) -> builtins.list[builtins.list[a.C]]" |
| 182 |
[builtins fixtures/list.pyi] |
| 183 |
|
| 184 |
[case testRecursiveAliasViaNamedTuple] |
| 185 |
from typing import List, NamedTuple, Union |
| 186 |
|
| 187 |
Exp = Union['A', 'B'] |
| 188 |
class A(NamedTuple('A', [('attr', List[Exp])])): pass |
| 189 |
class B(NamedTuple('B', [('val', object)])): pass |
| 190 |
|
| 191 |
def my_eval(exp: Exp) -> int: |
| 192 |
reveal_type(exp) # N: Revealed type is "tuple[builtins.list[...], fallback=__main__.A] | tuple[builtins.object, fallback=__main__.B]" |
| 193 |
if isinstance(exp, A): |
| 194 |
my_eval(exp[0][0]) |
| 195 |
return my_eval(exp.attr[0]) |
| 196 |
if isinstance(exp, B): |
| 197 |
return exp.val # E: Incompatible return value type (got "object", expected "int") |
| 198 |
return 0 |
| 199 |
|
| 200 |
my_eval(A([B(1), B(2)])) |
| 201 |
[builtins fixtures/isinstancelist.pyi] |
| 202 |
|
| 203 |
[case testRecursiveAliasesSimplifiedUnion] |
| 204 |
from typing import Sequence, TypeVar, Union |
| 205 |
|
| 206 |
class A: ... |
| 207 |
class B(A): ... |
| 208 |
|
| 209 |
NestedA = Sequence[Union[A, NestedA]] |
| 210 |
NestedB = Sequence[Union[B, NestedB]] |
| 211 |
a: NestedA |
| 212 |
b: NestedB |
| 213 |
|
| 214 |
T = TypeVar("T") |
| 215 |
S = TypeVar("S") |
| 216 |
def union(a: T, b: S) -> Union[T, S]: ... |
| 217 |
|
| 218 |
x: int |
| 219 |
y = union(a, b) |
| 220 |
x = y # E: Incompatible types in assignment (expression has type "Sequence[A | NestedA]", variable has type "int") |
| 221 |
[builtins fixtures/isinstancelist.pyi] |
| 222 |
|
| 223 |
[case testRecursiveAliasesJoins] |
| 224 |
from typing import Sequence, TypeVar, Union |
| 225 |
|
| 226 |
class A: ... |
| 227 |
class B(A): ... |
| 228 |
|
| 229 |
NestedA = Sequence[Union[A, NestedA]] |
| 230 |
NestedB = Sequence[Union[B, NestedB]] |
| 231 |
a: NestedA |
| 232 |
b: NestedB |
| 233 |
la: Sequence[Sequence[A]] |
| 234 |
lb: Sequence[Sequence[B]] |
| 235 |
|
| 236 |
T = TypeVar("T") |
| 237 |
def join(a: T, b: T) -> T: ... |
| 238 |
x: int |
| 239 |
|
| 240 |
y1 = join(a, b) |
| 241 |
x = y1 # E: Incompatible types in assignment (expression has type "Sequence[A | NestedA]", variable has type "int") |
| 242 |
y2 = join(a, lb) |
| 243 |
x = y2 # E: Incompatible types in assignment (expression has type "Sequence[A | NestedA]", variable has type "int") |
| 244 |
y3 = join(la, b) |
| 245 |
x = y3 # E: Incompatible types in assignment (expression has type "Sequence[Sequence[A] | B | NestedB]", variable has type "int") |
| 246 |
[builtins fixtures/isinstancelist.pyi] |
| 247 |
|
| 248 |
[case testRecursiveAliasesRestrictions] |
| 249 |
from typing import Sequence, Mapping, Union |
| 250 |
|
| 251 |
A = Sequence[Union[int, A]] |
| 252 |
B = Mapping[int, Union[int, B]] |
| 253 |
|
| 254 |
x: int |
| 255 |
y: Union[A, B] |
| 256 |
if isinstance(y, Sequence): |
| 257 |
x = y # E: Incompatible types in assignment (expression has type "Sequence[int | A]", variable has type "int") |
| 258 |
else: |
| 259 |
x = y # E: Incompatible types in assignment (expression has type "Mapping[int, int | B]", variable has type "int") |
| 260 |
[builtins fixtures/isinstancelist.pyi] |
| 261 |
|
| 262 |
[case testRecursiveAliasesRestrictions2] |
| 263 |
from typing import Sequence, Union |
| 264 |
|
| 265 |
class A: ... |
| 266 |
class B(A): ... |
| 267 |
|
| 268 |
NestedA = Sequence[Union[A, NestedA]] |
| 269 |
NestedB = Sequence[Union[B, NestedB]] |
| 270 |
|
| 271 |
a: NestedA |
| 272 |
b: NestedB |
| 273 |
aa: NestedA |
| 274 |
|
| 275 |
x: int |
| 276 |
x = a # E: Incompatible types in assignment (expression has type "NestedA", variable has type "int") |
| 277 |
a = b |
| 278 |
x = a # E: Incompatible types in assignment (expression has type "Sequence[B | NestedB]", variable has type "int") |
| 279 |
b = aa # E: Incompatible types in assignment (expression has type "NestedA", variable has type "NestedB") |
| 280 |
if isinstance(b[0], Sequence): |
| 281 |
a = b[0] |
| 282 |
x = a # E: Incompatible types in assignment (expression has type "Sequence[B | NestedB]", variable has type "int") |
| 283 |
[builtins fixtures/isinstancelist.pyi] |
| 284 |
|
| 285 |
[case testRecursiveAliasWithRecursiveInstance] |
| 286 |
from typing import Sequence, Union, TypeVar |
| 287 |
|
| 288 |
class A: ... |
| 289 |
T = TypeVar("T") |
| 290 |
Nested = Sequence[Union[T, Nested[T]]] |
| 291 |
class B(Sequence[B]): ... |
| 292 |
|
| 293 |
a: Nested[A] |
| 294 |
aa: Nested[A] |
| 295 |
b: B |
| 296 |
a = b # OK |
| 297 |
a = [[b]] # OK |
| 298 |
b = aa # E: Incompatible types in assignment (expression has type "Nested[A]", variable has type "B") |
| 299 |
|
| 300 |
def join(a: T, b: T) -> T: ... |
| 301 |
reveal_type(join(a, b)) # N: Revealed type is "typing.Sequence[__main__.A | typing.Sequence[__main__.A | ...]]" |
| 302 |
reveal_type(join(b, a)) # N: Revealed type is "typing.Sequence[__main__.A | typing.Sequence[__main__.A | ...]]" |
| 303 |
[builtins fixtures/isinstancelist.pyi] |
| 304 |
|
| 305 |
[case testRecursiveAliasWithRecursiveInstanceInference] |
| 306 |
from typing import Sequence, Union, TypeVar, List |
| 307 |
|
| 308 |
T = TypeVar("T") |
| 309 |
Nested = Sequence[Union[T, Nested[T]]] |
| 310 |
class B(Sequence[B]): ... |
| 311 |
|
| 312 |
nb: Nested[B] = [B(), [B(), [B()]]] |
| 313 |
lb: List[B] |
| 314 |
|
| 315 |
def foo(x: Nested[T]) -> T: ... |
| 316 |
reveal_type(foo(lb)) # N: Revealed type is "__main__.B" |
| 317 |
reveal_type(foo([B(), [B(), [B()]]])) # N: Revealed type is "__main__.B" |
| 318 |
|
| 319 |
NestedInv = List[Union[T, NestedInv[T]]] |
| 320 |
nib: NestedInv[B] = [B(), [B(), [B()]]] |
| 321 |
def bar(x: NestedInv[T]) -> T: ... |
| 322 |
reveal_type(bar(nib)) # N: Revealed type is "__main__.B" |
| 323 |
[builtins fixtures/isinstancelist.pyi] |
| 324 |
|
| 325 |
[case testRecursiveAliasTopUnion] |
| 326 |
from typing import Sequence, Union, TypeVar, List |
| 327 |
|
| 328 |
class A: ... |
| 329 |
class B(A): ... |
| 330 |
|
| 331 |
T = TypeVar("T") |
| 332 |
PlainNested = Union[T, Sequence[PlainNested[T]]] |
| 333 |
|
| 334 |
x: PlainNested[A] |
| 335 |
y: PlainNested[B] = [B(), [B(), [B()]]] |
| 336 |
x = y # OK |
| 337 |
|
| 338 |
xx: PlainNested[B] |
| 339 |
yy: PlainNested[A] |
| 340 |
xx = yy # E: Incompatible types in assignment (expression has type "PlainNested[A]", variable has type "PlainNested[B]") |
| 341 |
|
| 342 |
def foo(arg: PlainNested[T]) -> T: ... |
| 343 |
lb: List[B] |
| 344 |
reveal_type(foo([B(), [B(), [B()]]])) # N: Revealed type is "__main__.B" |
| 345 |
reveal_type(foo(lb)) # N: Revealed type is "__main__.B" |
| 346 |
reveal_type(foo(xx)) # N: Revealed type is "__main__.B" |
| 347 |
[builtins fixtures/isinstancelist.pyi] |
| 348 |
|
| 349 |
[case testRecursiveAliasInferenceExplicitNonRecursive] |
| 350 |
from typing import Sequence, Union, TypeVar, List |
| 351 |
|
| 352 |
T = TypeVar("T") |
| 353 |
Nested = Sequence[Union[T, Nested[T]]] |
| 354 |
PlainNested = Union[T, Sequence[PlainNested[T]]] |
| 355 |
|
| 356 |
def foo(x: Nested[T]) -> T: ... |
| 357 |
def bar(x: PlainNested[T]) -> T: ... |
| 358 |
|
| 359 |
class A: ... |
| 360 |
a: A |
| 361 |
la: List[A] |
| 362 |
lla: List[Union[A, List[A]]] |
| 363 |
llla: List[Union[A, List[Union[A, List[A]]]]] |
| 364 |
|
| 365 |
reveal_type(foo(la)) # N: Revealed type is "__main__.A" |
| 366 |
reveal_type(foo(lla)) # N: Revealed type is "__main__.A" |
| 367 |
reveal_type(foo(llla)) # N: Revealed type is "__main__.A" |
| 368 |
|
| 369 |
reveal_type(bar(a)) # N: Revealed type is "__main__.A" |
| 370 |
reveal_type(bar(la)) # N: Revealed type is "__main__.A" |
| 371 |
reveal_type(bar(lla)) # N: Revealed type is "__main__.A" |
| 372 |
reveal_type(bar(llla)) # N: Revealed type is "__main__.A" |
| 373 |
[builtins fixtures/isinstancelist.pyi] |
| 374 |
|
| 375 |
[case testRecursiveAliasesWithOptional] |
| 376 |
from typing import Optional, Sequence |
| 377 |
|
| 378 |
A = Sequence[Optional[A]] |
| 379 |
x: A |
| 380 |
y: str = x[0] # E: Incompatible types in assignment (expression has type "A | None", variable has type "str") |
| 381 |
|
| 382 |
[case testRecursiveAliasesProhibitBadAliases] |
| 383 |
# flags: --disable-error-code used-before-def |
| 384 |
from typing import Union, Type, List, TypeVar |
| 385 |
|
| 386 |
NR = List[int] |
| 387 |
NR2 = Union[NR, NR] |
| 388 |
NR3 = Union[NR, Union[NR2, NR2]] |
| 389 |
|
| 390 |
T = TypeVar("T") |
| 391 |
NRG = Union[int, T] |
| 392 |
NR4 = NRG[str] |
| 393 |
NR5 = Union[NRG[int], NR4] |
| 394 |
|
| 395 |
A = Union[B, int] # E: Invalid recursive alias: a union item of itself |
| 396 |
B = Union[int, A] # Error reported above |
| 397 |
def f() -> A: ... |
| 398 |
reveal_type(f()) # N: Revealed type is "Any" |
| 399 |
|
| 400 |
G = Union[T, G[T]] # E: Invalid recursive alias: a union item of itself |
| 401 |
GL = Union[T, GL[List[T]]] # E: Invalid recursive alias: a union item of itself \ |
| 402 |
# E: Invalid recursive alias: type variable nesting on right hand side |
| 403 |
def g() -> G[int]: ... |
| 404 |
reveal_type(g()) # N: Revealed type is "Any" |
| 405 |
|
| 406 |
def local() -> None: |
| 407 |
L = List[Union[int, L]] # E: Cannot resolve name "L" (possible cyclic definition) \ |
| 408 |
# N: Recursive types are not allowed at function scope |
| 409 |
x: L |
| 410 |
reveal_type(x) # N: Revealed type is "builtins.list[builtins.int | Any]" |
| 411 |
|
| 412 |
S = Type[S] # E: Type[...] can't contain "Type[...]" |
| 413 |
U = Type[Union[int, U]] # E: Type[...] can't contain "Union[Type[...], Type[...]]" \ |
| 414 |
# E: Type[...] can't contain "Type[...]" |
| 415 |
x: U |
| 416 |
reveal_type(x) # N: Revealed type is "type[Any]" |
| 417 |
|
| 418 |
D = List[F[List[T]]] # E: Invalid recursive alias: type variable nesting on right hand side |
| 419 |
F = D[T] # Error reported above |
| 420 |
E = List[E[E[T]]] # E: Invalid recursive alias: type variable nesting on right hand side |
| 421 |
d: D |
| 422 |
reveal_type(d) # N: Revealed type is "Any" |
| 423 |
[builtins fixtures/isinstancelist.pyi] |
| 424 |
|
| 425 |
[case testBasicRecursiveNamedTuple] |
| 426 |
from typing import NamedTuple, Optional |
| 427 |
|
| 428 |
NT = NamedTuple("NT", [("x", Optional[NT]), ("y", int)]) |
| 429 |
nt: NT |
| 430 |
reveal_type(nt) # N: Revealed type is "tuple[... | None, builtins.int, fallback=__main__.NT]" |
| 431 |
reveal_type(nt.x) # N: Revealed type is "tuple[... | None, builtins.int, fallback=__main__.NT] | None" |
| 432 |
reveal_type(nt[0]) # N: Revealed type is "tuple[... | None, builtins.int, fallback=__main__.NT] | None" |
| 433 |
y: str |
| 434 |
if nt.x is not None: |
| 435 |
y = nt.x[0] # E: Incompatible types in assignment (expression has type "NT | None", variable has type "str") |
| 436 |
[builtins fixtures/tuple.pyi] |
| 437 |
|
| 438 |
[case testBasicRecursiveNamedTupleSpecial] |
| 439 |
from typing import NamedTuple, TypeVar, Tuple |
| 440 |
|
| 441 |
NT = NamedTuple("NT", [("x", NT), ("y", int)]) |
| 442 |
nt: NT |
| 443 |
reveal_type(nt) # N: Revealed type is "tuple[..., builtins.int, fallback=__main__.NT]" |
| 444 |
reveal_type(nt.x) # N: Revealed type is "tuple[..., builtins.int, fallback=__main__.NT]" |
| 445 |
reveal_type(nt[0]) # N: Revealed type is "tuple[tuple[..., builtins.int, fallback=__main__.NT], builtins.int, fallback=__main__.NT]" |
| 446 |
y: str |
| 447 |
if nt.x is not None: |
| 448 |
y = nt.x[0] # E: Incompatible types in assignment (expression has type "NT", variable has type "str") |
| 449 |
|
| 450 |
T = TypeVar("T") |
| 451 |
def f(a: T, b: T) -> T: ... |
| 452 |
tnt: Tuple[NT] |
| 453 |
|
| 454 |
# TODO: these should be tuple[object] instead. |
| 455 |
reveal_type(f(nt, tnt)) # N: Revealed type is "builtins.tuple[Any, ...]" |
| 456 |
reveal_type(f(tnt, nt)) # N: Revealed type is "builtins.tuple[Any, ...]" |
| 457 |
[builtins fixtures/tuple.pyi] |
| 458 |
|
| 459 |
[case testBasicRecursiveNamedTupleClass] |
| 460 |
from typing import NamedTuple, Optional |
| 461 |
|
| 462 |
class NT(NamedTuple): |
| 463 |
x: Optional[NT] |
| 464 |
y: int |
| 465 |
|
| 466 |
nt: NT |
| 467 |
reveal_type(nt) # N: Revealed type is "tuple[... | None, builtins.int, fallback=__main__.NT]" |
| 468 |
reveal_type(nt.x) # N: Revealed type is "tuple[... | None, builtins.int, fallback=__main__.NT] | None" |
| 469 |
reveal_type(nt[0]) # N: Revealed type is "tuple[... | None, builtins.int, fallback=__main__.NT] | None" |
| 470 |
y: str |
| 471 |
if nt.x is not None: |
| 472 |
y = nt.x[0] # E: Incompatible types in assignment (expression has type "NT | None", variable has type "str") |
| 473 |
[builtins fixtures/tuple.pyi] |
| 474 |
|
| 475 |
[case testRecursiveRegularTupleClass] |
| 476 |
from typing import Tuple |
| 477 |
|
| 478 |
x: B |
| 479 |
class B(Tuple[B, int]): |
| 480 |
x: int |
| 481 |
|
| 482 |
b, _ = x |
| 483 |
reveal_type(b.x) # N: Revealed type is "builtins.int" |
| 484 |
[builtins fixtures/tuple.pyi] |
| 485 |
|
| 486 |
[case testRecursiveTupleClassesNewType] |
| 487 |
from typing import Tuple, NamedTuple, NewType |
| 488 |
|
| 489 |
x: C |
| 490 |
class B(Tuple[B, int]): |
| 491 |
x: int |
| 492 |
C = NewType("C", B) |
| 493 |
b, _ = x |
| 494 |
reveal_type(b) # N: Revealed type is "tuple[..., builtins.int, fallback=__main__.B]" |
| 495 |
reveal_type(b.x) # N: Revealed type is "builtins.int" |
| 496 |
|
| 497 |
y: CNT |
| 498 |
class BNT(NamedTuple): |
| 499 |
x: CNT |
| 500 |
y: int |
| 501 |
CNT = NewType("CNT", BNT) |
| 502 |
bnt, _ = y |
| 503 |
reveal_type(bnt.y) # N: Revealed type is "builtins.int" |
| 504 |
[builtins fixtures/tuple.pyi] |
| 505 |
|
| 506 |
-- Tests duplicating some existing named tuple tests with recursive aliases enabled |
| 507 |
|
| 508 |
[case testMutuallyRecursiveNamedTuples] |
| 509 |
# flags: --disable-error-code used-before-def |
| 510 |
|
| 511 |
from typing import Tuple, NamedTuple, TypeVar, Union |
| 512 |
|
| 513 |
A = NamedTuple('A', [('x', str), ('y', Tuple[B, ...])]) |
| 514 |
class B(NamedTuple): |
| 515 |
x: A |
| 516 |
y: int |
| 517 |
|
| 518 |
n: A |
| 519 |
reveal_type(n) # N: Revealed type is "tuple[builtins.str, builtins.tuple[tuple[..., builtins.int, fallback=__main__.B], ...], fallback=__main__.A]" |
| 520 |
|
| 521 |
T = TypeVar("T") |
| 522 |
S = TypeVar("S") |
| 523 |
def foo(arg: Tuple[T, S]) -> Union[T, S]: ... |
| 524 |
x = foo(n) |
| 525 |
y: str = x # E: Incompatible types in assignment (expression has type "str | tuple[B, ...]", variable has type "str") |
| 526 |
[builtins fixtures/tuple.pyi] |
| 527 |
|
| 528 |
[case testMutuallyRecursiveNamedTuplesJoin] |
| 529 |
from typing import NamedTuple, Tuple |
| 530 |
|
| 531 |
class B(NamedTuple): |
| 532 |
x: Tuple[A, int] |
| 533 |
y: int |
| 534 |
|
| 535 |
A = NamedTuple('A', [('x', str), ('y', B)]) |
| 536 |
n: B |
| 537 |
m: A |
| 538 |
s: str = n.x # E: Incompatible types in assignment (expression has type "tuple[A, int]", variable has type "str") |
| 539 |
reveal_type(m[0]) # N: Revealed type is "builtins.str" |
| 540 |
lst = [m, n] |
| 541 |
|
| 542 |
# Unfortunately, join of two recursive types is not very precise. |
| 543 |
reveal_type(lst[0]) # N: Revealed type is "builtins.object" |
| 544 |
|
| 545 |
# These just should not crash |
| 546 |
lst1 = [m] |
| 547 |
lst2 = [m, m] |
| 548 |
lst3 = [m, m, m] |
| 549 |
[builtins fixtures/tuple.pyi] |
| 550 |
|
| 551 |
[case testMutuallyRecursiveNamedTuplesClasses] |
| 552 |
from typing import NamedTuple, Tuple |
| 553 |
|
| 554 |
class B(NamedTuple): |
| 555 |
x: A |
| 556 |
y: int |
| 557 |
class A(NamedTuple): |
| 558 |
x: str |
| 559 |
y: B |
| 560 |
|
| 561 |
n: A |
| 562 |
s: str = n.y[0] # E: Incompatible types in assignment (expression has type "A", variable has type "str") |
| 563 |
|
| 564 |
m: B |
| 565 |
n = m.x |
| 566 |
n = n.y.x |
| 567 |
|
| 568 |
t: Tuple[str, B] |
| 569 |
t = n |
| 570 |
t = m # E: Incompatible types in assignment (expression has type "B", variable has type "tuple[str, B]") |
| 571 |
[builtins fixtures/tuple.pyi] |
| 572 |
|
| 573 |
[case testMutuallyRecursiveNamedTuplesCalls] |
| 574 |
# flags: --disable-error-code used-before-def |
| 575 |
from typing import NamedTuple |
| 576 |
|
| 577 |
B = NamedTuple('B', [('x', A), ('y', int)]) |
| 578 |
A = NamedTuple('A', [('x', str), ('y', 'B')]) |
| 579 |
n: A |
| 580 |
def f(m: B) -> None: pass |
| 581 |
reveal_type(n) # N: Revealed type is "tuple[builtins.str, tuple[..., builtins.int, fallback=__main__.B], fallback=__main__.A]" |
| 582 |
reveal_type(f) # N: Revealed type is "def (m: tuple[tuple[builtins.str, ..., fallback=__main__.A], builtins.int, fallback=__main__.B])" |
| 583 |
f(n) # E: Argument 1 to "f" has incompatible type "A"; expected "B" |
| 584 |
[builtins fixtures/tuple.pyi] |
| 585 |
|
| 586 |
[case testNoRecursiveTuplesAtFunctionScope] |
| 587 |
from typing import NamedTuple, Tuple |
| 588 |
def foo() -> None: |
| 589 |
class B(NamedTuple): |
| 590 |
x: B # E: Cannot resolve name "B" (possible cyclic definition) \ |
| 591 |
# N: Recursive types are not allowed at function scope |
| 592 |
y: int |
| 593 |
b: B |
| 594 |
reveal_type(b) # N: Revealed type is "tuple[Any, builtins.int, fallback=__main__.B@3]" |
| 595 |
[builtins fixtures/tuple.pyi] |
| 596 |
|
| 597 |
[case testBasicRecursiveGenericNamedTuple] |
| 598 |
from typing import Generic, NamedTuple, TypeVar, Union |
| 599 |
|
| 600 |
T = TypeVar("T", covariant=True) |
| 601 |
class NT(NamedTuple, Generic[T]): |
| 602 |
key: int |
| 603 |
value: Union[T, NT[T]] |
| 604 |
|
| 605 |
class A: ... |
| 606 |
class B(A): ... |
| 607 |
|
| 608 |
nti: NT[int] = NT(key=0, value=NT(key=1, value=A())) # E: Argument "value" to "NT" has incompatible type "A"; expected "int | NT[int]" |
| 609 |
reveal_type(nti) # N: Revealed type is "tuple[builtins.int, builtins.int | ..., fallback=__main__.NT[builtins.int]]" |
| 610 |
|
| 611 |
nta: NT[A] |
| 612 |
ntb: NT[B] |
| 613 |
nta = ntb # OK, covariance |
| 614 |
ntb = nti # E: Incompatible types in assignment (expression has type "NT[int]", variable has type "NT[B]") |
| 615 |
|
| 616 |
def last(arg: NT[T]) -> T: ... |
| 617 |
reveal_type(last(ntb)) # N: Revealed type is "__main__.B" |
| 618 |
[builtins fixtures/tuple.pyi] |
| 619 |
|
| 620 |
[case testBasicRecursiveTypedDictClass] |
| 621 |
from typing import TypedDict |
| 622 |
|
| 623 |
class TD(TypedDict): |
| 624 |
x: int |
| 625 |
y: TD |
| 626 |
|
| 627 |
td: TD |
| 628 |
reveal_type(td) # N: Revealed type is "TypedDict('__main__.TD', {'x': builtins.int, 'y': ...})" |
| 629 |
s: str = td["y"] # E: Incompatible types in assignment (expression has type "TD", variable has type "str") |
| 630 |
[builtins fixtures/dict.pyi] |
| 631 |
[typing fixtures/typing-typeddict.pyi] |
| 632 |
|
| 633 |
[case testBasicRecursiveTypedDictCall] |
| 634 |
from typing import TypedDict |
| 635 |
|
| 636 |
TD = TypedDict("TD", {"x": int, "y": TD}) |
| 637 |
td: TD |
| 638 |
reveal_type(td) # N: Revealed type is "TypedDict('__main__.TD', {'x': builtins.int, 'y': ...})" |
| 639 |
|
| 640 |
TD2 = TypedDict("TD2", {"x": int, "y": TD2}) |
| 641 |
td2: TD2 |
| 642 |
TD3 = TypedDict("TD3", {"x": str, "y": TD3}) |
| 643 |
td3: TD3 |
| 644 |
|
| 645 |
td = td2 |
| 646 |
td = td3 # E: Incompatible types in assignment (expression has type "TD3", variable has type "TD") |
| 647 |
[builtins fixtures/dict.pyi] |
| 648 |
[typing fixtures/typing-typeddict.pyi] |
| 649 |
|
| 650 |
[case testBasicRecursiveTypedDictExtending] |
| 651 |
from typing import TypedDict |
| 652 |
|
| 653 |
class TDA(TypedDict): |
| 654 |
xa: int |
| 655 |
ya: TD |
| 656 |
|
| 657 |
class TDB(TypedDict): |
| 658 |
xb: int |
| 659 |
yb: TD |
| 660 |
|
| 661 |
class TD(TDA, TDB): |
| 662 |
a: TDA |
| 663 |
b: TDB |
| 664 |
|
| 665 |
td: TD |
| 666 |
reveal_type(td) # N: Revealed type is "TypedDict('__main__.TD', {'xb': builtins.int, 'yb': ..., 'xa': builtins.int, 'ya': ..., 'a': TypedDict('__main__.TDA', {'xa': builtins.int, 'ya': ...}), 'b': TypedDict('__main__.TDB', {'xb': builtins.int, 'yb': ...})})" |
| 667 |
[builtins fixtures/dict.pyi] |
| 668 |
[typing fixtures/typing-typeddict.pyi] |
| 669 |
|
| 670 |
[case testRecursiveTypedDictCreation] |
| 671 |
from typing import TypedDict, Optional |
| 672 |
|
| 673 |
class TD(TypedDict): |
| 674 |
x: int |
| 675 |
y: Optional[TD] |
| 676 |
|
| 677 |
td: TD = {"x": 0, "y": None} |
| 678 |
td2: TD = {"x": 0, "y": {"x": 1, "y": {"x": 2, "y": None}}} |
| 679 |
|
| 680 |
itd = TD(x=0, y=None) |
| 681 |
itd2 = TD(x=0, y=TD(x=0, y=TD(x=0, y=None))) |
| 682 |
[builtins fixtures/dict.pyi] |
| 683 |
[typing fixtures/typing-typeddict.pyi] |
| 684 |
|
| 685 |
[case testRecursiveTypedDictMethods] |
| 686 |
from typing import TypedDict |
| 687 |
|
| 688 |
class TD(TypedDict, total=False): |
| 689 |
x: int |
| 690 |
y: TD |
| 691 |
|
| 692 |
td: TD |
| 693 |
reveal_type(td.get("y")) # N: Revealed type is "TypedDict('__main__.TD', {'x'?: builtins.int, 'y'?: ...}) | None" |
| 694 |
td["y"] = {"x": 0, "y": {}} |
| 695 |
td["y"] = {"x": 0, "y": {"x": 0, "y": 42}} # E: Incompatible types (expression has type "int", TypedDict item "y" has type "TD") |
| 696 |
|
| 697 |
reveal_type(td.get("y")) # N: Revealed type is "TypedDict('__main__.TD', {'x'?: builtins.int, 'y'?: ...}) | None" |
| 698 |
s: str = td.get("y") # E: Incompatible types in assignment (expression has type "TD | None", variable has type "str") |
| 699 |
|
| 700 |
td.update({"x": 0, "y": {"x": 1, "y": {}}}) |
| 701 |
td.update({"x": 0, "y": {"x": 1, "y": {"x": 2, "y": 42}}}) # E: Incompatible types (expression has type "int", TypedDict item "y" has type "TD") |
| 702 |
[builtins fixtures/dict.pyi] |
| 703 |
[typing fixtures/typing-typeddict.pyi] |
| 704 |
|
| 705 |
[case testRecursiveTypedDictSubtyping] |
| 706 |
from typing import TypedDict |
| 707 |
|
| 708 |
class TDA1(TypedDict): |
| 709 |
x: int |
| 710 |
y: TDA1 |
| 711 |
class TDA2(TypedDict): |
| 712 |
x: int |
| 713 |
y: TDA2 |
| 714 |
class TDB(TypedDict): |
| 715 |
x: str |
| 716 |
y: TDB |
| 717 |
|
| 718 |
tda1: TDA1 |
| 719 |
tda2: TDA2 |
| 720 |
tdb: TDB |
| 721 |
def fa1(arg: TDA1) -> None: ... |
| 722 |
def fa2(arg: TDA2) -> None: ... |
| 723 |
def fb(arg: TDB) -> None: ... |
| 724 |
|
| 725 |
fa1(tda2) |
| 726 |
fa2(tda1) |
| 727 |
fb(tda1) # E: Argument 1 to "fb" has incompatible type "TDA1"; expected "TDB" |
| 728 |
[builtins fixtures/dict.pyi] |
| 729 |
[typing fixtures/typing-typeddict.pyi] |
| 730 |
|
| 731 |
[case testRecursiveTypedDictJoin] |
| 732 |
from typing import TypedDict, TypeVar |
| 733 |
|
| 734 |
class TDA1(TypedDict): |
| 735 |
x: int |
| 736 |
y: TDA1 |
| 737 |
class TDA2(TypedDict): |
| 738 |
x: int |
| 739 |
y: TDA2 |
| 740 |
class TDB(TypedDict): |
| 741 |
x: str |
| 742 |
y: TDB |
| 743 |
|
| 744 |
tda1: TDA1 |
| 745 |
tda2: TDA2 |
| 746 |
tdb: TDB |
| 747 |
|
| 748 |
T = TypeVar("T") |
| 749 |
def f(x: T, y: T) -> T: ... |
| 750 |
# Join for recursive types is very basic, but just add tests that we don't crash. |
| 751 |
reveal_type(f(tda1, tda2)) # N: Revealed type is "TypedDict({'x': builtins.int, 'y': TypedDict('__main__.TDA2', {'x': builtins.int, 'y': ...})})" |
| 752 |
reveal_type(f(tda1, tdb)) # N: Revealed type is "TypedDict({'x'=: builtins.object, 'y'=: builtins.object})" |
| 753 |
[builtins fixtures/dict.pyi] |
| 754 |
[typing fixtures/typing-typeddict.pyi] |
| 755 |
|
| 756 |
[case testBasicRecursiveGenericTypedDict] |
| 757 |
from typing import TypedDict, TypeVar, Generic, Optional, List |
| 758 |
|
| 759 |
T = TypeVar("T") |
| 760 |
class Tree(TypedDict, Generic[T], total=False): |
| 761 |
value: T |
| 762 |
left: Tree[T] |
| 763 |
right: Tree[T] |
| 764 |
|
| 765 |
def collect(arg: Tree[T]) -> List[T]: ... |
| 766 |
|
| 767 |
reveal_type(collect({"left": {"right": {"value": 0}}})) # N: Revealed type is "builtins.list[builtins.int]" |
| 768 |
[builtins fixtures/dict.pyi] |
| 769 |
[typing fixtures/typing-typeddict.pyi] |
| 770 |
|
| 771 |
[case testRecursiveGenericTypedDictExtending] |
| 772 |
from typing import TypedDict, Generic, TypeVar, List |
| 773 |
|
| 774 |
T = TypeVar("T") |
| 775 |
|
| 776 |
class TD(TypedDict, Generic[T]): |
| 777 |
val: T |
| 778 |
other: STD[T] |
| 779 |
class STD(TD[T]): |
| 780 |
sval: T |
| 781 |
one: TD[T] |
| 782 |
|
| 783 |
std: STD[str] |
| 784 |
reveal_type(std) # N: Revealed type is "TypedDict('__main__.STD', {'val': builtins.str, 'other': ..., 'sval': builtins.str, 'one': TypedDict('__main__.TD', {'val': builtins.str, 'other': ...})})" |
| 785 |
[builtins fixtures/dict.pyi] |
| 786 |
[typing fixtures/typing-typeddict.pyi] |
| 787 |
|
| 788 |
[case testRecursiveClassLevelAlias] |
| 789 |
from typing import Union, Sequence |
| 790 |
|
| 791 |
class A: |
| 792 |
Children = Union[Sequence['Children'], 'A', None] |
| 793 |
x: A.Children |
| 794 |
reveal_type(x) # N: Revealed type is "typing.Sequence[...] | __main__.A | None" |
| 795 |
|
| 796 |
class B: |
| 797 |
Foo = Sequence[Bar] |
| 798 |
Bar = Sequence[Foo] |
| 799 |
y: B.Foo |
| 800 |
reveal_type(y) # N: Revealed type is "typing.Sequence[typing.Sequence[...]]" |
| 801 |
[builtins fixtures/tuple.pyi] |
| 802 |
|
| 803 |
[case testNoCrashOnRecursiveTupleFallback] |
| 804 |
from typing import Union, Tuple |
| 805 |
|
| 806 |
Tree1 = Union[str, Tuple[Tree1]] |
| 807 |
Tree2 = Union[str, Tuple[Tree2, Tree2]] |
| 808 |
Tree3 = Union[str, Tuple[Tree3, Tree3, Tree3]] |
| 809 |
|
| 810 |
def test1() -> Tree1: |
| 811 |
return 42 # E: Incompatible return value type (got "int", expected "str | tuple[Tree1]") |
| 812 |
def test2() -> Tree2: |
| 813 |
return 42 # E: Incompatible return value type (got "int", expected "str | tuple[Tree2, Tree2]") |
| 814 |
def test3() -> Tree3: |
| 815 |
return 42 # E: Incompatible return value type (got "int", expected "str | tuple[Tree3, Tree3, Tree3]") |
| 816 |
[builtins fixtures/tuple.pyi] |
| 817 |
|
| 818 |
[case testRecursiveDoubleUnionNoCrash] |
| 819 |
from typing import Tuple, Union, Callable, Sequence |
| 820 |
|
| 821 |
K = Union[int, Tuple[Union[int, K]]] |
| 822 |
L = Union[int, Callable[[], Union[int, L]]] |
| 823 |
M = Union[int, Sequence[Union[int, M]]] |
| 824 |
|
| 825 |
x: K |
| 826 |
x = x |
| 827 |
y: L |
| 828 |
y = y |
| 829 |
z: M |
| 830 |
z = z |
| 831 |
|
| 832 |
x = y # E: Incompatible types in assignment (expression has type "L", variable has type "K") |
| 833 |
z = x # OK |
| 834 |
[builtins fixtures/tuple.pyi] |
| 835 |
|
| 836 |
[case testRecursiveInstanceInferenceNoCrash] |
| 837 |
from typing import Sequence, TypeVar, Union |
| 838 |
|
| 839 |
class C(Sequence[C]): ... |
| 840 |
|
| 841 |
T = TypeVar("T") |
| 842 |
def foo(x: T) -> C: ... |
| 843 |
|
| 844 |
Nested = Union[C, Sequence[Nested]] |
| 845 |
x: Nested = foo(42) |
| 846 |
|
| 847 |
[case testNoRecursiveExpandInstanceUnionCrash] |
| 848 |
from typing import List, Union |
| 849 |
|
| 850 |
class Tag(List[Union[Tag, List[Tag]]]): ... |
| 851 |
Tag() |
| 852 |
|
| 853 |
[case testNoRecursiveExpandInstanceUnionCrashGeneric] |
| 854 |
from typing import Generic, Iterable, TypeVar, Union |
| 855 |
|
| 856 |
ValueT = TypeVar("ValueT") |
| 857 |
class Recursive(Iterable[Union[ValueT, Recursive[ValueT]]]): |
| 858 |
pass |
| 859 |
|
| 860 |
class Base(Generic[ValueT]): |
| 861 |
def __init__(self, element: ValueT): |
| 862 |
pass |
| 863 |
class Sub(Base[Union[ValueT, Recursive[ValueT]]]): |
| 864 |
pass |
| 865 |
|
| 866 |
x: Iterable[str] |
| 867 |
reveal_type(Sub) # N: Revealed type is "def [ValueT] (element: ValueT`1 | __main__.Recursive[ValueT`1]) -> __main__.Sub[ValueT`1]" |
| 868 |
reveal_type(Sub(x)) # N: Revealed type is "__main__.Sub[typing.Iterable[builtins.str]]" |
| 869 |
|
| 870 |
[case testNoRecursiveExpandInstanceUnionCrashInference] |
| 871 |
# flags: --disable-error-code used-before-def |
| 872 |
from typing import TypeVar, Union, Generic, List |
| 873 |
|
| 874 |
T = TypeVar("T") |
| 875 |
InList = Union[T, InListRecurse[T]] |
| 876 |
class InListRecurse(Generic[T], List[InList[T]]): ... |
| 877 |
|
| 878 |
def list_thing(transforming: InList[T]) -> T: |
| 879 |
... |
| 880 |
reveal_type(list_thing([5])) # N: Revealed type is "builtins.list[builtins.int]" |
| 881 |
|
| 882 |
[case testRecursiveTypedDictWithList] |
| 883 |
from typing import List, TypedDict |
| 884 |
|
| 885 |
Example = TypedDict("Example", {"rec": List["Example"]}) |
| 886 |
e: Example |
| 887 |
reveal_type(e) # N: Revealed type is "TypedDict('__main__.Example', {'rec': builtins.list[...]})" |
| 888 |
[builtins fixtures/dict.pyi] |
| 889 |
[typing fixtures/typing-typeddict.pyi] |
| 890 |
|
| 891 |
[case testRecursiveNamedTupleWithList] |
| 892 |
from typing import List, NamedTuple |
| 893 |
|
| 894 |
Example = NamedTuple("Example", [("rec", List["Example"])]) |
| 895 |
e: Example |
| 896 |
reveal_type(e) # N: Revealed type is "tuple[builtins.list[...], fallback=__main__.Example]" |
| 897 |
[builtins fixtures/tuple.pyi] |
| 898 |
|
| 899 |
[case testRecursiveBoundFunctionScopeNoCrash] |
| 900 |
from typing import TypeVar, Union, Dict |
| 901 |
|
| 902 |
def dummy() -> None: |
| 903 |
A = Union[str, Dict[str, "A"]] # E: Cannot resolve name "A" (possible cyclic definition) \ |
| 904 |
# N: Recursive types are not allowed at function scope |
| 905 |
T = TypeVar("T", bound=A) |
| 906 |
|
| 907 |
def bar(x: T) -> T: |
| 908 |
pass |
| 909 |
reveal_type(bar) # N: Revealed type is "def [T <: builtins.str | builtins.dict[builtins.str, Any]] (x: T`-1) -> T`-1" |
| 910 |
[builtins fixtures/dict.pyi] |
| 911 |
|
| 912 |
[case testForwardBoundFunctionScopeWorks] |
| 913 |
from typing import TypeVar, Dict |
| 914 |
|
| 915 |
def dummy() -> None: |
| 916 |
A = Dict[str, "B"] |
| 917 |
B = Dict[str, str] |
| 918 |
T = TypeVar("T", bound=A) |
| 919 |
|
| 920 |
def bar(x: T) -> T: |
| 921 |
pass |
| 922 |
reveal_type(bar) # N: Revealed type is "def [T <: builtins.dict[builtins.str, builtins.dict[builtins.str, builtins.str]]] (x: T`-1) -> T`-1" |
| 923 |
[builtins fixtures/dict.pyi] |
| 924 |
|
| 925 |
[case testAliasRecursiveUnpackMultiple] |
| 926 |
from typing import Tuple, TypeVar, Optional |
| 927 |
|
| 928 |
T = TypeVar("T") |
| 929 |
S = TypeVar("S") |
| 930 |
|
| 931 |
A = Tuple[T, S, Optional[A[T, S]]] |
| 932 |
x: A[int, str] |
| 933 |
|
| 934 |
*_, last = x |
| 935 |
if last is not None: |
| 936 |
reveal_type(last) # N: Revealed type is "tuple[builtins.int, builtins.str, tuple[builtins.int, builtins.str, ... | None] | None]" |
| 937 |
[builtins fixtures/tuple.pyi] |
| 938 |
|
| 939 |
[case testRecursiveAliasLiteral] |
| 940 |
from typing import Literal, Tuple |
| 941 |
|
| 942 |
NotFilter = Tuple[Literal["not"], "NotFilter"] |
| 943 |
n: NotFilter |
| 944 |
reveal_type(n[1][1][0]) # N: Revealed type is "Literal['not']" |
| 945 |
[builtins fixtures/tuple.pyi] |
| 946 |
|
| 947 |
[case testNoCrashOnRecursiveAliasWithNone] |
| 948 |
# flags: --strict-optional |
| 949 |
from typing import Union, Generic, TypeVar, Optional |
| 950 |
|
| 951 |
T = TypeVar("T") |
| 952 |
class A(Generic[T]): ... |
| 953 |
class B(Generic[T]): ... |
| 954 |
|
| 955 |
Z = Union[A[Z], B[Optional[Z]]] |
| 956 |
X = Union[A[Optional[X]], B[Optional[X]]] |
| 957 |
|
| 958 |
z: Z |
| 959 |
x: X |
| 960 |
reveal_type(z) # N: Revealed type is "__main__.A[...] | __main__.B[... | None]" |
| 961 |
reveal_type(x) # N: Revealed type is "__main__.A[... | None] | __main__.B[... | None]" |
| 962 |
|
| 963 |
[case testRecursiveTupleFallback1] |
| 964 |
from typing import NewType, Tuple, Union |
| 965 |
|
| 966 |
T1 = NewType("T1", str) |
| 967 |
T2 = Tuple[T1, "T4", "T4"] |
| 968 |
T3 = Tuple[str, "T4", "T4"] |
| 969 |
T4 = Union[T2, T3] |
| 970 |
[builtins fixtures/tuple.pyi] |
| 971 |
|
| 972 |
[case testRecursiveTupleFallback2] |
| 973 |
from typing import NewType, Tuple, Union |
| 974 |
|
| 975 |
T1 = NewType("T1", str) |
| 976 |
class T2(Tuple[T1, "T4", "T4"]): ... |
| 977 |
T3 = Tuple[str, "T4", "T4"] |
| 978 |
T4 = Union[T2, T3] |
| 979 |
[builtins fixtures/tuple.pyi] |
| 980 |
|
| 981 |
[case testRecursiveTupleFallback3] |
| 982 |
from typing import NewType, Tuple, Union |
| 983 |
|
| 984 |
T1 = NewType("T1", str) |
| 985 |
T2 = Tuple[T1, "T4", "T4"] |
| 986 |
class T3(Tuple[str, "T4", "T4"]): ... |
| 987 |
T4 = Union[T2, T3] |
| 988 |
[builtins fixtures/tuple.pyi] |
| 989 |
|
| 990 |
[case testRecursiveTupleFallback4] |
| 991 |
from typing import NewType, Tuple, Union |
| 992 |
|
| 993 |
T1 = NewType("T1", str) |
| 994 |
class T2(Tuple[T1, "T4", "T4"]): ... |
| 995 |
class T3(Tuple[str, "T4", "T4"]): ... |
| 996 |
T4 = Union[T2, T3] |
| 997 |
[builtins fixtures/tuple.pyi] |
| 998 |
|
| 999 |
[case testRecursiveTupleFallback5] |
| 1000 |
from typing import Protocol, Tuple, Union |
| 1001 |
|
| 1002 |
class Proto(Protocol): |
| 1003 |
def __len__(self) -> int: ... |
| 1004 |
|
| 1005 |
A = Union[Proto, Tuple[A]] |
| 1006 |
ta: Tuple[A] |
| 1007 |
p: Proto |
| 1008 |
p = ta |
| 1009 |
[builtins fixtures/tuple.pyi] |
| 1010 |
|
| 1011 |
[case testRecursiveAliasesWithAnyUnimported] |
| 1012 |
# flags: --disallow-any-unimported |
| 1013 |
from typing import Callable |
| 1014 |
from bogus import Foo # type: ignore |
| 1015 |
|
| 1016 |
A = Callable[[Foo, "B"], Foo] # E: Type alias target becomes "Callable[[Any, B], Any]" due to an unfollowed import |
| 1017 |
B = Callable[[Foo, A], Foo] # E: Type alias target becomes "Callable[[Any, A], Any]" due to an unfollowed import |
| 1018 |
|
| 1019 |
[case testRecursiveAliasOnArgumentDetected] |
| 1020 |
from typing import TypeVar |
| 1021 |
|
| 1022 |
T = TypeVar("T") |
| 1023 |
L = list[T] |
| 1024 |
|
| 1025 |
A = L[A] |
| 1026 |
a: A = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "A") |
| 1027 |
|
| 1028 |
[case testRecursiveAliasInstanceOverlapCheck] |
| 1029 |
# flags: --warn-unreachable |
| 1030 |
from typing_extensions import TypeAlias |
| 1031 |
|
| 1032 |
OneClass: TypeAlias = 'list[OneClass]' |
| 1033 |
|
| 1034 |
class TwoClass(list['TwoClass']): |
| 1035 |
pass |
| 1036 |
|
| 1037 |
def f(obj: OneClass) -> None: |
| 1038 |
if isinstance(obj, TwoClass): |
| 1039 |
reveal_type(obj) # N: Revealed type is "__main__.TwoClass" |
| 1040 |
else: |
| 1041 |
reveal_type(obj) # N: Revealed type is "builtins.list[...]" |
| 1042 |
[builtins fixtures/isinstancelist.pyi] |