Skip to content

Commit

Permalink
Recognize abc.ABCMeta as a metaclass base type (#121)
Browse files Browse the repository at this point in the history
Recognize abc.ABCMeta as a metaclass base type
  • Loading branch information
jparise committed Jul 17, 2019
2 parents baaa468 + 700461e commit 05697b6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/pep8ext_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
PYTHON_VERSION = sys.version_info[:3]
PY2 = PYTHON_VERSION[0] == 2

METACLASS_BASES = frozenset(('type', 'ABCMeta'))

# Node types which may contain class methods
METHOD_CONTAINER_NODES = {ast.If, ast.While, ast.For, ast.With}
FUNC_NODES = (ast.FunctionDef,)
Expand Down Expand Up @@ -204,7 +206,8 @@ def tag_class_functions(self, cls_node):
cls_bases = [b for b in cls_node.bases if isinstance(b, ast.Name)]
# If this class inherits from `type`, it's a metaclass, and we'll
# consider all of it's methods to be classmethods.
ismetaclass = any(name for name in cls_bases if name.id == 'type')
ismetaclass = any(
name for name in cls_bases if name.id in METACLASS_BASES)
self.set_function_nodes_types(
iter_child_nodes(cls_node), ismetaclass, late_decoration)

Expand Down
5 changes: 5 additions & 0 deletions testsuite/N804.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from abc import ABCMeta

#: N804:7:13
class Foo(object):
@classmethod
Expand Down Expand Up @@ -27,6 +29,9 @@ def __new__(self, name, bases, attrs):
class MetaMethod(type):
def test(cls):
pass
class MetaMethod(ABCMeta):
def test(cls):
pass
#: Okay
class NotMeta(object):
otherclass = Foo
Expand Down
8 changes: 8 additions & 0 deletions testsuite/N805.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from abc import ABCMeta

#: Okay
class C:
def __init__(*args, **kwargs):
Expand Down Expand Up @@ -46,6 +48,12 @@ def __new__(cls, name, bases, attrs):
pass
def test(cls):
pass
#: Okay
class Meta(ABCMeta):
def __new__(cls, name, bases, attrs):
pass
def test(cls):
pass
#: Okay(--classmethod-decorators=clazzy,cool)
class NewClassmethodDecorators(object):
@clazzy
Expand Down

0 comments on commit 05697b6

Please sign in to comment.