-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-122634: Deprecate __class_getitem__
on metaclasses
#122743
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a version guard so the test only runs on 3.14 and 3.15, and also add the test (with version guard) that will run on 3.16+.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add a version guard so the test only runs on 3.14 and 3.15, and also add the test (with version guard) that will run on 3.16+.
Can we add a doc-only deprecation warning starting with 3.13?
I don't think that this is really needed, we don't do that in other typing-related cases. But, no strong feelings.
I think that this might be a good idea. |
@@ -5616,6 +5616,16 @@ _Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int * suppress_missin | ||
res = meta_get(meta_attribute, (PyObject *)type, | ||
(PyObject *)metatype); | ||
Py_DECREF(meta_attribute); | ||
if (res && PyUnicode_EqualToUTF8(name, "__class_getitem__")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes it so the deprecation gets triggered if you use regular attribute access to access the attribute, but I don't think that's right:
>>> class M(type):
... def __class_getitem__(*args):
... return "CGI"
...
>>> class A(metaclass=M): pass
...
>>> A.__class_getitem__
<python-input-7>:1: DeprecationWarning: Accessing __class_getitem__ on a metaclass is deprecated since 3.14 and will be removed in 3.16. Instead, define a regular __getitem__ method on a metaclass, if you need this behavior.
A.__class_getitem__
<bound method M.__class_getitem__ of <class '__main__.M'>>
Instead, the deprecation should trigger only if the lookup is triggered by the subscript operator, like in this example:
>>> A["x"]
<python-input-8>:1: DeprecationWarning: Accessing __class_getitem__ on a metaclass is deprecated since 3.14 and will be removed in 3.16. Instead, define a regular __getitem__ method on a metaclass, if you need this behavior.
A["x"]
'CGI'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that there's any clean way to do that. We don't have any info about that in _Py_type_getattro_impl
(nor in its public version).
Passing this info would require either a global state or a new parameter in several functions (some of which are public).
Maybe I am missing something?
I don't think that .__class_getitem__
access should really bother us, because:
- Users should not access magic method directly
- Warning is valid in some cases
When you're done making the requested changes, leave the comment: |
📚 Documentation preview 📚: https://cpython-previews--122743.org.readthedocs.build/