Skip to content

Commit 3b913a4

Browse files
sobolevnJelleZijlstra
authored andcommitted
Documents explicit type aliases (#11800)
Refs https://www.python.org/dev/peps/pep-0613/ Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 768ce5b commit 3b913a4

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

docs/source/common_issues.rst

+31-22
Original file line numberDiff line numberDiff line change
@@ -638,39 +638,48 @@ Mypy has both type aliases and variables with types like ``Type[...]`` and it is
638638

639639
1. Variables with type ``Type[...]`` should be created by assignments with an explicit type annotations:
640640

641-
.. code-block:: python
641+
.. code-block:: python
642642
643-
class A: ...
644-
tp: Type[A] = A
643+
class A: ...
644+
tp: Type[A] = A
645645
646-
2. Aliases are created by assignments without an explicit type:
646+
2. Aliases are created by assignments without an explicit type.
647647

648-
.. code-block:: python
648+
.. code-block:: python
649+
650+
class A: ...
651+
Alias = A
649652
650-
class A: ...
651-
Alias = A
653+
Or you can also use :pep:`613` and explicit type aliases:
654+
655+
.. code-block:: python
656+
657+
from typing import TypeAlias # or `from typing_extensions` before `python3.10`
658+
659+
class A: ...
660+
Alias: TypeAlias = A
652661
653662
3. The difference is that aliases are completely known statically and can be used in type context (annotations):
654663

655-
.. code-block:: python
664+
.. code-block:: python
656665
657-
class A: ...
658-
class B: ...
666+
class A: ...
667+
class B: ...
659668
660-
if random() > 0.5:
661-
Alias = A
662-
else:
663-
Alias = B # error: Cannot assign multiple types to name "Alias" without an explicit "Type[...]" annotation \
664-
# error: Incompatible types in assignment (expression has type "Type[B]", variable has type "Type[A]")
669+
if random() > 0.5:
670+
Alias = A
671+
else:
672+
Alias = B # error: Cannot assign multiple types to name "Alias" without an explicit "Type[...]" annotation \
673+
# error: Incompatible types in assignment (expression has type "Type[B]", variable has type "Type[A]")
665674
666-
tp: Type[object] # tp is a type variable
667-
if random() > 0.5:
668-
tp = A
669-
else:
670-
tp = B # This is OK
675+
tp: Type[object] # tp is a type variable
676+
if random() > 0.5:
677+
tp = A
678+
else:
679+
tp = B # This is OK
671680
672-
def fun1(x: Alias) -> None: ... # This is OK
673-
def fun2(x: tp) -> None: ... # error: Variable "__main__.tp" is not valid as a type
681+
def fun1(x: Alias) -> None: ... # This is OK
682+
def fun2(x: tp) -> None: ... # error: Variable "__main__.tp" is not valid as a type
674683
675684
Incompatible overrides
676685
----------------------

docs/source/kinds_of_types.rst

+15-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ is needed:
362362

363363
.. code-block:: python
364364
365-
366365
class Container:
367366
items: list[str] # No initializer
368367
@@ -522,6 +521,21 @@ assigning the type to a variable:
522521
another type -- it's equivalent to the target type except for
523522
:ref:`generic aliases <generic-type-aliases>`.
524523

524+
Since Mypy 0.930 you can also use explicit type aliases which are defined by :pep:`613`.
525+
526+
Implicit type alias declaration rules create confusion when type aliases involve forward references,
527+
invalid types, or violate other restrictions enforced on type alias declaration.
528+
Because the distinction between an unannotated value and a type alias is implicit,
529+
ambiguous or incorrect type alias declarations implicitly default to a valid value assignment.
530+
531+
.. code-block:: python
532+
533+
from typing import TypeAlias # or `from typing_extensions` before `python3.10`
534+
535+
AliasType: TypeAlias = Union[list[dict[tuple[int, str], set[int]]], tuple[str, list[str]]]
536+
537+
Explicit type aliases are unambiguous and improve readability.
538+
525539
.. _named-tuples:
526540

527541
Named tuples

0 commit comments

Comments
 (0)