Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
gh-104600: Make type.__type_params__ writable#104634
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
41b9279
gh-104600: Make type.__type_params__ writable
JelleZijlstra 3f4339d
be like __doc__, not __annotations__
JelleZijlstra 9b982c4
Add tests for generic namedtuples and typeddicts
JelleZijlstra b870cd1
Update test
JelleZijlstra 0bed086
Update Lib/test/test_typing.py
JelleZijlstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -6795,6 +6795,19 @@ class Y(Generic[T], NamedTuple): | ||
| with self.assertRaises(TypeError): | ||
| G[int, str] | ||
| def test_generic_pep695(self): | ||
| class X[T](NamedTuple): | ||
| x: T | ||
| T, = X.__type_params__ | ||
| self.assertIsInstance(T, TypeVar) | ||
| self.assertEqual(T.__name__, 'T') | ||
| self.assertEqual(X.__bases__, (tuple, Generic)) | ||
| self.assertEqual(X.__orig_bases__, (NamedTuple, Generic[T])) | ||
| self.assertEqual(X.__mro__, (X, tuple, Generic, object)) | ||
JelleZijlstra marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| self.assertEqual(X.__parameters__, (T,)) | ||
| self.assertEqual(X[str].__args__, (str,)) | ||
| self.assertEqual(X[str].__parameters__, ()) | ||
| def test_non_generic_subscript(self): | ||
| # For backward compatibility, subscription works | ||
| # on arbitrary NamedTuple types. | ||
| @@ -7205,6 +7218,20 @@ class FooBarGeneric(BarGeneric[int]): | ||
| {'a': typing.Optional[T], 'b': int, 'c': str} | ||
| ) | ||
| def test_pep695_generic_typeddict(self): | ||
| class A[T](TypedDict): | ||
| a: T | ||
| T, = A.__type_params__ | ||
| self.assertIsInstance(T, TypeVar) | ||
| self.assertEqual(T.__name__, 'T') | ||
| self.assertEqual(A.__bases__, (Generic, dict)) | ||
| self.assertEqual(A.__orig_bases__, (TypedDict, Generic[T])) | ||
| self.assertEqual(A.__mro__, (A, Generic, dict, object)) | ||
| self.assertEqual(A.__parameters__, (T,)) | ||
| self.assertEqual(A[str].__parameters__, ()) | ||
| self.assertEqual(A[str].__args__, (str,)) | ||
| def test_generic_inheritance(self): | ||
| class A(TypedDict, Generic[T]): | ||
| a: T | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1460,18 +1460,6 @@ type_get_annotations(PyTypeObject *type, void *context) | ||
| return annotations; | ||
| } | ||
| static PyObject * | ||
| type_get_type_params(PyTypeObject *type, void *context) | ||
| { | ||
| PyObject *params = PyDict_GetItem(lookup_tp_dict(type), &_Py_ID(__type_params__)); | ||
| if (params) { | ||
| return Py_NewRef(params); | ||
| } | ||
| return PyTuple_New(0); | ||
| } | ||
| static int | ||
JelleZijlstra marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| type_set_annotations(PyTypeObject *type, PyObject *value, void *context) | ||
| { | ||
| @@ -1502,6 +1490,34 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context) | ||
| return result; | ||
| } | ||
| static PyObject * | ||
| type_get_type_params(PyTypeObject *type, void *context) | ||
| { | ||
| PyObject *params = PyDict_GetItem(lookup_tp_dict(type), &_Py_ID(__type_params__)); | ||
| if (params) { | ||
| return Py_NewRef(params); | ||
| } | ||
| return PyTuple_New(0); | ||
| } | ||
| static int | ||
| type_set_type_params(PyTypeObject *type, PyObject *value, void *context) | ||
| { | ||
| if (!check_set_special_type_attr(type, value, "__type_params__")) { | ||
| return -1; | ||
| } | ||
| PyObject *dict = lookup_tp_dict(type); | ||
| int result = PyDict_SetItem(dict, &_Py_ID(__type_params__), value); | ||
| if (result == 0) { | ||
| PyType_Modified(type); | ||
| } | ||
| return result; | ||
| } | ||
| /*[clinic input] | ||
| type.__instancecheck__ -> bool | ||
| @@ -1548,7 +1564,7 @@ static PyGetSetDef type_getsets[] = { | ||
| {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL}, | ||
| {"__text_signature__", (getter)type_get_text_signature, NULL, NULL}, | ||
| {"__annotations__", (getter)type_get_annotations, (setter)type_set_annotations, NULL}, | ||
| {"__type_params__", (getter)type_get_type_params, NULL, NULL}, | ||
| {"__type_params__", (getter)type_get_type_params, (setter)type_set_type_params, NULL}, | ||
| {0} | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.