Commit 9bcc8775 authored by Yuan's avatar Yuan Committed by GitHub

Support PEP-560 ("__class_getitem__") for extension classes (GH-3765)

parent 0208bf2b
......@@ -2965,10 +2965,12 @@ class DefNode(FuncDefNode):
# staticmethod() was overridden - not much we can do here ...
self.is_staticmethod = False
if env.is_py_class_scope:
if self.name == '__new__':
if env.is_py_class_scope or env.is_c_class_scope:
if self.name == '__new__' and env.is_py_class_scope:
self.is_staticmethod = True
elif not self.is_classmethod and self.name in IMPLICIT_CLASSMETHODS:
elif self.name == '__init_subclass__' and env.is_c_class_scope:
error(self.pos, "'__init_subclass__' is not supported by extension class")
elif self.name in IMPLICIT_CLASSMETHODS and not self.is_classmethod:
self.is_classmethod = True
# TODO: remove the need to generate a real decorator here, is_classmethod=True should suffice.
from .ExprNodes import NameNode
......
# mode: error
cdef class Imp:
def __init_subclass__(cls, a=None, **kwargs):
super().__init_subclass__(**kwargs)
print(a)
cdef class ExImp1(Imp): pass
class ExImp2(Imp, a=60): pass
_ERRORS = u"""
4:4: '__init_subclass__' is not supported by extension class
"""
# mode: run
# cython: language_level=3
import unittest
import sys
cdef class UnSupport: pass
cdef class Unpack:
para_list = []
def __class_getitem__(*args, **kwargs):
Unpack.para_list.extend([args, kwargs])
cdef class Format:
def __class_getitem__(cls, item):
return f'{cls.__name__}[{item.__name__}]'
cdef class ExFormat(Format): pass
cdef class Override:
def __class_getitem__(cls, item):
return 'Should not see this'
cdef class Covered(Override):
def __class_getitem__(cls, item):
return f'{cls.__name__}[{item.__name__}]'
cdef class Decorated:
@classmethod
def __class_getitem__(cls, item):
return f'{cls.__name__}[{item.__name__}]'
cdef class ExDecorated(Decorated): pass
cdef class Invalid1:
def __class_getitem__(cls): pass
cdef class Invalid2:
def __class_getitem__(cls, item1, item2): pass
cdef class Invalid3:
cdef dict __dict__
def __init__(self):
self.__class_getitem__ = lambda cls, items: 'This will not work'
cdef class Invalid4:
__class_getitem__ = "Surprise!"
class TestClassGetitem(unittest.TestCase):
# BEGIN - Additional tests from cython
def test_no_class_getitem(self):
with self.assertRaises(TypeError):
UnSupport[int]
# END - Additional tests from cython
def test_class_getitem(self):
Unpack[int, str]
self.assertEqual(Unpack.para_list[0], (Unpack, (int, str)))
self.assertEqual(Unpack.para_list[1], {})
def test_class_getitem_format(self):
self.assertEqual(Format[int], 'Format[int]')
self.assertEqual(Format[Format], 'Format[Format]')
def test_class_getitem_inheritance(self):
self.assertEqual(ExFormat[int], 'ExFormat[int]')
self.assertEqual(ExFormat[ExFormat], 'ExFormat[ExFormat]')
def test_class_getitem_inheritance_2(self):
self.assertEqual(Covered[int], 'Covered[int]')
self.assertEqual(Covered[Covered], 'Covered[Covered]')
def test_class_getitem_classmethod(self):
self.assertEqual(ExDecorated[int], 'ExDecorated[int]')
self.assertEqual(ExDecorated[ExDecorated], 'ExDecorated[ExDecorated]')
def test_class_getitem_errors(self):
with self.assertRaises(TypeError):
Invalid1[int]
with self.assertRaises(TypeError):
Invalid2[int]
def test_class_getitem_errors_2(self):
with self.assertRaises(TypeError):
Format()[int]
with self.assertRaises(TypeError):
Invalid3()[int]
with self.assertRaises(TypeError):
Invalid4[int]
if __name__ == '__main__':
unittest.main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment