]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-44633: Fix parameter substitution of the union type with wrong types. (GH-27218)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 18 Jul 2021 09:10:19 +0000 (12:10 +0300)
committerGitHub <noreply@github.com>
Sun, 18 Jul 2021 09:10:19 +0000 (12:10 +0300)
A TypeError is now raised instead of returning NotImplemented.

Lib/test/test_types.py
Misc/NEWS.d/next/Core and Builtins/2021-07-17-21-04-04.bpo-44633.5-zKeI.rst [new file with mode: 0644]
Objects/unionobject.c

index b2e11308f3e80da7207e13b3d89d625f7eb326bd..6ed74714dc578ebbf8af8ad31977a2fa631e79c5 100644 (file)
@@ -772,6 +772,12 @@ class UnionTests(unittest.TestCase):
         self.assertEqual((list[T] | list[S])[int, T], list[int] | list[T])
         self.assertEqual((list[T] | list[S])[int, int], list[int])
 
+    def test_union_parameter_substitution_errors(self):
+        T = typing.TypeVar("T")
+        x = int | T
+        with self.assertRaises(TypeError):
+            x[42]
+
     def test_or_type_operator_with_forward(self):
         T = typing.TypeVar('T')
         ForwardAfter = T | 'Forward'
diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-07-17-21-04-04.bpo-44633.5-zKeI.rst b/Misc/NEWS.d/next/Core and Builtins/2021-07-17-21-04-04.bpo-44633.5-zKeI.rst
new file mode 100644 (file)
index 0000000..507a68b
--- /dev/null
@@ -0,0 +1,2 @@
+Parameter substitution of the union type with wrong types now raises
+``TypeError`` instead of returning ``NotImplemented``.
index c744c8746cb4d7c52cb5c098939b5d217eded97c..c0c9a24bcc204a09a6f272fc401654d67d0e9783 100644 (file)
@@ -302,10 +302,22 @@ is_unionable(PyObject *obj)
 PyObject *
 _Py_union_type_or(PyObject* self, PyObject* other)
 {
+    int r = is_unionable(self);
+    if (r > 0) {
+        r = is_unionable(other);
+    }
+    if (r < 0) {
+        return NULL;
+    }
+    if (!r) {
+        Py_RETURN_NOTIMPLEMENTED;
+    }
+
     PyObject *tuple = PyTuple_Pack(2, self, other);
     if (tuple == NULL) {
         return NULL;
     }
+
     PyObject *new_union = make_union(tuple);
     Py_DECREF(tuple);
     return new_union;
@@ -434,6 +446,21 @@ union_getitem(PyObject *self, PyObject *item)
         return NULL;
     }
 
+    // Check arguments are unionable.
+    Py_ssize_t nargs = PyTuple_GET_SIZE(newargs);
+    for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
+        PyObject *arg = PyTuple_GET_ITEM(newargs, iarg);
+        int is_arg_unionable = is_unionable(arg);
+        if (is_arg_unionable <= 0) {
+            Py_DECREF(newargs);
+            if (is_arg_unionable == 0) {
+                PyErr_Format(PyExc_TypeError,
+                             "Each union argument must be a type, got %.100R", arg);
+            }
+            return NULL;
+        }
+    }
+
     PyObject *res = make_union(newargs);
 
     Py_DECREF(newargs);
@@ -495,21 +522,6 @@ make_union(PyObject *args)
 {
     assert(PyTuple_CheckExact(args));
 
-    unionobject* result = NULL;
-
-    // Check arguments are unionable.
-    Py_ssize_t nargs = PyTuple_GET_SIZE(args);
-    for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
-        PyObject *arg = PyTuple_GET_ITEM(args, iarg);
-        int is_arg_unionable = is_unionable(arg);
-        if (is_arg_unionable < 0) {
-            return NULL;
-        }
-        if (!is_arg_unionable) {
-            Py_RETURN_NOTIMPLEMENTED;
-        }
-    }
-
     args = dedup_and_flatten_args(args);
     if (args == NULL) {
         return NULL;
@@ -521,7 +533,7 @@ make_union(PyObject *args)
         return result1;
     }
 
-    result = PyObject_GC_New(unionobject, &_PyUnion_Type);
+    unionobject *result = PyObject_GC_New(unionobject, &_PyUnion_Type);
     if (result == NULL) {
         Py_DECREF(args);
         return NULL;