Author: Yannick Jadoul <[email protected]>
Branch: py3.7
Changeset: r97504:5972be85ad6d
Date: 2019-09-08 22:45 +0200
http://bitbucket.org/pypy/pypy/changeset/5972be85ad6d/
Log: Making sure __class_getitem__ is a classmethod, even when not
annotated with @classmethod
diff --git a/pypy/objspace/std/test/test_typeobject.py
b/pypy/objspace/std/test/test_typeobject.py
--- a/pypy/objspace/std/test/test_typeobject.py
+++ b/pypy/objspace/std/test/test_typeobject.py
@@ -1412,7 +1412,7 @@
def __getitem__(self, index):
return index + 1
def __class_getitem__(cls, item):
- return "{}[{}]".format(cls.__name__, item.__name__)
+ return super().__class_getitem__(item)
assert WithoutMetaclass()[0] == 1
assert WithoutMetaclass[int] == "WithoutMetaclass[int]"
@@ -1427,7 +1427,7 @@
def __getitem__(self, index):
return index + 1
def __class_getitem__(cls, item):
- return "{}[{}]".format(cls.__name__, item.__name__)
+ return super().__class_getitem__(item)
assert WithMetaclass()[0] == 1
assert WithMetaclass[int] == "Metaclass[int]"
diff --git a/pypy/objspace/std/typeobject.py b/pypy/objspace/std/typeobject.py
--- a/pypy/objspace/std/typeobject.py
+++ b/pypy/objspace/std/typeobject.py
@@ -1380,7 +1380,7 @@
w_self.mro_w = [] # temporarily
w_self.hasmro = False
compute_mro(w_self)
- ensure_classmethod_init_subclass(w_self)
+ ensure_classmethods(w_self, ['__init_subclass__', '__class_getitem__'])
def ensure_static_new(w_self):
# special-case __new__, as in CPython:
@@ -1390,11 +1390,12 @@
if isinstance(w_new, Function):
w_self.dict_w['__new__'] = StaticMethod(w_new)
-def ensure_classmethod_init_subclass(w_self):
- if '__init_subclass__' in w_self.dict_w:
- w_init_subclass = w_self.dict_w['__init_subclass__']
- if isinstance(w_init_subclass, Function):
- w_self.dict_w['__init_subclass__'] = ClassMethod(w_init_subclass)
+def ensure_classmethods(w_self, method_names):
+ for method_name in method_names:
+ if method_name in w_self.dict_w:
+ w_method = w_self.dict_w[method_name]
+ if isinstance(w_method, Function):
+ w_self.dict_w[method_name] = ClassMethod(w_method)
def ensure_module_attr(w_self):
# initialize __module__ in the dict (user-defined types only)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit