diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2021-07-15 07:15:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-15 07:15:14 (GMT) |
commit | 6dec5255829a31826990ea40ca106cc496570df2 (patch) | |
tree | 39654b3a31b3d56ff680feb5880413576105d353 | |
parent | 016af14d93cfba43e7a95721a97fa954c534af8e (diff) | |
download | cpython-6dec5255829a31826990ea40ca106cc496570df2.zip cpython-6dec5255829a31826990ea40ca106cc496570df2.tar.gz cpython-6dec5255829a31826990ea40ca106cc496570df2.tar.bz2 |
[3.10] bpo-44635: Convert None to NoneType in the union type constructor (GH-27136). (GH-27142)
(cherry picked from commit b81cac05606c84958b52ada09f690463a3c7e949)
-rw-r--r-- | Lib/test/test_types.py | 13 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2021-07-14-13-54-07.bpo-44635.7ZMAdB.rst | 1 | ||||
-rw-r--r-- | Objects/unionobject.c | 16 |
3 files changed, 21 insertions, 9 deletions
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 0e1a242..5254d58 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -612,6 +612,8 @@ class TypesTests(unittest.TestCase): self.assertEqual(str | int, typing.Union[int, str]) self.assertEqual(int | None, typing.Union[int, None]) self.assertEqual(None | int, typing.Union[int, None]) + self.assertEqual(int | type(None), int | None) + self.assertEqual(type(None) | int, None | int) self.assertEqual(int | str | list, typing.Union[int, str, list]) self.assertEqual(int | (str | list), typing.Union[int, str, list]) self.assertEqual(str | (int | list), typing.Union[int, str, list]) @@ -699,6 +701,13 @@ class TypesTests(unittest.TestCase): assert TV | str == typing.Union[TV, str] assert str | TV == typing.Union[str, TV] + def test_union_args(self): + self.assertEqual((int | str).__args__, (int, str)) + self.assertEqual(((int | str) | list).__args__, (int, str, list)) + self.assertEqual((int | (str | list)).__args__, (int, str, list)) + self.assertEqual((int | None).__args__, (int, type(None))) + self.assertEqual((int | type(None)).__args__, (int, type(None))) + def test_or_type_operator_with_forward(self): T = typing.TypeVar('T') ForwardAfter = T | 'Forward' @@ -744,7 +753,11 @@ class TypesTests(unittest.TestCase): assert typing.Union[int, bool] | str == typing.Union[int, bool, str] def test_or_type_repr(self): + assert repr(int | str) == "int | str" + assert repr((int | str) | list) == "int | str | list" + assert repr(int | (str | list)) == "int | str | list" assert repr(int | None) == "int | None" + assert repr(int | type(None)) == "int | None" assert repr(int | typing.GenericAlias(list, int)) == "int | list[int]" def test_or_type_operator_with_genericalias(self): diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-14-13-54-07.bpo-44635.7ZMAdB.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-14-13-54-07.bpo-44635.7ZMAdB.rst new file mode 100644 index 0000000..ea00554 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-14-13-54-07.bpo-44635.7ZMAdB.rst @@ -0,0 +1 @@ +Convert ``None`` to ``type(None)`` in the union type constructor. diff --git a/Objects/unionobject.c b/Objects/unionobject.c index bcba0f6..98db27d 100644 --- a/Objects/unionobject.c +++ b/Objects/unionobject.c @@ -64,9 +64,6 @@ union_instancecheck(PyObject *self, PyObject *instance) } for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg); - if (arg == Py_None) { - arg = (PyObject *)&_PyNone_Type; - } if (PyType_Check(arg)) { int res = PyObject_IsInstance(instance, arg); if (res < 0) { @@ -96,9 +93,6 @@ union_subclasscheck(PyObject *self, PyObject *instance) Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args); for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) { PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg); - if (arg == Py_None) { - arg = (PyObject *)&_PyNone_Type; - } if (PyType_Check(arg)) { int res = PyObject_IsSubclass(instance, arg); if (res < 0) { @@ -172,9 +166,6 @@ union_richcompare(PyObject *a, PyObject *b, int op) Py_ssize_t b_arg_length = PyTuple_GET_SIZE(b_args); for (Py_ssize_t i = 0; i < b_arg_length; i++) { PyObject* arg = PyTuple_GET_ITEM(b_args, i); - if (arg == (PyObject *)&_PyNone_Type) { - arg = Py_None; - } if (PySet_Add(b_set, arg) == -1) { Py_DECREF(b_args); goto exit; @@ -236,6 +227,9 @@ flatten_args(PyObject* args) pos++; } } else { + if (arg == Py_None) { + arg = (PyObject *)&_PyNone_Type; + } Py_INCREF(arg); PyTuple_SET_ITEM(flattened_args, pos, arg); pos++; @@ -362,6 +356,10 @@ union_repr_item(_PyUnicodeWriter *writer, PyObject *p) PyObject *r = NULL; int err; + if (p == (PyObject *)&_PyNone_Type) { + return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4); + } + if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) { goto exit; } |