8000 gh-118814: Fix the TypeVar constructor when name is passed by keyword by serhiy-storchaka · Pull Request #122664 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-118814: Fix the TypeVar constructor when name is passed by keyword #122664

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

Merged
merged 2 commits into from
Aug 7, 2024
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-118814: Fix the TypeVar constructor when name is passed by keyword
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.
  • Loading branch information
serhiy-storchaka committed Aug 4, 2024
commit dc5949efa56b29c7d006ff33621486ac48c191d4
49 changes: 49 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,55 @@ def test_constructor(self):
self.assertEqual(T.__name__, "T")
self.assertEqual(T.__constraints__, ())
self.assertIs(T.__bound__, None)
self.assertIs(T.__default__, typing.NoDefault)
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.__default__, typing.NoDefault)
self.assertIs(T.__covariant__, False)
self.assertIs(T.__contravariant__, False)
self.assertIs(T.__infer_variance__, False)

T = TypeVar(name="T", default=())
self.assertEqual(T.__name__, "T")
self.assertEqual(T.__constraints__, ())
self.assertIs(T.__bound__, None)
self.assertIs(T.__default__, ())
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.__default__, typing.NoDefault)
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.__default__, typing.NoDefault)
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.__default__, typing.NoDefault)
self.assertIs(T.__covariant__, False)
self.assertIs(T.__contravariant__, False)
self.assertIs(T.__infer_variance__, True)


class TypeParameterDefaultsTests(BaseTestCase):
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.
2 changes: 1 addition & 1 deletion Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2601,7 +2601,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 && i != vararg) {
if (i < vararg) {
buf[i] = current_arg;
}
else {
Expand Down
Loading
0