PEP 585: updates green-lighted by Lukasz (#1289)
* 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 * Link to implementation * Explicitly state that pickling or copying should work
This commit is contained in:
parent
d52d55964a
commit
7f402aef96
30
pep-0585.rst
30
pep-0585.rst
|
@ -112,6 +112,8 @@ Python 3.9, the following collections become generic using
|
||||||
* ``collections.abc.ValuesView``
|
* ``collections.abc.ValuesView``
|
||||||
* ``contextlib.AbstractContextManager`` # typing.ContextManager
|
* ``contextlib.AbstractContextManager`` # typing.ContextManager
|
||||||
* ``contextlib.AbstractAsyncContextManager`` # typing.AsyncContextManager
|
* ``contextlib.AbstractAsyncContextManager`` # typing.AsyncContextManager
|
||||||
|
* ``re.Pattern`` # typing.Pattern, typing.re.Pattern
|
||||||
|
* ``re.Match`` # typing.Match, typing.re.Match
|
||||||
|
|
||||||
Importing those from ``typing`` is deprecated. Due to PEP 563 and the
|
Importing those from ``typing`` is deprecated. Due to PEP 563 and the
|
||||||
intention to minimize the runtime impact of typing, this deprecation
|
intention to minimize the runtime impact of typing, this deprecation
|
||||||
|
@ -150,10 +152,13 @@ exceptions:
|
||||||
* the ``__repr__`` shows the parametrized type;
|
* the ``__repr__`` shows the parametrized type;
|
||||||
* the ``__origin__`` attribute points at the non-parametrized
|
* the ``__origin__`` attribute points at the non-parametrized
|
||||||
generic class;
|
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__``;
|
1) of generic types passed to the original ``__class_getitem__``;
|
||||||
* the ``__class_getitem__`` raises an exception to disallow mistakes
|
* the ``__parameters__`` attribute is a lazily computed tuple
|
||||||
like ``dict[str][str]``.
|
(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
|
This design means that it is possible to create instances of
|
||||||
parametrized collections, like::
|
parametrized collections, like::
|
||||||
|
@ -165,7 +170,11 @@ parametrized collections, like::
|
||||||
>>> list is list[str]
|
>>> list is list[str]
|
||||||
False
|
False
|
||||||
>>> list == list[str]
|
>>> list == list[str]
|
||||||
|
False
|
||||||
|
>>> list[str] == list[str]
|
||||||
True
|
True
|
||||||
|
>>> list[str] == list[int]
|
||||||
|
False
|
||||||
|
|
||||||
Objects created with bare types and parametrized types are exactly the
|
Objects created with bare types and parametrized types are exactly the
|
||||||
same. The generic parameters are not preserved in instances created
|
same. The generic parameters are not preserved in instances created
|
||||||
|
@ -182,6 +191,12 @@ and::
|
||||||
|
|
||||||
l = list[str]()
|
l = list[str]()
|
||||||
|
|
||||||
|
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
|
Forward compatibility
|
||||||
---------------------
|
---------------------
|
||||||
|
@ -189,6 +204,13 @@ Forward compatibility
|
||||||
Future standard collections must implement the same behavior.
|
Future standard collections must implement the same behavior.
|
||||||
|
|
||||||
|
|
||||||
|
Reference implementation
|
||||||
|
========================
|
||||||
|
|
||||||
|
A proof-of-concept or prototype `implementation
|
||||||
|
<https://bugs.python.org/issue39481>`__ exists.
|
||||||
|
|
||||||
|
|
||||||
Rejected alternatives
|
Rejected alternatives
|
||||||
=====================
|
=====================
|
||||||
|
|
||||||
|
@ -261,7 +283,7 @@ Disallowing instantiation of parametrized types
|
||||||
-----------------------------------------------
|
-----------------------------------------------
|
||||||
|
|
||||||
Given that the proxy type which preserves ``__origin__`` and
|
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.
|
we might have disallowed instantiation of parametrized types.
|
||||||
|
|
||||||
In fact, forbidding instantiation of parametrized types is what the
|
In fact, forbidding instantiation of parametrized types is what the
|
||||||
|
|
Loading…
Reference in New Issue