8000 gh-124498: Fix `TypeAliasType` not to be generic, when `type_params=()` by sobolevn · Pull Request #124499 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-124498: Fix TypeAliasType not to be generic, when type_params=() #124499

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
Sep 26, 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-124498: Fix TypeAliasType not to be generic, when type_params=()
  • Loading branch information
sobolevn committed Sep 25, 2024
commit 87b91c0bf6558ee1992cca0b56dbd0e076181236
13 changes: 13 additions & 0 deletions Lib/test/test_type_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ def test_generic(self):
self.assertEqual(TA.__value__, list[T])
self.assertEqual(TA.__type_params__, (T,))
self.assertEqual(TA.__module__, __name__)
self.assertIs(TA[int].__class__, types.GenericAlias)

def test_not_generic(self):
TA = TypeAliasType("TA", list[int], type_params=())
self.assertEqual(TA.__name__, "TA")
self.assertEqual(TA.__value__, list[int])
self.assertEqual(TA.__type_params__, ())
self.assertEqual(TA.__module__, __name__)
with self.assertRaisesRegex(
TypeError,
"Only generic type aliases are subscriptable",
):
TA[int]

def test_keywords(self):
TA = TypeAliasType(name="TA", value=int)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :class:`typing.TypeAliasType` not to be generic, when ``type_params`` is
an empty tuple.
3 changes: 2 additions & 1 deletion Objects/typevarobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,8 @@ typealias_reduce_impl(typealiasobject *self)
static PyObject *
typealias_subscript(PyObject *self, PyObject *args)
{
if (((typealiasobject *)self)->type_params == NULL) {
typealiasobject *ta = ((typealiasobject *)self);
if (ta->type_params == NULL || PyTuple_GET_SIZE(ta->type_params) == 0) {
PyErr_SetString(PyExc_TypeError,
"Only generic type aliases are subscriptable");
return NULL;
Expand Down
Loading
0