Skip to content

Commit 2b8ad9e

Browse files
AliyevHambvJelleZijlstra
authored
bpo-44732: Rename types.Union to types.UnionType (#27342)
Co-authored-by: Łukasz Langa <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 3e5b82e commit 2b8ad9e

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
@@ -3090,7 +3090,7 @@ class C(Generic[T]): pass
30903090
self.assertIs(get_origin(Callable), collections.abc.Callable)
30913091
self.assertIs(get_origin(list[int]), list)
30923092
self.assertIs(get_origin(list), None)
3093-
self.assertIs(get_origin(list | str), types.Union)
3093+
self.assertIs(get_origin(list | str), types.UnionType)
30943094
self.assertIs(get_origin(P.args), P)
30953095
self.assertIs(get_origin(P.kwargs), P)
30963096

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
@@ -175,7 +175,7 @@ def _type_check(arg, msg, is_argument=True, module=None):
175175
return arg
176176
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
177177
raise TypeError(f"Plain {arg} is not valid as type argument")
178-
if isinstance(arg, (type, TypeVar, ForwardRef, types.Union, ParamSpec)):
178+
if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec)):
179179
return arg
180180
if not callable(arg):
181181
raise TypeError(f"{msg} Got {arg!r:.100}.")
@@ -215,7 +215,7 @@ def _collect_type_vars(types_, typevar_types=None):
215215
for t in types_:
216216
if isinstance(t, typevar_types) and t not in tvars:
217217
tvars.append(t)
218-
if isinstance(t, (_GenericAlias, GenericAlias, types.Union)):
218+
if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
219219
tvars.extend([t for t in t.__parameters__ if t not in tvars])
220220
return tuple(tvars)
221221

@@ -268,7 +268,7 @@ def _remove_dups_flatten(parameters):
268268
# Flatten out Union[Union[...], ...].
269269
params = []
270270
for p in parameters:
271-
if isinstance(p, (_UnionGenericAlias, types.Union)):
271+
if isinstance(p, (_UnionGenericAlias, types.UnionType)):
272272
params.extend(p.__args__)
273273
elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
274274
params.extend(p[1:])
@@ -322,13 +322,13 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
322322
"""
323323
if isinstance(t, ForwardRef):
324324
return t._evaluate(globalns, localns, recursive_guard)
325-
if isinstance(t, (_GenericAlias, GenericAlias, types.Union)):
325+
if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
326326
ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
327327
if ev_args == t.__args__:
328328
return t
329329
if isinstance(t, GenericAlias):
330330
return GenericAlias(t.__origin__, ev_args)
331-
if isinstance(t, types.Union):
331+
if isinstance(t, types.UnionType):
332332
return functools.reduce(operator.or_, ev_args)
333333
else:
334334
return t.copy_with(ev_args)
@@ -1038,7 +1038,7 @@ def __getitem__(self, params):
10381038
for arg in self.__args__:
10391039
if isinstance(arg, self._typevar_types):
10401040
arg = subst[arg]
1041-
elif isinstance(arg, (_GenericAlias, GenericAlias, types.Union)):
1041+
elif isinstance(arg, (_GenericAlias, GenericAlias, types.UnionType)):
10421042
subparams = arg.__parameters__
10431043
if subparams:
10441044
subargs = tuple(subst[x] for x in subparams)
@@ -1206,7 +1206,7 @@ def copy_with(self, params):
12061206
return Union[params]
12071207

12081208
def __eq__(self, other):
1209-
if not isinstance(other, (_UnionGenericAlias, types.Union)):
1209+
if not isinstance(other, (_UnionGenericAlias, types.UnionType)):
12101210
return NotImplemented
12111211
return set(self.__args__) == set(other.__args__)
12121212

@@ -1810,7 +1810,7 @@ def _strip_annotations(t):
18101810
if stripped_args == t.__args__:
18111811
return t
18121812
return GenericAlias(t.__origin__, stripped_args)
1813-
if isinstance(t, types.Union):
1813+
if isinstance(t, types.UnionType):
18141814
stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
18151815
if stripped_args == t.__args__:
18161816
return t
@@ -1841,8 +1841,8 @@ def get_origin(tp):
18411841
return tp.__origin__
18421842
if tp is Generic:
18431843
return Generic
1844-
if isinstance(tp, types.Union):
1845-
return types.Union
1844+
if isinstance(tp, types.UnionType):
1845+
return types.UnionType
18461846
return None
18471847

18481848

@@ -1866,7 +1866,7 @@ def get_args(tp):
18661866
or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
18671867
res = (list(res[:-1]), res[-1])
18681868
return res
1869-
if isinstance(tp, types.Union):
1869+
if isinstance(tp, types.UnionType):
18701870
return tp.__args__
18711871
return ()
18721872

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)