[Python-checkins] [3.10] bpo-44636: Collapse union of equal types (GH-27178) (GH-27181)
serhiy-storchaka
webhook-mailer at python.org
Fri Jul 16 07:48:24 EDT 2021
https://github.com/python/cpython/commit/c3007ab3c6cb384203bac8aa64d89c4b42f671a1
commit: c3007ab3c6cb384203bac8aa64d89c4b42f671a1
branch: 3.10
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2021-07-16T14:48:20+03:00
summary:
[3.10] bpo-44636: Collapse union of equal types (GH-27178) (GH-27181)
The result of `int | int` is now `int`.
Fix comparison of the union type with non-hashable objects.
`int | str == {}` no longer raises a TypeError.
(cherry picked from commit d9f923280f204204f8703756aef4f655b579b4b8)
files:
A Misc/NEWS.d/next/Core and Builtins/2021-07-16-09-36-12.bpo-44636.ZWebi8.rst
M Lib/test/test_types.py
M Objects/unionobject.c
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index bb504122ba796..8db9880e1faf5 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -623,7 +623,7 @@ def test_or_types_operator(self):
self.assertEqual(None | typing.List[int], typing.Union[None, typing.List[int]])
self.assertEqual(str | float | int | complex | int, (int | str) | (float | complex))
self.assertEqual(typing.Union[str, int, typing.List[int]], str | int | typing.List[int])
- self.assertEqual(int | int, int)
+ self.assertIs(int | int, int)
self.assertEqual(
BaseException |
bool |
@@ -651,6 +651,8 @@ def test_or_types_operator(self):
3 | int
with self.assertRaises(TypeError):
Example() | int
+ x = int | str
+ self.assertNotEqual(x, {})
with self.assertRaises(TypeError):
(int | str) < typing.Union[str, int]
with self.assertRaises(TypeError):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-16-09-36-12.bpo-44636.ZWebi8.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-16-09-36-12.bpo-44636.ZWebi8.rst
new file mode 100644
index 0000000000000..1a053ae69e97e
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-07-16-09-36-12.bpo-44636.ZWebi8.rst
@@ -0,0 +1 @@
+Collapse union of equal types. E.g. the result of ``int | int`` is now ``int``. Fix comparison of the union type with non-hashable objects. E.g. ``int | str == {}`` no longer raises a TypeError.
diff --git a/Objects/unionobject.c b/Objects/unionobject.c
index 762df5d9780a3..f3e0aa6c62d1e 100644
--- a/Objects/unionobject.c
+++ b/Objects/unionobject.c
@@ -184,9 +184,9 @@ union_richcompare(PyObject *a, PyObject *b, int op)
}
}
} else {
- if (PySet_Add(b_set, b) == -1) {
- goto exit;
- }
+ Py_DECREF(a_set);
+ Py_DECREF(b_set);
+ Py_RETURN_NOTIMPLEMENTED;
}
result = PyObject_RichCompare(a_set, b_set, op);
exit:
@@ -499,16 +499,24 @@ _Py_Union(PyObject *args)
}
}
+ args = dedup_and_flatten_args(args);
+ if (args == NULL) {
+ return NULL;
+ }
+ if (PyTuple_GET_SIZE(args) == 1) {
+ PyObject *result1 = PyTuple_GET_ITEM(args, 0);
+ Py_INCREF(result1);
+ Py_DECREF(args);
+ return result1;
+ }
+
result = PyObject_GC_New(unionobject, &_Py_UnionType);
if (result == NULL) {
+ Py_DECREF(args);
return NULL;
}
- result->args = dedup_and_flatten_args(args);
+ result->args = args;
_PyObject_GC_TRACK(result);
- if (result->args == NULL) {
- Py_DECREF(result);
- return NULL;
- }
return (PyObject*)result;
}
More information about the Python-checkins
mailing list