Skip to content

Commit b7f8077

Browse files
committed
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
1 parent 565b677 commit b7f8077

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

pep-0585.rst

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ 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
117+
* ``io.IO`` # typing.IO, typing.io.IO
115118

116119
Importing those from ``typing`` is deprecated. Due to PEP 563 and the
117120
intention to minimize the runtime impact of typing, this deprecation
@@ -150,10 +153,13 @@ exceptions:
150153
* the ``__repr__`` shows the parametrized type;
151154
* the ``__origin__`` attribute points at the non-parametrized
152155
generic class;
153-
* the ``__parameters__`` attribute is a tuple (possibly of length
156+
* the ``__args__`` attribute is a tuple (possibly of length
154157
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]``.
158+
* the ``__parameters__`` attribute is a lazily computed tuple
159+
(possibly empty) of unique type variables found in ``__args__``;
160+
* the ``__getitem__`` raises an exception to disallow mistakes
161+
like ``dict[str][str]``. However it allows e.g. ``dict[str, T][int]``
162+
and in that case returns ``dict[str, int]``.
157163

158164
This design means that it is possible to create instances of
159165
parametrized collections, like::
@@ -165,7 +171,11 @@ parametrized collections, like::
165171
>>> list is list[str]
166172
False
167173
>>> list == list[str]
174+
False
175+
>>> list[str] == list[str]
168176
True
177+
>>> list[str] == list[int]
178+
False
169179

170180
Objects created with bare types and parametrized types are exactly the
171181
same. The generic parameters are not preserved in instances created
@@ -182,6 +192,9 @@ and::
182192

183193
l = list[str]()
184194

195+
For accessing the proxy type from Python code, it will be exported
196+
from the ``types`` module as ``GenericAlias``.
197+
185198

186199
Forward compatibility
187200
---------------------
@@ -261,7 +274,7 @@ Disallowing instantiation of parametrized types
261274
-----------------------------------------------
262275

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

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

0 commit comments

Comments
 (0)