Skip to content
Merged
Changes from 2 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
41 changes: 20 additions & 21 deletions src/pep8ext_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from fnmatch import fnmatchcase
from functools import partial
from itertools import chain
from typing import List, Tuple
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I prefer import typing as t but won't inflict that on others


from flake8 import style_guide

Expand All @@ -31,35 +32,33 @@
FUNC_NODES = (ast.FunctionDef, ast.AsyncFunctionDef)


class _ASTCheckMeta(type):
def __init__(cls, class_name, bases, namespace):
cls.codes = tuple(code for code in namespace if code.startswith('N'))
try:
cls.all.append(cls())
except AttributeError:
cls.all = []
class BaseASTCheck:
"""Base for AST Checks."""

all: List['BaseASTCheck'] = []
codes: Tuple[str, ...]

def _err(self, node, code, **kwargs):
lineno, col_offset = node.lineno, node.col_offset
if isinstance(node, ast.ClassDef):
col_offset += 6
elif isinstance(node, FUNC_NODES):
col_offset += 4
code_str = getattr(self, code)
if kwargs:
code_str = code_str.format(**kwargs)
return lineno, col_offset + 1, f'{code} {code_str}', self
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
cls.all.append(cls())
cls.codes = tuple(code for code in dir(cls) if code.startswith('N'))

def err(self, node, code: str, **kwargs):
lineno, col_offset = node.lineno, node.col_offset
if isinstance(node, ast.ClassDef):
col_offset += 6
elif isinstance(node, FUNC_NODES):
col_offset += 4
code_str = getattr(self, code)
if kwargs:
code_str = code_str.format(**kwargs)
return lineno, col_offset + 1, f'{code} {code_str}', self


def _ignored(name, ignore):
return any(fnmatchcase(name, i) for i in ignore)


BaseASTCheck = _ASTCheckMeta('BaseASTCheck', (object,),
{'__doc__': "Base for AST Checks.", 'err': _err})


class _FunctionType:
CLASSMETHOD = 'classmethod'
STATICMETHOD = 'staticmethod'
Expand Down