Skip to content

Built-in generic now work without limitations #1068

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions docs/source/stubs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,20 @@ Type checkers support cyclic imports in stub files.
Built-in Generics
-----------------

PEP 585 [#pep585]_ built-in generics are generally supported, with
the following exceptions [#ts-4820]_:
PEP 585 [#pep585]_ built-in generics are supported and should be used instead
of the corresponding types from ``typing``::

* Built-in generics don't work in type aliases.
* Built-in generics don't work in base classes.
* ``type`` is not supported.
* Variable length tuples (``tuple[X, ...]``) are not supported.
from collections import defaultdict

In these cases, the appropriate types from ``typing`` must be used.
def foo(t: type[MyClass]) -> list[int]: ...
x: defaultdict[int]

Using imports from ``collections.abc`` instead of ``typing`` is
generally possible and recommended.
generally possible and recommended::

from collections.abc import Iterable

def foo(iter: Iterable[int]) -> None: ...

Unions
------
Expand Down Expand Up @@ -1102,7 +1104,6 @@ Bugs
----

.. [#ts-4819] typeshed issue #4819 -- PEP 604 tracker (https://github.com/python/typeshed/issues/4819)
.. [#ts-4820] typeshed issue #4820 -- PEP 585 tracker (https://github.com/python/typeshed/issues/4820)
.. [#ts-4913] typeshed issue #4913 -- PEP 613 tracker (https://github.com/python/typeshed/issues/4913)
.. [#ts-4972] typeshed issue #4972 -- PEP 570 tracker (https://github.com/python/typeshed/issues/4972)

Expand Down