@@ -112,6 +112,8 @@ Python 3.9, the following collections become generic using
112
112
* ``collections.abc.ValuesView ``
113
113
* ``contextlib.AbstractContextManager `` # typing.ContextManager
114
114
* ``contextlib.AbstractAsyncContextManager `` # typing.AsyncContextManager
115
+ * ``re.Pattern `` # typing.Pattern, typing.re.Pattern
116
+ * ``re.Match `` # typing.Match, typing.re.Match
115
117
116
118
Importing those from ``typing `` is deprecated. Due to PEP 563 and the
117
119
intention to minimize the runtime impact of typing, this deprecation
@@ -150,10 +152,13 @@ exceptions:
150
152
* the ``__repr__ `` shows the parametrized type;
151
153
* the ``__origin__ `` attribute points at the non-parametrized
152
154
generic class;
153
- * the ``__parameters__ `` attribute is a tuple (possibly of length
155
+ * the ``__args__ `` attribute is a tuple (possibly of length
154
156
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] ``.
157
162
158
163
This design means that it is possible to create instances of
159
164
parametrized collections, like::
@@ -165,7 +170,11 @@ parametrized collections, like::
165
170
>>> list is list[str]
166
171
False
167
172
>>> list == list[str]
173
+ False
174
+ >>> list[str] == list[str]
168
175
True
176
+ >>> list[str] == list[int]
177
+ False
169
178
170
179
Objects created with bare types and parametrized types are exactly the
171
180
same. The generic parameters are not preserved in instances created
@@ -182,13 +191,26 @@ and::
182
191
183
192
l = list[str]()
184
193
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
+
185
200
186
201
Forward compatibility
187
202
---------------------
188
203
189
204
Future standard collections must implement the same behavior.
190
205
191
206
207
+ Reference implementation
208
+ ========================
209
+
210
+ A proof-of-concept or prototype `implementation
211
+ <https://bugs.python.org/issue39481> `__ exists.
212
+
213
+
192
214
Rejected alternatives
193
215
=====================
194
216
@@ -261,7 +283,7 @@ Disallowing instantiation of parametrized types
261
283
-----------------------------------------------
262
284
263
285
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,
265
287
we might have disallowed instantiation of parametrized types.
266
288
267
289
In fact, forbidding instantiation of parametrized types is what the
0 commit comments