Skip to content

PEP 585: updates green-lighted by Lukasz #1289

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 6, 2020
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
30 changes: 26 additions & 4 deletions pep-0585.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ 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

Copy link
Member

Choose a reason for hiding this comment

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

I believe there are still other containers that should be made generic like queue.*Queue, should the PEP enumerate all of them?

Copy link
Member Author

Choose a reason for hiding this comment

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

Honestly I think the PEP should stick to things that are currently shadowed in typing.py. Once we have GenericAlias it's easy enough to make other classes generic.

Copy link
Member

Choose a reason for hiding this comment

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

Okay sounds good!

Importing those from ``typing`` is deprecated. Due to PEP 563 and the
intention to minimize the runtime impact of typing, this deprecation
Expand Down Expand Up @@ -150,10 +152,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::
Expand All @@ -165,7 +170,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
Expand All @@ -182,13 +191,26 @@ and::

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
---------------------

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
=====================

Expand Down Expand Up @@ -261,7 +283,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
Expand Down