gh-122634: Deprecate __class_getitem__ on metaclasses#122743
gh-122634: Deprecate __class_getitem__ on metaclasses#122743sobolevn wants to merge 2 commits intopython:mainfrom
__class_getitem__ on metaclasses#122743Conversation
ethanfurman
left a comment
There was a problem hiding this comment.
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+.
ethanfurman
left a comment
There was a problem hiding this comment.
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. |
| 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.
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.
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: |
|
This PR is stale because it has been open for 30 days with no activity. |
📚 Documentation preview 📚: https://cpython-previews--122743.org.readthedocs.build/