mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
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 2b8ad9e6c5
)
Co-authored-by: Hasan <hasan.aleeyev@gmail.com>
This commit is contained in:
parent
16a174f7ba
commit
8a37e8cf45
7 changed files with 22 additions and 22 deletions
|
@ -5040,16 +5040,16 @@ enables cleaner type hinting syntax compared to :data:`typing.Union`.
|
||||||
TypeError: isinstance() argument 2 cannot contain a parameterized generic
|
TypeError: isinstance() argument 2 cannot contain a parameterized generic
|
||||||
|
|
||||||
The user-exposed type for the union object can be accessed from
|
The user-exposed type for the union object can be accessed from
|
||||||
:data:`types.Union` and used for :func:`isinstance` checks. An object cannot be
|
:data:`types.UnionType` and used for :func:`isinstance` checks. An object cannot be
|
||||||
instantiated from the type::
|
instantiated from the type::
|
||||||
|
|
||||||
>>> import types
|
>>> import types
|
||||||
>>> isinstance(int | str, types.Union)
|
>>> isinstance(int | str, types.UnionType)
|
||||||
True
|
True
|
||||||
>>> types.Union()
|
>>> types.UnionType()
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
File "<stdin>", line 1, in <module>
|
File "<stdin>", line 1, in <module>
|
||||||
TypeError: cannot create 'types.Union' instances
|
TypeError: cannot create 'types.UnionType' instances
|
||||||
|
|
||||||
.. note::
|
.. note::
|
||||||
The :meth:`__or__` method for type objects was added to support the syntax
|
The :meth:`__or__` method for type objects was added to support the syntax
|
||||||
|
|
|
@ -312,7 +312,7 @@ Standard names are defined for the following types:
|
||||||
This type can now be subclassed.
|
This type can now be subclassed.
|
||||||
|
|
||||||
|
|
||||||
.. data:: Union
|
.. data:: UnionType
|
||||||
|
|
||||||
The type of :ref:`union type expressions<types-union>`.
|
The type of :ref:`union type expressions<types-union>`.
|
||||||
|
|
||||||
|
|
|
@ -3084,7 +3084,7 @@ class GetUtilitiesTestCase(TestCase):
|
||||||
self.assertIs(get_origin(Callable), collections.abc.Callable)
|
self.assertIs(get_origin(Callable), collections.abc.Callable)
|
||||||
self.assertIs(get_origin(list[int]), list)
|
self.assertIs(get_origin(list[int]), list)
|
||||||
self.assertIs(get_origin(list), None)
|
self.assertIs(get_origin(list), None)
|
||||||
self.assertIs(get_origin(list | str), types.Union)
|
self.assertIs(get_origin(list | str), types.UnionType)
|
||||||
self.assertIs(get_origin(P.args), P)
|
self.assertIs(get_origin(P.args), P)
|
||||||
self.assertIs(get_origin(P.kwargs), P)
|
self.assertIs(get_origin(P.kwargs), P)
|
||||||
|
|
||||||
|
|
|
@ -297,9 +297,8 @@ def coroutine(func):
|
||||||
|
|
||||||
return wrapped
|
return wrapped
|
||||||
|
|
||||||
|
|
||||||
GenericAlias = type(list[int])
|
GenericAlias = type(list[int])
|
||||||
Union = type(int | str)
|
UnionType = type(int | str)
|
||||||
|
|
||||||
EllipsisType = type(Ellipsis)
|
EllipsisType = type(Ellipsis)
|
||||||
NoneType = type(None)
|
NoneType = type(None)
|
||||||
|
|
|
@ -167,7 +167,7 @@ def _type_check(arg, msg, is_argument=True, module=None):
|
||||||
return arg
|
return arg
|
||||||
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
|
if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol):
|
||||||
raise TypeError(f"Plain {arg} is not valid as type argument")
|
raise TypeError(f"Plain {arg} is not valid as type argument")
|
||||||
if isinstance(arg, (type, TypeVar, ForwardRef, types.Union, ParamSpec)):
|
if isinstance(arg, (type, TypeVar, ForwardRef, types.UnionType, ParamSpec)):
|
||||||
return arg
|
return arg
|
||||||
if not callable(arg):
|
if not callable(arg):
|
||||||
raise TypeError(f"{msg} Got {arg!r:.100}.")
|
raise TypeError(f"{msg} Got {arg!r:.100}.")
|
||||||
|
@ -207,7 +207,7 @@ def _collect_type_vars(types_, typevar_types=None):
|
||||||
for t in types_:
|
for t in types_:
|
||||||
if isinstance(t, typevar_types) and t not in tvars:
|
if isinstance(t, typevar_types) and t not in tvars:
|
||||||
tvars.append(t)
|
tvars.append(t)
|
||||||
if isinstance(t, (_GenericAlias, GenericAlias, types.Union)):
|
if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
|
||||||
tvars.extend([t for t in t.__parameters__ if t not in tvars])
|
tvars.extend([t for t in t.__parameters__ if t not in tvars])
|
||||||
return tuple(tvars)
|
return tuple(tvars)
|
||||||
|
|
||||||
|
@ -260,7 +260,7 @@ def _remove_dups_flatten(parameters):
|
||||||
# Flatten out Union[Union[...], ...].
|
# Flatten out Union[Union[...], ...].
|
||||||
params = []
|
params = []
|
||||||
for p in parameters:
|
for p in parameters:
|
||||||
if isinstance(p, (_UnionGenericAlias, types.Union)):
|
if isinstance(p, (_UnionGenericAlias, types.UnionType)):
|
||||||
params.extend(p.__args__)
|
params.extend(p.__args__)
|
||||||
elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
|
elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
|
||||||
params.extend(p[1:])
|
params.extend(p[1:])
|
||||||
|
@ -314,13 +314,13 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()):
|
||||||
"""
|
"""
|
||||||
if isinstance(t, ForwardRef):
|
if isinstance(t, ForwardRef):
|
||||||
return t._evaluate(globalns, localns, recursive_guard)
|
return t._evaluate(globalns, localns, recursive_guard)
|
||||||
if isinstance(t, (_GenericAlias, GenericAlias, types.Union)):
|
if isinstance(t, (_GenericAlias, GenericAlias, types.UnionType)):
|
||||||
ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
|
ev_args = tuple(_eval_type(a, globalns, localns, recursive_guard) for a in t.__args__)
|
||||||
if ev_args == t.__args__:
|
if ev_args == t.__args__:
|
||||||
return t
|
return t
|
||||||
if isinstance(t, GenericAlias):
|
if isinstance(t, GenericAlias):
|
||||||
return GenericAlias(t.__origin__, ev_args)
|
return GenericAlias(t.__origin__, ev_args)
|
||||||
if isinstance(t, types.Union):
|
if isinstance(t, types.UnionType):
|
||||||
return functools.reduce(operator.or_, ev_args)
|
return functools.reduce(operator.or_, ev_args)
|
||||||
else:
|
else:
|
||||||
return t.copy_with(ev_args)
|
return t.copy_with(ev_args)
|
||||||
|
@ -1030,7 +1030,7 @@ class _GenericAlias(_BaseGenericAlias, _root=True):
|
||||||
for arg in self.__args__:
|
for arg in self.__args__:
|
||||||
if isinstance(arg, self._typevar_types):
|
if isinstance(arg, self._typevar_types):
|
||||||
arg = subst[arg]
|
arg = subst[arg]
|
||||||
elif isinstance(arg, (_GenericAlias, GenericAlias, types.Union)):
|
elif isinstance(arg, (_GenericAlias, GenericAlias, types.UnionType)):
|
||||||
subparams = arg.__parameters__
|
subparams = arg.__parameters__
|
||||||
if subparams:
|
if subparams:
|
||||||
subargs = tuple(subst[x] for x in subparams)
|
subargs = tuple(subst[x] for x in subparams)
|
||||||
|
@ -1198,7 +1198,7 @@ class _UnionGenericAlias(_GenericAlias, _root=True):
|
||||||
return Union[params]
|
return Union[params]
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
if not isinstance(other, (_UnionGenericAlias, types.Union)):
|
if not isinstance(other, (_UnionGenericAlias, types.UnionType)):
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return set(self.__args__) == set(other.__args__)
|
return set(self.__args__) == set(other.__args__)
|
||||||
|
|
||||||
|
@ -1802,7 +1802,7 @@ def _strip_annotations(t):
|
||||||
if stripped_args == t.__args__:
|
if stripped_args == t.__args__:
|
||||||
return t
|
return t
|
||||||
return GenericAlias(t.__origin__, stripped_args)
|
return GenericAlias(t.__origin__, stripped_args)
|
||||||
if isinstance(t, types.Union):
|
if isinstance(t, types.UnionType):
|
||||||
stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
|
stripped_args = tuple(_strip_annotations(a) for a in t.__args__)
|
||||||
if stripped_args == t.__args__:
|
if stripped_args == t.__args__:
|
||||||
return t
|
return t
|
||||||
|
@ -1833,8 +1833,8 @@ def get_origin(tp):
|
||||||
return tp.__origin__
|
return tp.__origin__
|
||||||
if tp is Generic:
|
if tp is Generic:
|
||||||
return Generic
|
return Generic
|
||||||
if isinstance(tp, types.Union):
|
if isinstance(tp, types.UnionType):
|
||||||
return types.Union
|
return types.UnionType
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
@ -1858,7 +1858,7 @@ def get_args(tp):
|
||||||
or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
|
or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))):
|
||||||
res = (list(res[:-1]), res[-1])
|
res = (list(res[:-1]), res[-1])
|
||||||
return res
|
return res
|
||||||
if isinstance(tp, types.Union):
|
if isinstance(tp, types.UnionType):
|
||||||
return tp.__args__
|
return tp.__args__
|
||||||
return ()
|
return ()
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Rename ``types.Union`` to ``types.UnionType``.
|
|
@ -1,4 +1,4 @@
|
||||||
// types.Union -- used to represent e.g. Union[int, str], int | str
|
// types.UnionType -- used to represent e.g. Union[int, str], int | str
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
|
#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
|
||||||
#include "pycore_unionobject.h"
|
#include "pycore_unionobject.h"
|
||||||
|
@ -414,7 +414,7 @@ union_parameters(PyObject *self, void *Py_UNUSED(unused))
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyGetSetDef union_properties[] = {
|
static PyGetSetDef union_properties[] = {
|
||||||
{"__parameters__", union_parameters, (setter)NULL, "Type variables in the types.Union.", NULL},
|
{"__parameters__", union_parameters, (setter)NULL, "Type variables in the types.UnionType.", NULL},
|
||||||
{0}
|
{0}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -424,7 +424,7 @@ static PyNumberMethods union_as_number = {
|
||||||
|
|
||||||
PyTypeObject _PyUnion_Type = {
|
PyTypeObject _PyUnion_Type = {
|
||||||
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
||||||
.tp_name = "types.Union",
|
.tp_name = "types.UnionType",
|
||||||
.tp_doc = "Represent a PEP 604 union type\n"
|
.tp_doc = "Represent a PEP 604 union type\n"
|
||||||
"\n"
|
"\n"
|
||||||
"E.g. for int | str",
|
"E.g. for int | str",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue