Skip to content

Commit

Permalink
[3.12] pythongh-118814: Fix the TypeVar constructor when name is pass…
Browse files Browse the repository at this point in the history
…ed by keyword (pythonGH-122664)

Fix _PyArg_UnpackKeywordsWithVararg for the case when argument for
positional-or-keyword parameter is passed by keyword.
There was only one such case in the stdlib -- the TypeVar constructor.
(cherry picked from commit 540fcc6)

Co-authored-by: Serhiy Storchaka <[email protected]>
  • Loading branch information
serhiy-storchaka committed Aug 7, 2024
1 parent 8f4892a commit 87c6e9d
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 7 deletions.
26 changes: 21 additions & 5 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2093,11 +2093,27 @@ def test_vararg(self):
self.assertEqual(ac_tester.vararg(1, 2, 3, 4), (1, (2, 3, 4)))

def test_vararg_with_default(self):
with self.assertRaises(TypeError):
ac_tester.vararg_with_default()
self.assertEqual(ac_tester.vararg_with_default(1, b=False), (1, (), False))
self.assertEqual(ac_tester.vararg_with_default(1, 2, 3, 4), (1, (2, 3, 4), False))
self.assertEqual(ac_tester.vararg_with_default(1, 2, 3, 4, b=True), (1, (2, 3, 4), True))
fn = ac_tester.vararg_with_default
self.assertRaises(TypeError, fn)
self.assertRaises(TypeError, fn, 1, a=2)
self.assertEqual(fn(1, b=2), (1, (), True))
self.assertEqual(fn(1, 2, 3, 4), (1, (2, 3, 4), False))
self.assertEqual(fn(1, 2, 3, 4, b=5), (1, (2, 3, 4), True))
self.assertEqual(fn(a=1), (1, (), False))
self.assertEqual(fn(a=1, b=2), (1, (), True))

def test_vararg_with_default2(self):
fn = ac_tester.vararg_with_default2
self.assertRaises(TypeError, fn)
self.assertRaises(TypeError, fn, 1, a=2)
self.assertEqual(fn(1, b=2), (1, (), 2, None))
self.assertEqual(fn(1, b=2, c=3), (1, (), 2, 3))
self.assertEqual(fn(1, 2, 3), (1, (2, 3), None, None))
self.assertEqual(fn(1, 2, 3, b=4), (1, (2, 3), 4, None))
self.assertEqual(fn(1, 2, 3, b=4, c=5), (1, (2, 3), 4, 5))
self.assertEqual(fn(a=1), (1, (), None, None))
self.assertEqual(fn(a=1, b=2), (1, (), 2, None))
self.assertEqual(fn(a=1, b=2, c=3), (1, (), 2, 3))

def test_vararg_with_only_defaults(self):
self.assertEqual(ac_tester.vararg_with_only_defaults(), ((), None))
Expand Down
35 changes: 35 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,41 @@ def test_constructor(self):
self.assertEqual(T.__name__, "T")
self.assertEqual(T.__constraints__, ())
self.assertIs(T.__bound__, None)
self.assertIs(T.__covariant__, False)
self.assertIs(T.__contravariant__, False)
self.assertIs(T.__infer_variance__, False)

T = TypeVar(name="T", bound=type)
self.assertEqual(T.__name__, "T")
self.assertEqual(T.__constraints__, ())
self.assertIs(T.__bound__, type)
self.assertIs(T.__covariant__, False)
self.assertIs(T.__contravariant__, False)
self.assertIs(T.__infer_variance__, False)

T = TypeVar(name="T", covariant=True)
self.assertEqual(T.__name__, "T")
self.assertEqual(T.__constraints__, ())
self.assertIs(T.__bound__, None)
self.assertIs(T.__covariant__, True)
self.assertIs(T.__contravariant__, False)
self.assertIs(T.__infer_variance__, False)

T = TypeVar(name="T", contravariant=True)
self.assertEqual(T.__name__, "T")
self.assertEqual(T.__constraints__, ())
self.assertIs(T.__bound__, None)
self.assertIs(T.__covariant__, False)
self.assertIs(T.__contravariant__, True)
self.assertIs(T.__infer_variance__, False)

T = TypeVar(name="T", infer_variance=True)
self.assertEqual(T.__name__, "T")
self.assertEqual(T.__constraints__, ())
self.assertIs(T.__bound__, None)
self.assertIs(T.__covariant__, False)
self.assertIs(T.__contravariant__, False)
self.assertIs(T.__infer_variance__, True)


def template_replace(templates: list[str], replacements: dict[str, list[str]]) -> list[tuple[str]]:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the :class:`typing.TypeVar` constructor when name is passed by keyword.
20 changes: 20 additions & 0 deletions Modules/_testclinic.c
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,25 @@ vararg_with_default_impl(PyObject *module, PyObject *a, PyObject *args,
}


/*[clinic input]
vararg_with_default2
a: object
*args: object
b: object = None
c: object = None
[clinic start generated code]*/

static PyObject *
vararg_with_default2_impl(PyObject *module, PyObject *a, PyObject *args,
PyObject *b, PyObject *c)
/*[clinic end generated code: output=a0fb7c37796e2129 input=59fb22f5f0a8925f]*/
{
return pack_arguments_newref(4, a, args, b, c);
}


/*[clinic input]
vararg_with_only_defaults
Expand Down Expand Up @@ -1274,6 +1293,7 @@ static PyMethodDef tester_methods[] = {
VARARG_AND_POSONLY_METHODDEF
VARARG_METHODDEF
VARARG_WITH_DEFAULT_METHODDEF
VARARG_WITH_DEFAULT2_METHODDEF
VARARG_WITH_ONLY_DEFAULTS_METHODDEF
GH_32092_OOB_METHODDEF
GH_32092_KW_PASS_METHODDEF
Expand Down
74 changes: 73 additions & 1 deletion Modules/clinic/_testclinic.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2640,7 +2640,7 @@ _PyArg_UnpackKeywordsWithVararg(PyObject *const *args, Py_ssize_t nargs,
*
* Otherwise, we leave a place at `buf[vararg]` for vararg tuple
* so the index is `i + 1`. */
if (nargs < vararg) {
if (i < vararg) {
buf[i] = current_arg;
}
else {
Expand Down

0 comments on commit 87c6e9d

Please sign in to comment.