Author: Yannick Jadoul <[email protected]>
Branch: py3.7
Changeset: r97389:4c3f7009c136
Date: 2019-09-06 15:33 +0200
http://bitbucket.org/pypy/pypy/changeset/4c3f7009c136/
Log: Implemented __class_getitem__ from PEP 560
diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -305,6 +305,8 @@
def getitem(space, w_obj, w_key):
w_descr = space.lookup(w_obj, '__getitem__')
+ if w_descr is None and space.isinstance_w(w_obj, space.w_type):
+ w_descr = space.getattr(w_obj, space.newtext('__class_getitem__'))
if w_descr is None:
raise oefmt(space.w_TypeError,
"'%T' object is not subscriptable (key %R)",
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
@@ -1400,6 +1400,39 @@
assert found == [1]
"""
+ def test_class_getitem(self):
+ """
+ class WithoutMetaclass:
+ def __getitem__(self, index):
+ return index + 1
+ def __class_getitem__(cls, item):
+ return "{}[{}]".format(cls.__name__, item.__name__)
+
+ class WithoutMetaclassSubclass(WithoutMetaclass):
+ def __getitem__(self, index):
+ return index + 1
+ def __class_getitem__(cls, item):
+ return "{}[{}]".format(cls.__name__, item.__name__)
+
+ assert WithoutMetaclass()[0] == 1
+ assert WithoutMetaclass[int] == "WithoutMetaclass[int]"
+ assert WithoutMetaclassSubclass()[0] == 1
+ assert WithoutMetaclassSubclass[int] == "WithoutMetaclassSubclass[int]"
+
+ class Metaclass(type):
+ def __getitem__(self, item):
+ return "Metaclass[{}]".format(item.__name__)
+
+ class WithMetaclass(metaclass=Metaclass):
+ def __getitem__(self, index):
+ return index + 1
+ def __class_getitem__(cls, item):
+ return "{}[{}]".format(cls.__name__, item.__name__)
+
+ assert WithMetaclass()[0] == 1
+ assert WithMetaclass[int] == "Metaclass[int]"
+ """
+
class AppTestWithMethodCacheCounter:
spaceconfig = {"objspace.std.withmethodcachecounter": True}
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit