MCPcopy Index your code
hub / github.com/python/mypy / TypeAlias

Class TypeAlias

mypy/nodes.py:4444–4706  ·  view source on GitHub ↗

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[...

Source from the content-addressed store, hash-verified

4442
4443
4444class 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

Callers 10

missing_aliasFunction · 0.90
create_aliasMethod · 0.90
visit_type_alias_stmtMethod · 0.90
def_alias_1Method · 0.90
def_alias_2Method · 0.90
non_rec_aliasMethod · 0.90
from_tuple_typeMethod · 0.85
from_typeddict_typeMethod · 0.85
readMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…