From 2b8ad9e6c5f0a66e9ca2d15f85336d8a3eefefb0 Mon Sep 17 00:00:00 2001 From: Hasan Date: Mon, 26 Jul 2021 20:00:21 +0400 Subject: [PATCH] bpo-44732: Rename types.Union to types.UnionType (#27342) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ɓukasz Langa Co-authored-by: Jelle Zijlstra --- Doc/library/stdtypes.rst | 8 +++---- Doc/library/types.rst | 2 +- Lib/test/test_typing.py | 2 +- Lib/types.py | 3 +-- Lib/typing.py | 22 +++++++++---------- .../2021-07-26-15-27-03.bpo-44732.IxObt3.rst | 1 + Objects/unionobject.c | 6 ++--- 7 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2021-07-26-15-27-03.bpo-44732.IxObt3.rst diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index 3b2ff8090ce..87a343dbe42 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -5040,16 +5040,16 @@ enables cleaner type hinting syntax compared to :data:`typing.Union`. TypeError: isinstance() argument 2 cannot contain a parameterized generic 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:: >>> import types - >>> isinstance(int | str, types.Union) + >>> isinstance(int | str, types.UnionType) True - >>> types.Union() + >>> types.UnionType() Traceback (most recent call last): File "", line 1, in - TypeError: cannot create 'types.Union' instances + TypeError: cannot create 'types.UnionType' instances .. note:: The :meth:`__or__` method for type objects was added to support the syntax diff --git a/Doc/library/types.rst b/Doc/library/types.rst index 2dfc0f2fcad..88fe47a8d4a 100644 --- a/Doc/library/types.rst +++ b/Doc/library/types.rst @@ -312,7 +312,7 @@ Standard names are defined for the following types: This type can now be subclassed. -.. data:: Union +.. data:: UnionType The type of :ref:`union type expressions`. diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 85cb527a24d..3aed26bbfc3 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -3090,7 +3090,7 @@ class GetUtilitiesTestCase(TestCase): self.assertIs(get_origin(Callable), collections.abc.Callable) self.assertIs(get_origin(list[int]), list) 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.kwargs), P) diff --git a/Lib/types.py b/Lib/types.py index c665e6f8615..ce1c28d5625 100644 --- a/Lib/types.py +++ b/Lib/types.py @@ -297,9 +297,8 @@ def coroutine(func): return wrapped - GenericAlias = type(list[int]) -Union = type(int | str) +UnionType = type(int | str) EllipsisType = type(Ellipsis) NoneType = type(None) diff --git a/Lib/typing.py b/Lib/typing.py index 844cf97bfe1..f07b00f32ec 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -175,7 +175,7 @@ def _type_check(arg, msg, is_argument=True, module=None): return arg if isinstance(arg, _SpecialForm) or arg in (Generic, Protocol): 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 if not callable(arg): raise TypeError(f"{msg} Got {arg!r:.100}.") @@ -215,7 +215,7 @@ def _collect_type_vars(types_, typevar_types=None): for t in types_: if isinstance(t, typevar_types) and t not in tvars: 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]) return tuple(tvars) @@ -268,7 +268,7 @@ def _remove_dups_flatten(parameters): # Flatten out Union[Union[...], ...]. params = [] for p in parameters: - if isinstance(p, (_UnionGenericAlias, types.Union)): + if isinstance(p, (_UnionGenericAlias, types.UnionType)): params.extend(p.__args__) elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union: params.extend(p[1:]) @@ -322,13 +322,13 @@ def _eval_type(t, globalns, localns, recursive_guard=frozenset()): """ if isinstance(t, ForwardRef): 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__) if ev_args == t.__args__: return t if isinstance(t, GenericAlias): return GenericAlias(t.__origin__, ev_args) - if isinstance(t, types.Union): + if isinstance(t, types.UnionType): return functools.reduce(operator.or_, ev_args) else: return t.copy_with(ev_args) @@ -1038,7 +1038,7 @@ class _GenericAlias(_BaseGenericAlias, _root=True): for arg in self.__args__: if isinstance(arg, self._typevar_types): arg = subst[arg] - elif isinstance(arg, (_GenericAlias, GenericAlias, types.Union)): + elif isinstance(arg, (_GenericAlias, GenericAlias, types.UnionType)): subparams = arg.__parameters__ if subparams: subargs = tuple(subst[x] for x in subparams) @@ -1206,7 +1206,7 @@ class _UnionGenericAlias(_GenericAlias, _root=True): return Union[params] def __eq__(self, other): - if not isinstance(other, (_UnionGenericAlias, types.Union)): + if not isinstance(other, (_UnionGenericAlias, types.UnionType)): return NotImplemented return set(self.__args__) == set(other.__args__) @@ -1810,7 +1810,7 @@ def _strip_annotations(t): if stripped_args == t.__args__: return t 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__) if stripped_args == t.__args__: return t @@ -1841,8 +1841,8 @@ def get_origin(tp): return tp.__origin__ if tp is Generic: return Generic - if isinstance(tp, types.Union): - return types.Union + if isinstance(tp, types.UnionType): + return types.UnionType return None @@ -1866,7 +1866,7 @@ def get_args(tp): or isinstance(res[0], (ParamSpec, _ConcatenateGenericAlias)))): res = (list(res[:-1]), res[-1]) return res - if isinstance(tp, types.Union): + if isinstance(tp, types.UnionType): return tp.__args__ return () diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-26-15-27-03.bpo-44732.IxObt3.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-26-15-27-03.bpo-44732.IxObt3.rst new file mode 100644 index 00000000000..5094688a294 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-26-15-27-03.bpo-44732.IxObt3.rst @@ -0,0 +1 @@ +Rename ``types.Union`` to ``types.UnionType``. diff --git a/Objects/unionobject.c b/Objects/unionobject.c index 475311e598d..9e670b47b7e 100644 --- a/Objects/unionobject.c +++ b/Objects/unionobject.c @@ -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 "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK #include "pycore_unionobject.h" @@ -414,7 +414,7 @@ union_parameters(PyObject *self, void *Py_UNUSED(unused)) } 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} }; @@ -424,7 +424,7 @@ static PyNumberMethods union_as_number = { PyTypeObject _PyUnion_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) - .tp_name = "types.Union", + .tp_name = "types.UnionType", .tp_doc = "Represent a PEP 604 union type\n" "\n" "E.g. for int | str",