mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
bpo-32226: Make __class_getitem__ an automatic class method. (#5098)
This commit is contained in:
parent
87be28f4a1
commit
ce5b0e9db1
5 changed files with 31 additions and 12 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue