| 1 |
[case testListTypeAlias] |
| 2 |
from typing import List |
| 3 |
def f() -> List[int]: pass |
| 4 |
[builtins fixtures/list.pyi] |
| 5 |
[out] |
| 6 |
MypyFile:1( |
| 7 |
ImportFrom:1(typing, [List]) |
| 8 |
FuncDef:2( |
| 9 |
f |
| 10 |
def () -> builtins.list[builtins.int] |
| 11 |
Block:2( |
| 12 |
PassStmt:2()))) |
| 13 |
|
| 14 |
[case testDictTypeAlias] |
| 15 |
from typing import Dict |
| 16 |
def f() -> Dict[int, str]: pass |
| 17 |
[builtins fixtures/dict.pyi] |
| 18 |
[out] |
| 19 |
MypyFile:1( |
| 20 |
ImportFrom:1(typing, [Dict]) |
| 21 |
FuncDef:2( |
| 22 |
f |
| 23 |
def () -> builtins.dict[builtins.int, builtins.str] |
| 24 |
Block:2( |
| 25 |
PassStmt:2()))) |
| 26 |
|
| 27 |
[case testQualifiedTypeAlias] |
| 28 |
import typing |
| 29 |
def f() -> typing.List[int]: pass |
| 30 |
[builtins fixtures/list.pyi] |
| 31 |
[out] |
| 32 |
MypyFile:1( |
| 33 |
Import:1(typing) |
| 34 |
FuncDef:2( |
| 35 |
f |
| 36 |
def () -> builtins.list[builtins.int] |
| 37 |
Block:2( |
| 38 |
PassStmt:2()))) |
| 39 |
|
| 40 |
[case testTypeApplicationWithTypeAlias] |
| 41 |
from typing import List |
| 42 |
List[List[int]] |
| 43 |
[builtins fixtures/list.pyi] |
| 44 |
[out] |
| 45 |
MypyFile:1( |
| 46 |
ImportFrom:1(typing, [List]) |
| 47 |
ExpressionStmt:2( |
| 48 |
TypeApplication:2( |
| 49 |
NameExpr(List [typing.List]) |
| 50 |
Types( |
| 51 |
builtins.list[builtins.int])))) |
| 52 |
|
| 53 |
[case testTypeApplicationWithQualifiedTypeAlias] |
| 54 |
import typing |
| 55 |
typing.List[typing.List[int]] |
| 56 |
[builtins fixtures/list.pyi] |
| 57 |
[out] |
| 58 |
MypyFile:1( |
| 59 |
Import:1(typing) |
| 60 |
ExpressionStmt:2( |
| 61 |
TypeApplication:2( |
| 62 |
MemberExpr:2( |
| 63 |
NameExpr(typing) |
| 64 |
List [typing.List]) |
| 65 |
Types( |
| 66 |
builtins.list[builtins.int])))) |
| 67 |
|
| 68 |
[case testSimpleTypeAlias] |
| 69 |
import typing |
| 70 |
class A: pass |
| 71 |
A2 = A |
| 72 |
def f(x: A2) -> A: pass |
| 73 |
[out] |
| 74 |
MypyFile:1( |
| 75 |
Import:1(typing) |
| 76 |
ClassDef:2( |
| 77 |
A |
| 78 |
PassStmt:2()) |
| 79 |
AssignmentStmt:3( |
| 80 |
NameExpr(A2* [__main__.A2]) |
| 81 |
NameExpr(A [__main__.A])) |
| 82 |
FuncDef:4( |
| 83 |
f |
| 84 |
Args( |
| 85 |
Var(x)) |
| 86 |
def (x: __main__.A) -> __main__.A |
| 87 |
Block:4( |
| 88 |
PassStmt:4()))) |
| 89 |
|
| 90 |
[case testQualifiedSimpleTypeAlias] |
| 91 |
import typing |
| 92 |
import _m |
| 93 |
A2 = _m.A |
| 94 |
x = 1 # type: A2 |
| 95 |
[fixture _m.py] |
| 96 |
import typing |
| 97 |
class A: pass |
| 98 |
[out] |
| 99 |
MypyFile:1( |
| 100 |
Import:1(typing) |
| 101 |
Import:2(_m) |
| 102 |
AssignmentStmt:3( |
| 103 |
NameExpr(A2* [__main__.A2]) |
| 104 |
MemberExpr:3( |
| 105 |
NameExpr(_m) |
| 106 |
A [_m.A])) |
| 107 |
AssignmentStmt:4( |
| 108 |
NameExpr(x [__main__.x]) |
| 109 |
IntExpr(1) |
| 110 |
_m.A)) |
| 111 |
|
| 112 |
[case testUnionTypeAlias] |
| 113 |
from typing import Union |
| 114 |
U = Union[int, str] |
| 115 |
def f(x: U) -> None: pass |
| 116 |
[out] |
| 117 |
MypyFile:1( |
| 118 |
ImportFrom:1(typing, [Union]) |
| 119 |
AssignmentStmt:2( |
| 120 |
NameExpr(U* [__main__.U]) |
| 121 |
TypeAliasExpr(builtins.int | builtins.str)) |
| 122 |
FuncDef:3( |
| 123 |
f |
| 124 |
Args( |
| 125 |
Var(x)) |
| 126 |
def (x: builtins.int | builtins.str) |
| 127 |
Block:3( |
| 128 |
PassStmt:3()))) |
| 129 |
|
| 130 |
[case testUnionTypeAlias2] |
| 131 |
from typing import Union |
| 132 |
class A: pass |
| 133 |
U = Union[int, A] |
| 134 |
def f(x: U) -> None: pass |
| 135 |
[out] |
| 136 |
MypyFile:1( |
| 137 |
ImportFrom:1(typing, [Union]) |
| 138 |
ClassDef:2( |
| 139 |
A |
| 140 |
PassStmt:2()) |
| 141 |
AssignmentStmt:3( |
| 142 |
NameExpr(U* [__main__.U]) |
| 143 |
TypeAliasExpr(builtins.int | __main__.A)) |
| 144 |
FuncDef:4( |
| 145 |
f |
| 146 |
Args( |
| 147 |
Var(x)) |
| 148 |
def (x: builtins.int | __main__.A) |
| 149 |
Block:4( |
| 150 |
PassStmt:4()))) |
| 151 |
|
| 152 |
[case testUnionTypeAliasWithQualifiedUnion] |
| 153 |
import typing |
| 154 |
U = typing.Union[int, str] |
| 155 |
def f(x: U) -> None: pass |
| 156 |
[out] |
| 157 |
MypyFile:1( |
| 158 |
Import:1(typing) |
| 159 |
AssignmentStmt:2( |
| 160 |
NameExpr(U* [__main__.U]) |
| 161 |
TypeAliasExpr(builtins.int | builtins.str)) |
| 162 |
FuncDef:3( |
| 163 |
f |
| 164 |
Args( |
| 165 |
Var(x)) |
| 166 |
def (x: builtins.int | builtins.str) |
| 167 |
Block:3( |
| 168 |
PassStmt:3()))) |
| 169 |
|
| 170 |
[case testTupleTypeAlias] |
| 171 |
from typing import Tuple |
| 172 |
T = Tuple[int, str] |
| 173 |
def f(x: T) -> None: pass |
| 174 |
[builtins fixtures/tuple.pyi] |
| 175 |
[out] |
| 176 |
MypyFile:1( |
| 177 |
ImportFrom:1(typing, [Tuple]) |
| 178 |
AssignmentStmt:2( |
| 179 |
NameExpr(T* [__main__.T]) |
| 180 |
TypeAliasExpr(tuple[builtins.int, builtins.str])) |
| 181 |
FuncDef:3( |
| 182 |
f |
| 183 |
Args( |
| 184 |
Var(x)) |
| 185 |
def (x: tuple[builtins.int, builtins.str]) |
| 186 |
Block:3( |
| 187 |
PassStmt:3()))) |
| 188 |
|
| 189 |
[case testCallableTypeAlias] |
| 190 |
from typing import Callable |
| 191 |
C = Callable[[int], None] |
| 192 |
def f(x: C) -> None: pass |
| 193 |
[out] |
| 194 |
MypyFile:1( |
| 195 |
ImportFrom:1(typing, [Callable]) |
| 196 |
AssignmentStmt:2( |
| 197 |
NameExpr(C* [__main__.C]) |
| 198 |
TypeAliasExpr(def (builtins.int))) |
| 199 |
FuncDef:3( |
| 200 |
f |
| 201 |
Args( |
| 202 |
Var(x)) |
| 203 |
def (x: def (builtins.int)) |
| 204 |
Block:3( |
| 205 |
PassStmt:3()))) |
| 206 |
|
| 207 |
[case testGenericTypeAlias] |
| 208 |
from typing import Generic, TypeVar |
| 209 |
T = TypeVar('T') |
| 210 |
class G(Generic[T]): pass |
| 211 |
A = G[int] |
| 212 |
def f(x: A) -> None: pass |
| 213 |
[out] |
| 214 |
MypyFile:1( |
| 215 |
ImportFrom:1(typing, [Generic, TypeVar]) |
| 216 |
AssignmentStmt:2( |
| 217 |
NameExpr(T* [__main__.T]) |
| 218 |
TypeVarExpr:2()) |
| 219 |
ClassDef:3( |
| 220 |
G |
| 221 |
TypeVars( |
| 222 |
T`1) |
| 223 |
PassStmt:3()) |
| 224 |
AssignmentStmt:4( |
| 225 |
NameExpr(A* [__main__.A]) |
| 226 |
TypeAliasExpr(__main__.G[builtins.int])) |
| 227 |
FuncDef:5( |
| 228 |
f |
| 229 |
Args( |
| 230 |
Var(x)) |
| 231 |
def (x: __main__.G[builtins.int]) |
| 232 |
Block:5( |
| 233 |
PassStmt:5()))) |
| 234 |
|
| 235 |
[case testGenericTypeAlias2] |
| 236 |
from typing import List |
| 237 |
A = List[int] |
| 238 |
def f(x: A) -> None: pass |
| 239 |
[builtins fixtures/list.pyi] |
| 240 |
[out] |
| 241 |
MypyFile:1( |
| 242 |
ImportFrom:1(typing, [List]) |
| 243 |
AssignmentStmt:2( |
| 244 |
NameExpr(A* [__main__.A]) |
| 245 |
TypeAliasExpr(builtins.list[builtins.int])) |
| 246 |
FuncDef:3( |
| 247 |
f |
| 248 |
Args( |
| 249 |
Var(x)) |
| 250 |
def (x: builtins.list[builtins.int]) |
| 251 |
Block:3( |
| 252 |
PassStmt:3()))) |
| 253 |
|
| 254 |
[case testImportUnionTypeAlias] |
| 255 |
import typing |
| 256 |
from _m import U |
| 257 |
def f(x: U) -> None: pass |
| 258 |
[fixture _m.py] |
| 259 |
from typing import Union |
| 260 |
class A: pass |
| 261 |
U = Union[int, A] |
| 262 |
[out] |
| 263 |
MypyFile:1( |
| 264 |
Import:1(typing) |
| 265 |
ImportFrom:2(_m, [U]) |
| 266 |
FuncDef:3( |
| 267 |
f |
| 268 |
Args( |
| 269 |
Var(x)) |
| 270 |
def (x: builtins.int | _m.A) |
| 271 |
Block:3( |
| 272 |
PassStmt:3()))) |
| 273 |
|
| 274 |
[case testImportUnionTypeAlias2] |
| 275 |
import typing |
| 276 |
import _m |
| 277 |
def f(x: _m.U) -> None: pass |
| 278 |
[fixture _m.py] |
| 279 |
from typing import Union |
| 280 |
class A: pass |
| 281 |
U = Union[int, A] |
| 282 |
[out] |
| 283 |
MypyFile:1( |
| 284 |
Import:1(typing) |
| 285 |
Import:2(_m) |
| 286 |
FuncDef:3( |
| 287 |
f |
| 288 |
Args( |
| 289 |
Var(x)) |
| 290 |
def (x: builtins.int | _m.A) |
| 291 |
Block:3( |
| 292 |
PassStmt:3()))) |
| 293 |
|
| 294 |
[case testImportSimpleTypeAlias] |
| 295 |
import typing |
| 296 |
from _m import A |
| 297 |
def f(x: A) -> None: pass |
| 298 |
[fixture _m.py] |
| 299 |
import typing |
| 300 |
A = int |
| 301 |
[out] |
| 302 |
MypyFile:1( |
| 303 |
Import:1(typing) |
| 304 |
ImportFrom:2(_m, [A]) |
| 305 |
FuncDef:3( |
| 306 |
f |
| 307 |
Args( |
| 308 |
Var(x)) |
| 309 |
def (x: builtins.int) |
| 310 |
Block:3( |
| 311 |
PassStmt:3()))) |
| 312 |
|
| 313 |
[case testImportSimpleTypeAlias2] |
| 314 |
import typing |
| 315 |
import _m |
| 316 |
def f(x: _m.A) -> None: pass |
| 317 |
[fixture _m.py] |
| 318 |
import typing |
| 319 |
A = int |
| 320 |
[out] |
| 321 |
MypyFile:1( |
| 322 |
Import:1(typing) |
| 323 |
Import:2(_m) |
| 324 |
FuncDef:3( |
| 325 |
f |
| 326 |
Args( |
| 327 |
Var(x)) |
| 328 |
def (x: builtins.int) |
| 329 |
Block:3( |
| 330 |
PassStmt:3()))) |
| 331 |
|
| 332 |
[case testAnyTypeAlias] |
| 333 |
from typing import Any |
| 334 |
A = Any |
| 335 |
a = 1 # type: A |
| 336 |
[out] |
| 337 |
MypyFile:1( |
| 338 |
ImportFrom:1(typing, [Any]) |
| 339 |
AssignmentStmt:2( |
| 340 |
NameExpr(A* [__main__.A]) |
| 341 |
NameExpr(Any [typing.Any])) |
| 342 |
AssignmentStmt:3( |
| 343 |
NameExpr(a [__main__.a]) |
| 344 |
IntExpr(1) |
| 345 |
Any)) |
| 346 |
|
| 347 |
[case testAnyTypeAlias2] |
| 348 |
import typing |
| 349 |
A = typing.Any |
| 350 |
a = 1 # type: A |
| 351 |
[out] |
| 352 |
MypyFile:1( |
| 353 |
Import:1(typing) |
| 354 |
AssignmentStmt:2( |
| 355 |
NameExpr(A* [__main__.A]) |
| 356 |
MemberExpr:2( |
| 357 |
NameExpr(typing) |
| 358 |
Any [typing.Any])) |
| 359 |
AssignmentStmt:3( |
| 360 |
NameExpr(a [__main__.a]) |
| 361 |
IntExpr(1) |
| 362 |
Any)) |
| 363 |
|
| 364 |
[case testTypeAliasAlias] |
| 365 |
from typing import Union |
| 366 |
U = Union[int, str] |
| 367 |
U2 = U |
| 368 |
x = 1 # type: U2 |
| 369 |
[out] |
| 370 |
MypyFile:1( |
| 371 |
ImportFrom:1(typing, [Union]) |
| 372 |
AssignmentStmt:2( |
| 373 |
NameExpr(U* [__main__.U]) |
| 374 |
TypeAliasExpr(builtins.int | builtins.str)) |
| 375 |
AssignmentStmt:3( |
| 376 |
NameExpr(U2* [__main__.U2]) |
| 377 |
NameExpr(U [__main__.U])) |
| 378 |
AssignmentStmt:4( |
| 379 |
NameExpr(x [__main__.x]) |
| 380 |
IntExpr(1) |
| 381 |
builtins.int | builtins.str)) |
| 382 |
|
| 383 |
[case testTypeAliasOfImportedAlias] |
| 384 |
from typing import Union |
| 385 |
from _m import U |
| 386 |
U2 = U |
| 387 |
x = 1 # type: U2 |
| 388 |
[fixture _m.py] |
| 389 |
from typing import Union |
| 390 |
U = Union[int, str] |
| 391 |
[out] |
| 392 |
MypyFile:1( |
| 393 |
ImportFrom:1(typing, [Union]) |
| 394 |
ImportFrom:2(_m, [U]) |
| 395 |
AssignmentStmt:3( |
| 396 |
NameExpr(U2* [__main__.U2]) |
| 397 |
NameExpr(U [_m.U])) |
| 398 |
AssignmentStmt:4( |
| 399 |
NameExpr(x [__main__.x]) |
| 400 |
IntExpr(1) |
| 401 |
builtins.int | builtins.str)) |
| 402 |
|
| 403 |
[case testListTypeDoesNotGenerateAlias] |
| 404 |
|
| 405 |
import typing |
| 406 |
A = [int, str] |
| 407 |
a = 1 # type: A # E: Variable "__main__.A" is not valid as a type \ |
| 408 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 409 |
|
| 410 |
[case testCantUseStringLiteralAsTypeAlias] |
| 411 |
|
| 412 |
from typing import Union |
| 413 |
A = 'Union[int, str]' |
| 414 |
a = 1 # type: A # E: Variable "__main__.A" is not valid as a type \ |
| 415 |
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases |
| 416 |
|
| 417 |
[case testStringLiteralTypeAsAliasComponent] |
| 418 |
from typing import Union |
| 419 |
A = Union['int', str] |
| 420 |
a = 1 # type: A |
| 421 |
[out] |
| 422 |
MypyFile:1( |
| 423 |
ImportFrom:1(typing, [Union]) |
| 424 |
AssignmentStmt:2( |
| 425 |
NameExpr(A* [__main__.A]) |
| 426 |
TypeAliasExpr(builtins.int | builtins.str)) |
| 427 |
AssignmentStmt:3( |
| 428 |
NameExpr(a [__main__.a]) |
| 429 |
IntExpr(1) |
| 430 |
builtins.int | builtins.str)) |
| 431 |
|
| 432 |
[case testComplexTypeAlias] |
| 433 |
from typing import Union, Tuple, Any |
| 434 |
A = Union['int', Tuple[int, Any]] |
| 435 |
a = 1 # type: A |
| 436 |
[builtins fixtures/tuple.pyi] |
| 437 |
[out] |
| 438 |
MypyFile:1( |
| 439 |
ImportFrom:1(typing, [Union, Tuple, Any]) |
| 440 |
AssignmentStmt:2( |
| 441 |
NameExpr(A* [__main__.A]) |
| 442 |
TypeAliasExpr(builtins.int | tuple[builtins.int, Any])) |
| 443 |
AssignmentStmt:3( |
| 444 |
NameExpr(a [__main__.a]) |
| 445 |
IntExpr(1) |
| 446 |
builtins.int | tuple[builtins.int, Any])) |