Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-110782: Fix crash when TypeVar is constructed with keyword args
  • Loading branch information
JelleZijlstra committed Oct 12, 2023
commit fb598e7568ce390aeedaaa3b03fb68d474c279c0
6 changes: 6 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,12 @@ def test_many_weakrefs(self):
vals[x] = cls(str(x))
del vals

def test_constructor(self):
T = TypeVar(name="T")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this form offensive, but since we allowed it in 3.11 I guess we have to keep supporting it, and we can't just make it an error (not crashing of course). (It's especially weird since a type var with constraints requires the name to be positional.)

self.assertEqual(T.__name__, "T")
self.assertEqual(T.__constraints__, ())
self.assertIs(T.__bound__, None)


def template_replace(templates: list[str], replacements: dict[str, list[str]]) -> list[tuple[str]]:
"""Renders templates with possible combinations of replacements.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash when :class:`typing.TypeVar` is constructed with a keyword
argument. Patch by Jelle Zijlstra.
38 changes: 20 additions & 18 deletions Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,24 +364,26 @@ typevar_new_impl(PyTypeObject *type, PyObject *name, PyObject *constraints,
}
}

if (!PyTuple_CheckExact(constraints)) {
PyErr_SetString(PyExc_TypeError,
"constraints must be a tuple");
return NULL;
}
Py_ssize_t n_constraints = PyTuple_GET_SIZE(constraints);
if (n_constraints == 1) {
PyErr_SetString(PyExc_TypeError,
"A single constraint is not allowed");
Py_XDECREF(bound);
return NULL;
} else if (n_constraints == 0) {
constraints = NULL;
} else if (bound != NULL) {
PyErr_SetString(PyExc_TypeError,
"Constraints cannot be combined with bound=...");
Py_XDECREF(bound);
return NULL;
if (constraints != NULL) {
if (!PyTuple_CheckExact(constraints)) {
PyErr_SetString(PyExc_TypeError,
"constraints must be a tuple");
return NULL;
}
Py_ssize_t n_constraints = PyTuple_GET_SIZE(constraints);
if (n_constraints == 1) {
PyErr_SetString(PyExc_TypeError,
"A single constraint is not allowed");
Py_XDECREF(bound);
return NULL;
} else if (n_constraints == 0) {
constraints = NULL;
} else if (bound != NULL) {
PyErr_SetString(PyExc_TypeError,
"Constraints cannot be combined with bound=...");
Py_XDECREF(bound);
return NULL;
}
}
PyObject *module = caller();
if (module == NULL) {
Expand Down