-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-44653: Support typing types in parameter substitution in the union type. #27247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support :mod:`typing` types in parameter substitution in the union type. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
This comment was marked as resolved.
Sorry, something went wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it has quadratic complexity. But I think that most union types has less than 10 args. If it be a problem (many thousands args in real code) we can add some optimizations, but for now I prefer simplicity. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point! Old union de-duplication also had quadratic complexity. So I think it's ok for now. Especially since this is only for substitution, which isn't a common use case either. |
||
if (res == NULL) { | ||
break; | ||
} | ||
return NULL; | ||
} | ||
} | ||
|
||
PyObject *res = make_union(newargs); | ||
|
||
Py_DECREF(newargs); | ||
return res; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure that this situation is possible now, but just for the case...