Skip to content

[3.6] bpo-29822: make inspect.isabstract() work during __init_subclass__ #1979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Lib/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
__author__ = ('Ka-Ping Yee <[email protected]>',
'Yury Selivanov <[email protected]>')

import abc
import ast
import dis
import collections.abc
Expand Down Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,30 @@ def foo(self):
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):
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ Core and Builtins
Library
-------

- bpo-29822: inspect.isabstract() now works during __init_subclass__. Patch
by Nate Soares.

- bpo-29581: ABCMeta.__new__ now accepts ``**kwargs``, allowing abstract base
classes to use keyword parameters in __init_subclass__. Patch by Nate Soares.

Expand Down