summaryrefslogtreecommitdiff
path: root/Lib
diff options
context:
space:
mode:
authorNate <nate@so8r.es>2017-04-24 10:06:15 -0700
committerSerhiy Storchaka <storchaka@gmail.com>2017-04-24 20:06:15 +0300
commitfcfe80ec2592fed8b3941c79056a8737abef7d3b (patch)
tree5dc4a68452313e95e2e9bcecac7b9821d14c3a15 /Lib
parent2e576f5aec1f8f23f07001e2eb3db9276851a4fc (diff)
downloadcpython-git-fcfe80ec2592fed8b3941c79056a8737abef7d3b.tar.gz
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.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/inspect.py23
-rw-r--r--Lib/test/test_inspect.py24
2 files changed, 46 insertions, 1 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index a2dcb888a0..2894672f50 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -31,6 +31,7 @@ Here are some of the useful functions provided by this module:
__author__ = ('Ka-Ping Yee <ping@lfw.org>',
'Yury Selivanov <yselivanov@sprymix.com>')
+import abc
import ast
import dis
import collections.abc
@@ -291,7 +292,27 @@ def isroutine(object):
def isabstract(object):
"""Return true if the object is an abstract base class (ABC)."""
- return bool(isinstance(object, type) and object.__flags__ & TPFLAGS_IS_ABSTRACT)
+ if not isinstance(object, type):
+ return False
+ if object.__flags__ & TPFLAGS_IS_ABSTRACT:
+ return True
+ if not issubclass(type(object), abc.ABCMeta):
+ return False
+ if hasattr(object, '__abstractmethods__'):
+ # It looks like ABCMeta.__new__ has finished running;
+ # TPFLAGS_IS_ABSTRACT should have been accurate.
+ return False
+ # It looks like ABCMeta.__new__ has not finished running yet; we're
+ # probably in __init_subclass__. We'll look for abstractmethods manually.
+ for name, value in object.__dict__.items():
+ if getattr(value, "__isabstractmethod__", False):
+ return True
+ for base in object.__bases__:
+ for name in getattr(base, "__abstractmethods__", ()):
+ value = getattr(object, name, None)
+ if getattr(value, "__isabstractmethod__", False):
+ return True
+ return False
def getmembers(object, predicate=None):
"""Return all members of an object as (name, value) pairs sorted by name.
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 2f12e989d9..799a9faa78 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -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):