mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-29822: Make inspect.isabstract() work during __init_subclass__. (#678)
At the time when an abstract base class' __init_subclass__ runs, ABCMeta.__new__ has not yet finished running, so in the presence of __init_subclass__, inspect.isabstract() can no longer depend only on TPFLAGS_IS_ABSTRACT.
This commit is contained in:
parent
2e576f5aec
commit
fcfe80ec25
3 changed files with 49 additions and 1 deletions
|
@ -229,6 +229,30 @@ class TestPredicates(IsTestBase):
|
|||
self.assertFalse(inspect.isabstract(int))
|
||||
self.assertFalse(inspect.isabstract(5))
|
||||
|
||||
def test_isabstract_during_init_subclass(self):
|
||||
from abc import ABCMeta, abstractmethod
|
||||
isabstract_checks = []
|
||||
class AbstractChecker(metaclass=ABCMeta):
|
||||
def __init_subclass__(cls):
|
||||
isabstract_checks.append(inspect.isabstract(cls))
|
||||
class AbstractClassExample(AbstractChecker):
|
||||
@abstractmethod
|
||||
def foo(self):
|
||||
pass
|
||||
class ClassExample(AbstractClassExample):
|
||||
def foo(self):
|
||||
pass
|
||||
self.assertEqual(isabstract_checks, [True, False])
|
||||
|
||||
isabstract_checks.clear()
|
||||
class AbstractChild(AbstractClassExample):
|
||||
pass
|
||||
class AbstractGrandchild(AbstractChild):
|
||||
pass
|
||||
class ConcreteGrandchild(ClassExample):
|
||||
pass
|
||||
self.assertEqual(isabstract_checks, [True, True, False])
|
||||
|
||||
|
||||
class TestInterpreterStack(IsTestBase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue