Skip to content

Commit ab9ffbe

Browse files
authored
Ignore methods decorated by @typing.override (#217)
PEP 0698 introduces the `@typing.override` decorator which indicates a method overrides a base class method. It's nice to skip naming checks in this case because the user don't have any control over the name used by the subclass.
1 parent 7b1c56b commit ab9ffbe

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/pep8ext_naming.py

+13
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,26 @@ class FunctionNameCheck(BaseASTCheck):
317317
N802 = "function name '{name}' should be lowercase"
318318
N807 = "function name '{name}' should not start and end with '__'"
319319

320+
@staticmethod
321+
def has_override_decorator(node):
322+
for d in node.decorator_list:
323+
if isinstance(d, ast.Name) and d.id == 'override':
324+
return True
325+
if (isinstance(d, ast.Attribute) and isinstance(d.value, ast.Name)
326+
and d.value.id == 'typing' and d.attr == 'override'):
327+
return True
328+
return False
329+
320330
def visit_functiondef(self, node, parents, ignore=None):
321331
function_type = getattr(node, 'function_type', _FunctionType.FUNCTION)
322332
name = node.name
323333
if _ignored(name, ignore):
324334
return
325335
if name in ('__dir__', '__getattr__'):
326336
return
337+
if (function_type != _FunctionType.FUNCTION
338+
and self.has_override_decorator(node)):
339+
return
327340
if name.lower() != name:
328341
yield self.err(node, 'N802', name=name)
329342
if (function_type == _FunctionType.FUNCTION

testsuite/N802.py

+8
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ class ClassName:
5353
def notOk(self):
5454
pass
5555
#: Okay
56+
class ClassName:
57+
@override
58+
def notOk(self):
59+
pass
60+
@typing.override
61+
def alsoNotOk(self):
62+
pass
63+
#: Okay
5664
def setUp():
5765
pass
5866

0 commit comments

Comments
 (0)