From 3a789dfc66a4b4f3998dc1e8c74169bc3118a6df Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Fri, 11 Feb 2022 12:24:44 +0100 Subject: [PATCH 1/3] Built-in generic now work without limitations Closes: #1067 --- docs/source/stubs.rst | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/docs/source/stubs.rst b/docs/source/stubs.rst index a7ab6dfe..48624666 100644 --- a/docs/source/stubs.rst +++ b/docs/source/stubs.rst @@ -174,15 +174,8 @@ 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]_: - -* 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. - -In these cases, the appropriate types from ``typing`` must be used. +PEP 585 [#pep585]_ built-in generics are supported and should be used instead +of the corresponding types from ``typing``. Using imports from ``collections.abc`` instead of ``typing`` is generally possible and recommended. @@ -1101,7 +1094,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-4827] typeshed issue #4827 -- PEP 612 tracker (https://github.com/python/typeshed/issues/4827) .. [#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) From 926898606a3e239416061be6ee9a4962614a0297 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Fri, 11 Feb 2022 18:26:57 +0100 Subject: [PATCH 2/3] Add examples --- docs/source/stubs.rst | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/source/stubs.rst b/docs/source/stubs.rst index c6b558cd..7f08e979 100644 --- a/docs/source/stubs.rst +++ b/docs/source/stubs.rst @@ -175,10 +175,19 @@ Built-in Generics ----------------- PEP 585 [#pep585]_ built-in generics are supported and should be used instead -of the corresponding types from ``typing``. +of the corresponding types from ``typing``:: + + from collections import defaultdict + + def foo(t: type[MyClass]) -> list[int]: ... + x: defaultdict[int] = defaultdict(lambda: 42) 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 ------ From 47bdef3e880d5d09c48c0514d2fea4474ef13cb7 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Sat, 12 Feb 2022 03:09:58 +0100 Subject: [PATCH 3/3] Update docs/source/stubs.rst Co-authored-by: Jelle Zijlstra --- docs/source/stubs.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/stubs.rst b/docs/source/stubs.rst index 7f08e979..8408c805 100644 --- a/docs/source/stubs.rst +++ b/docs/source/stubs.rst @@ -180,7 +180,7 @@ of the corresponding types from ``typing``:: from collections import defaultdict def foo(t: type[MyClass]) -> list[int]: ... - x: defaultdict[int] = defaultdict(lambda: 42) + x: defaultdict[int] Using imports from ``collections.abc`` instead of ``typing`` is generally possible and recommended::