Skip to content

Commit 7f402ae

Browse files
authored
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
1 parent d52d559 commit 7f402ae

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

pep-0585.rst

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ Python 3.9, the following collections become generic using
112112
* ``collections.abc.ValuesView``
113113
* ``contextlib.AbstractContextManager`` # typing.ContextManager
114114
* ``contextlib.AbstractAsyncContextManager`` # typing.AsyncContextManager
115+
* ``re.Pattern`` # typing.Pattern, typing.re.Pattern
116+
* ``re.Match`` # typing.Match, typing.re.Match
115117

116118
Importing those from ``typing`` is deprecated. Due to PEP 563 and the
117119
intention to minimize the runtime impact of typing, this deprecation
@@ -150,10 +152,13 @@ exceptions:
150152
* the ``__repr__`` shows the parametrized type;
151153
* the ``__origin__`` attribute points at the non-parametrized
152154
generic class;
153-
* the ``__parameters__`` attribute is a tuple (possibly of length
155+
* the ``__args__`` attribute is a tuple (possibly of length
154156
1) of generic types passed to the original ``__class_getitem__``;
155-
* the ``__class_getitem__`` raises an exception to disallow mistakes
156-
like ``dict[str][str]``.
157+
* the ``__parameters__`` attribute is a lazily computed tuple
158+
(possibly empty) of unique type variables found in ``__args__``;
159+
* the ``__getitem__`` raises an exception to disallow mistakes
160+
like ``dict[str][str]``. However it allows e.g. ``dict[str, T][int]``
161+
and in that case returns ``dict[str, int]``.
157162

158163
This design means that it is possible to create instances of
159164
parametrized collections, like::
@@ -165,7 +170,11 @@ parametrized collections, like::
165170
>>> list is list[str]
166171
False
167172
>>> list == list[str]
173+
False
174+
>>> list[str] == list[str]
168175
True
176+
>>> list[str] == list[int]
177+
False
169178

170179
Objects created with bare types and parametrized types are exactly the
171180
same. The generic parameters are not preserved in instances created
@@ -182,13 +191,26 @@ and::
182191

183192
l = list[str]()
184193

194+
For accessing the proxy type from Python code, it will be exported
195+
from the ``types`` module as ``GenericAlias``.
196+
197+
Pickling or (shallow- or deep-) copying a ``GenericAlias`` instance
198+
will preserve the type, origin, attributes and parameters.
199+
185200

186201
Forward compatibility
187202
---------------------
188203

189204
Future standard collections must implement the same behavior.
190205

191206

207+
Reference implementation
208+
========================
209+
210+
A proof-of-concept or prototype `implementation
211+
<https://bugs.python.org/issue39481>`__ exists.
212+
213+
192214
Rejected alternatives
193215
=====================
194216

@@ -261,7 +283,7 @@ Disallowing instantiation of parametrized types
261283
-----------------------------------------------
262284

263285
Given that the proxy type which preserves ``__origin__`` and
264-
``__parameters__`` is mostly useful for runtime introspection purposes,
286+
``__args__`` is mostly useful for runtime introspection purposes,
265287
we might have disallowed instantiation of parametrized types.
266288

267289
In fact, forbidding instantiation of parametrized types is what the

0 commit comments

Comments
 (0)