bpo-44653: Support typing types in parameter substitution in the union type. (GH-27247)

This commit is contained in:
Serhiy Storchaka 2021-07-23 00:57:06 +03:00 committed by GitHub
parent 96c4cbd96c
commit 2e3744d50b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 13 deletions

View file

@ -446,23 +446,22 @@ union_getitem(PyObject *self, PyObject *item)
return NULL;
}
// Check arguments are unionable.
PyObject *res;
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);
if (nargs == 0) {
res = make_union(newargs);
}
else {
res = PyTuple_GET_ITEM(newargs, 0);
Py_INCREF(res);
for (Py_ssize_t iarg = 1; iarg < nargs; iarg++) {
PyObject *arg = PyTuple_GET_ITEM(newargs, iarg);
Py_SETREF(res, PyNumber_Or(res, arg));
if (res == NULL) {
break;
}
return NULL;
}
}
PyObject *res = make_union(newargs);
Py_DECREF(newargs);
return res;
}