From 4a7865e6eb00106277e8274459990f7a0780785e Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 30 Jan 2020 10:02:38 -0800 Subject: [PATCH 1/4] PEP 585: updates green-lighted by Lukasz - Rename __parameters__ to __args__, for typing.py compatibility - Genericize, re.{Pattern,Match} and io.IO - list != list[int], but list[int] == list[int] (and list[str] != list[int]) - Add a lazy __parameters__ that contains the unique type vars in __args__ (also for typing.py compatibility) - make dict[str][str] fail, but dict[T, str][int] return dict[int, str] - expose proxy type as types.GenericAlias --- pep-0585.rst | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/pep-0585.rst b/pep-0585.rst index 6fee22365ef..060caf3d3f2 100644 --- a/pep-0585.rst +++ b/pep-0585.rst @@ -112,6 +112,9 @@ Python 3.9, the following collections become generic using * ``collections.abc.ValuesView`` * ``contextlib.AbstractContextManager`` # typing.ContextManager * ``contextlib.AbstractAsyncContextManager`` # typing.AsyncContextManager +* ``re.Pattern`` # typing.Pattern, typing.re.Pattern +* ``re.Match`` # typing.Match, typing.re.Match +* ``io.IO`` # typing.IO, typing.io.IO Importing those from ``typing`` is deprecated. Due to PEP 563 and the intention to minimize the runtime impact of typing, this deprecation @@ -150,10 +153,13 @@ exceptions: * the ``__repr__`` shows the parametrized type; * the ``__origin__`` attribute points at the non-parametrized generic class; -* the ``__parameters__`` attribute is a tuple (possibly of length +* the ``__args__`` attribute is a tuple (possibly of length 1) of generic types passed to the original ``__class_getitem__``; -* the ``__class_getitem__`` raises an exception to disallow mistakes - like ``dict[str][str]``. +* the ``__parameters__`` attribute is a lazily computed tuple + (possibly empty) of unique type variables found in ``__args__``; +* the ``__getitem__`` raises an exception to disallow mistakes + like ``dict[str][str]``. However it allows e.g. ``dict[str, T][int]`` + and in that case returns ``dict[str, int]``. This design means that it is possible to create instances of parametrized collections, like:: @@ -165,7 +171,11 @@ parametrized collections, like:: >>> list is list[str] False >>> list == list[str] + False + >>> list[str] == list[str] True + >>> list[str] == list[int] + False Objects created with bare types and parametrized types are exactly the same. The generic parameters are not preserved in instances created @@ -182,6 +192,9 @@ and:: l = list[str]() +For accessing the proxy type from Python code, it will be exported +from the ``types`` module as ``GenericAlias``. + Forward compatibility --------------------- @@ -261,7 +274,7 @@ Disallowing instantiation of parametrized types ----------------------------------------------- Given that the proxy type which preserves ``__origin__`` and -``__parameters__`` is mostly useful for runtime introspection purposes, +``__args__`` is mostly useful for runtime introspection purposes, we might have disallowed instantiation of parametrized types. In fact, forbidding instantiation of parametrized types is what the From 8bcbc137d244da9cbffd06570e9deb7a243dc71f Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Thu, 30 Jan 2020 20:34:33 -0800 Subject: [PATCH 2/4] Link to implementation --- pep-0585.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pep-0585.rst b/pep-0585.rst index 060caf3d3f2..c6f6348f87e 100644 --- a/pep-0585.rst +++ b/pep-0585.rst @@ -202,6 +202,13 @@ Forward compatibility Future standard collections must implement the same behavior. +Reference implementation +======================== + +A proof-of-concept or prototype `implementation +`__ exists. + + Rejected alternatives ===================== From 1480aee1214a7d37c2f9e9afec8b6a6ccb40effd Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 31 Jan 2020 15:45:25 -0800 Subject: [PATCH 3/4] Explicitly state that pickling or copying should work --- pep-0585.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pep-0585.rst b/pep-0585.rst index c6f6348f87e..e7b2fbc9b58 100644 --- a/pep-0585.rst +++ b/pep-0585.rst @@ -195,6 +195,9 @@ and:: For accessing the proxy type from Python code, it will be exported from the ``types`` module as ``GenericAlias``. +Pickling or (shallow- or deep-) copying a ``GenericAlias`` instance +will preserve the type, origin, attributes and parameters. + Forward compatibility --------------------- From ab3fedbaa2e62e1d2f0a15c4de616de72643ee44 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 4 Feb 2020 20:48:08 -0800 Subject: [PATCH 4/4] Scratch changing typing.IO (it's not like the others) --- pep-0585.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/pep-0585.rst b/pep-0585.rst index e7b2fbc9b58..4a30235e08c 100644 --- a/pep-0585.rst +++ b/pep-0585.rst @@ -114,7 +114,6 @@ Python 3.9, the following collections become generic using * ``contextlib.AbstractAsyncContextManager`` # typing.AsyncContextManager * ``re.Pattern`` # typing.Pattern, typing.re.Pattern * ``re.Match`` # typing.Match, typing.re.Match -* ``io.IO`` # typing.IO, typing.io.IO Importing those from ``typing`` is deprecated. Due to PEP 563 and the intention to minimize the runtime impact of typing, this deprecation