Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documents explicit type aliases #11800

Merged
merged 4 commits into from Dec 21, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 15 additions & 1 deletion docs/source/kinds_of_types.rst
Expand Up @@ -362,7 +362,6 @@ is needed:

.. code-block:: python


class Container:
items: list[str] # No initializer

Expand Down Expand Up @@ -522,6 +521,21 @@ assigning the type to a variable:
another type -- it's equivalent to the target type except for
:ref:`generic aliases <generic-type-aliases>`.

Since Mypy 0.930 you can also use explicit type aliases which are defined by :pep:`613`.

Implicit type alias declaration rules create confusion when type aliases involve forward references,
invalid types, or violate other restrictions enforced on type alias declaration.
Because the distinction between an unannotated value and a type alias is implicit,
ambiguous or incorrect type alias declarations implicitly default to a valid value assignment.

.. code-block:: python

from typing import TypeAlias # or `from typing_extensions` before `python3.10`

AliasType: TypeAlias = Union[list[dict[tuple[int, str], set[int]]], tuple[str, list[str]]]

Explicit type aliases solves the ambiguness and improves readability.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Explicit type aliases solves the ambiguness and improves readability.
Explicit type aliases are unambiguous and improve readability.

"ambiguess" is not a word :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Sometimes I make quite funny typos 馃檪


.. _named-tuples:

Named tuples
Expand Down