Skip to content
Snippets Groups Projects
Commit f89139b8 authored by 5j9's avatar 5j9
Browse files

Detect class methods defined within compound statements

Fix #45
parent 8e669436
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -19,6 +19,11 @@ UPPERCASE_REGEX = re.compile(r'[_A-Z][_A-Z0-9]*$')
MIXEDCASE_REGEX = re.compile(r'_?[A-Z][a-zA-Z0-9]*$')
 
 
# Node types which may contain class methods
METHOD_CONTAINER_NODES = {ast.If, ast.While, ast.For, ast.With} | (
{ast.TryExcept, ast.TryFinally} if sys.version_info[0] == 2 else {ast.Try})
if sys.version_info[0] < 3:
def _unpack_args(args):
ret = []
Loading
Loading
@@ -184,9 +189,15 @@ class NamingChecker(object):
# 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')
self.set_function_nodes_types(
iter_child_nodes(cls_node), ismetaclass, late_decoration)
 
def set_function_nodes_types(self, nodes, ismetaclass, late_decoration):
# iterate over all functions and tag them
for node in iter_child_nodes(cls_node):
for node in nodes:
if type(node) in METHOD_CONTAINER_NODES:
self.set_function_nodes_types(
iter_child_nodes(node), ismetaclass, late_decoration)
if not isinstance(node, ast.FunctionDef):
continue
node.function_type = _FunctionType.METHOD
Loading
Loading
#: Okay
class C1:
def __str__(self):
return ''
#: Okay
class C2:
if True:
def __str__(self):
return ''
#: Okay
class C3:
try:
if True:
while True:
def __str__(self):
return ''
break
except:
pass
#: N807
def __bad():
pass
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment