A symbol node representing a type alias. Type alias is a static concept, in contrast to variables with types like Type[...]. Namely: * type aliases - can be used in type context (annotations) - cannot be re-assigned * variables with type Type[...
| 4442 | |
| 4443 | |
| 4444 | class TypeAlias(SymbolNode): |
| 4445 | """ |
| 4446 | A symbol node representing a type alias. |
| 4447 | |
| 4448 | Type alias is a static concept, in contrast to variables with types |
| 4449 | like Type[...]. Namely: |
| 4450 | * type aliases |
| 4451 | - can be used in type context (annotations) |
| 4452 | - cannot be re-assigned |
| 4453 | * variables with type Type[...] |
| 4454 | - cannot be used in type context |
| 4455 | - but can be re-assigned |
| 4456 | |
| 4457 | An alias can be defined only by an assignment to a name (not any other lvalues). |
| 4458 | |
| 4459 | Such assignment defines an alias by default. To define a variable, |
| 4460 | an explicit Type[...] annotation is required. As an exception, |
| 4461 | at non-global scope non-subscripted rvalue creates a variable even without |
| 4462 | an annotation. This exception exists to accommodate the common use case of |
| 4463 | class-valued attributes. See SemanticAnalyzerPass2.check_and_set_up_type_alias |
| 4464 | for details. |
| 4465 | |
| 4466 | Aliases can be generic. We use bound type variables for generic aliases, similar |
| 4467 | to classes. Essentially, type aliases work as macros that expand textually. |
| 4468 | The definition and expansion rules are following: |
| 4469 | |
| 4470 | 1. An alias targeting a generic class without explicit variables act as |
| 4471 | the given class (this doesn't apply to TypedDict, Tuple and Callable, which |
| 4472 | are not proper classes but special type constructors): |
| 4473 | |
| 4474 | A = List |
| 4475 | AA = List[Any] |
| 4476 | |
| 4477 | x: A # same as List[Any] |
| 4478 | x: A[int] # same as List[int] |
| 4479 | |
| 4480 | x: AA # same as List[Any] |
| 4481 | x: AA[int] # Error! |
| 4482 | |
| 4483 | C = Callable # Same as Callable[..., Any] |
| 4484 | T = Tuple # Same as Tuple[Any, ...] |
| 4485 | |
| 4486 | 2. An alias using explicit type variables in its rvalue expects |
| 4487 | replacements (type arguments) for these variables. If missing, they |
| 4488 | are treated as Any, like for other generics: |
| 4489 | |
| 4490 | B = List[Tuple[T, T]] |
| 4491 | |
| 4492 | x: B # same as List[Tuple[Any, Any]] |
| 4493 | x: B[int] # same as List[Tuple[int, int]] |
| 4494 | |
| 4495 | def f(x: B[T]) -> T: ... # without T, Any would be used here |
| 4496 | |
| 4497 | 3. An alias can be defined using another aliases. In the definition |
| 4498 | rvalue the Any substitution doesn't happen for top level unsubscripted |
| 4499 | generic classes: |
| 4500 | |
| 4501 | A = List |
no outgoing calls
no test coverage detected
searching dependent graphs…