8000 bpo-44732: Rename types.Union to types.UnionType (GH-27342) · python/cpython@8a37e8c · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a37e8c

Browse files
bpo-44732: Rename types.Union to types.UnionType (GH-27342)
Co-authored-by: Łukasz Langa <lukasz@langa.pl> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> (cherry picked from commit 2b8ad9e) Co-authored-by: Hasan <hasan.aleeyev@gmail.com>
1 parent 16a174f commit 8a37e8c

File tree

7 files changed

+22
-22
lines changed

7 files changed

+22
-22
lines changed

Doc/library/stdtypes.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5040,16 +5040,16 @@ enables cleaner type hinting syntax compared to :data:`typing.Union`.
50405040
TypeError: isinstance() argument 2 cannot contain a parameterized generic
50415041

50425042
The user-exposed type for the union object can be accessed from
5043-
:data:`types.Union` and used for :func:`isinstance` checks. An object cannot be
5043+
:data:`types.UnionType` and used for :func:`isinstance` checks. An object cannot be
50445044
instantiated from the type::
50455045

50465046
>>> import types
5047-
>>> isinstance(int | str, types.Union)
5047+
>>> isinstance(int | str, types.UnionType)
50485048
True
5049-
>>> types.Union()
5049+
>>> types.UnionType()
50505050
Traceback (most recent call last):
50515051
File "<stdin>", line 1, in <module>
5052-
TypeError: cannot create 'types.Union' instances
5052+
TypeError: cannot create 'types.UnionType' instances
50535053

50545054
.. note::
50555055
The :meth:`__or__` method for type objects was added to support the syntax

Doc/library/types.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ Standard names are defined for the following types:
312312
This type can now be subclassed.
313313

314314

315-
.. data:: Union
315+
.. data:: UnionType
316316

317317
The type of :ref:`union type expressions<types-union>`.
318318

Lib/test/test_typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3084,7 +3084,7 @@ class C(Generic[T]): pass
30843084
self.assertIs(get_origin(Callable), collections.abc.Callable)
30853085
self.assertIs(get_origin(list[int]), list)
30863086
self.assertIs(get_origin(list), None)
3087-
self.assertIs(get_origin(list | str), types.Union)
3087+
self.assertIs(get_origin(list | str), types.UnionType)
30883088
self.assertIs(get_origin(P.args), P)
30893089
self.assertIs(get_origin(P.kwargs), P)
30903090

Lib/types.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,8 @@ def wrapped(*args, **kwargs):
297297

298298
return wrapped
299299

300-
301300
GenericAlias = type(list[int])
302-
Union = type(int | str)
301+
UnionType = type(int | str)
303302

304303
EllipsisType = type(Ellipsis)
305304
NoneType = type(None)

Lib/typing.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def _type_check(arg, msg, is_argument=True, module=None):
167167
return arg
168168
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
169169
raise TypeError(f"Plain {arg} is not valid as type argument")
170-
if isinstance(arg, (type, TypeVar, ForwardRef, types.Union, ParamSpec)):
170+
if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec)):
171171
return arg
172172
if not callable(arg):
173173
raise TypeError(f"{msg} Got {arg!r:.100}.")
@@ -207,7 +207,7 @@ def _collect_type_vars(types_, typevar_types=None):
207207
for t in types_:
208208
if isinstance(t, typevar_types) and t not in tvars:
209209
tvars.append(t)
210-
if isinstance(t, (_GenericAlias, GenericAlias, types.Union)):
210+
if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
211211
tvars.extend([t for t in t.__parameters__ if t not in tvars])
212212
return tuple(tvars)
213213

@@ -260,7 +260,7 @@ def _remove_dups_flatten(parameters):
260260
# Flatten out Union[Union[...], ...].
261261
params = []
262262
for p in parameters:
263-
if isinstance(p, (_UnionGenericAlias, types.Union)):
263+
if isinstance(p, (_UnionGenericAlias, types.UnionType)):
264264
params.extend(p.__args__)
265265
elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
266266
params.extend(p[1:])
@@ -314,13 +314,13 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
314314
"""
315315
if isinstance(t, ForwardRef):
316316
return t._evaluate(globalns, localns, recursive_guard)
317-
if isinstance(t, (_GenericAlias, GenericAlias, types.Union)):
317+
if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
318318
ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
319319
if ev_args == t.__args__:
320320
return t
321321
if isinstance(t, GenericAlias):
322322
return GenericAlias(t.__origin__, ev_args)
323-
if isinstance(t, types.Union):
323+
if isinstance(t, types.UnionType):
324324
return functools.reduce(operator.or_, ev_args)
325325
else:
326326
return t.copy_with(ev_args)
@@ -1030,7 +1030,7 @@ def __getitem__(self, params):
10301030
for arg in self.__args__:
10311031
if isinstance(arg, self._typevar_types):
10321032
arg = subst[arg]
1033-
elif isinstance(arg, (_GenericAlias, GenericAlias, types.Union)):
1033+
elif isinstance(arg, (_GenericAlias, GenericAlias, types.UnionType)):
10341034
subparams = arg.__parameters__
10351035
if subparams:
10361036
subargs = tuple(subst[x] for x in subparams)
@@ -1198,7 +1198,7 @@ def copy_with(self, params):
11981198
return Union[params]
11991199

12001200
def __eq__(self, other):
1201-
if not isinstance(other, (_UnionGenericAlias, types.Union)):
1201+
if not isinstance(other, (_UnionGenericAlias, types.UnionType)):
12021202
return NotImplemented
12031203
return set(self.__args__) == set(other.__args__)
12041204

@@ -1802,7 +1802,7 @@ def _strip_annotations(t):
18021802
if stripped_args == t.__args__:
18031803
return t
18041804
return GenericAlias(t.__origin__, stripped_args)
1805-
if isinstance(t, types.Union):
1805+
if isinstance(t, types.UnionType):
18061806
stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
18071807
if stripped_args == t.__args__:
18081808
return t
@@ -1833,8 +1833,8 @@ def get_origin(tp):
18331833
return tp.__origin__
18341834
if tp is Generic:
18351835
return Generic
1836-
if isinstance(tp, types.Union):
1837-
return types.Union
1836+
if isinstance(tp, types.UnionType):
1837+
return types.UnionType
18381838
return None
18391839

18401840

@@ -1858,7 +1858,7 @@ def get_args(tp):
18581858
or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
18591859
res = (list(res[:-1]), res[-1])
18601860
return res
1861-
if isinstance(tp, types.Union):
1861+
if isinstance(tp, types.UnionType):
18621862
return tp.__args__
18631863
return ()
18641864

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Rename ``types.Union`` to ``types.UnionType``.

Objects/unionobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// types.Union -- used to represent e.g. Union[int, str], int | str
1+
// types.UnionType -- used to represent e.g. Union[int, str], int | str
22
#include "Python.h"
33
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
44
#include "pycore_unionobject.h"
@@ -414,7 +414,7 @@ union_parameters(PyObject *self, void *Py_UNUSED(unused))
414414
}
415415

416416
static PyGetSetDef union_properties[] = {
417-
{"__parameters__", union_parameters, (setter)NULL, "Type variables in the types.Union.", NULL},
417+
{"__parameters__", union_parameters, (setter)NULL, "Type variables in the types.UnionType.", NULL},
418418
{0}
419419
};
420420

@@ -424,7 +424,7 @@ static PyNumberMethods union_as_number = {
424424

425425
PyTypeObject _PyUnion_Type = {
426426
PyVarObject_HEAD_INIT(&PyType_Type, 0)
427-
.tp_name = "types.Union",
427+
.tp_name = "types.UnionType",
428428
.tp_doc = "Represent a PEP 604 union type\n"
429429
"\n"
430430
"E.g. for int | str",

0 commit comments

Comments
 (0)
0