self.assertEqual(Union[Literal[1], Literal[Ints.B], Literal[True]].__args__,
(Literal[1], Literal[Ints.B], Literal[True]))
+ def test_allow_non_types_in_or(self):
+ # gh-140348: Test that using | with a Union object allows things that are
+ # not allowed by is_unionable().
+ U1 = Union[int, str]
+ self.assertEqual(U1 | float, Union[int, str, float])
+ self.assertEqual(U1 | "float", Union[int, str, "float"])
+ self.assertEqual(float | U1, Union[float, int, str])
+ self.assertEqual("float" | U1, Union["float", int, str])
+
class TupleTests(BaseTestCase):
{0}
};
+static PyObject *
+union_nb_or(PyObject *a, PyObject *b)
+{
+ unionbuilder ub;
+ if (!unionbuilder_init(&ub, true)) {
+ return NULL;
+ }
+ if (!unionbuilder_add_single(&ub, a) ||
+ !unionbuilder_add_single(&ub, b)) {
+ unionbuilder_finalize(&ub);
+ return NULL;
+ }
+ return make_union(&ub);
+}
+
static PyNumberMethods union_as_number = {
- .nb_or = _Py_union_type_or, // Add __or__ function
+ .nb_or = union_nb_or, // Add __or__ function
};
static const char* const cls_attrs[] = {