bpo-32226: Make __class_getitem__ an automatic class method. (#5098)

This commit is contained in:
Serhiy Storchaka 2018-01-05 00:21:41 +02:00 committed by GitHub
parent 87be28f4a1
commit ce5b0e9db1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 12 deletions

View file

@ -183,12 +183,21 @@ class TestClassGetitem(unittest.TestCase):
self.assertEqual(D[int], 'D[int]')
self.assertEqual(D[D], 'D[D]')
def test_class_getitem_classmethod(self):
class C:
@classmethod
def __class_getitem__(cls, item):
return f'{cls.__name__}[{item.__name__}]'
class D(C): ...
self.assertEqual(D[int], 'D[int]')
self.assertEqual(D[D], 'D[D]')
def test_class_getitem_patched(self):
class C:
def __init_subclass__(cls):
def __class_getitem__(cls, item):
return f'{cls.__name__}[{item.__name__}]'
cls.__class_getitem__ = __class_getitem__
cls.__class_getitem__ = classmethod(__class_getitem__)
class D(C): ...
self.assertEqual(D[int], 'D[int]')
self.assertEqual(D[D], 'D[D]')
@ -254,7 +263,7 @@ class CAPITest(unittest.TestCase):
def test_c_class(self):
from _testcapi import Generic, GenericAlias
self.assertIsInstance(Generic.__class_getitem__(Generic, int), GenericAlias)
self.assertIsInstance(Generic.__class_getitem__(int), GenericAlias)
IntGeneric = Generic[int]
self.assertIs(type(IntGeneric), GenericAlias)