Skip to content
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
3 changes: 1 addition & 2 deletions run_tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import io
import optparse
import os
import platform
Expand All @@ -25,7 +24,7 @@ def main():
errors = 0
for filename in os.listdir('testsuite'):
filepath = os.path.join('testsuite', filename)
with io.open(filepath, encoding='utf8') as fd:
with open(filepath, encoding='utf8') as fd:
lines = list(fd)
if not is_test_allowed(lines):
continue
Expand Down
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@ project_urls =
Source=https://github.com/PyCQA/pep8-naming
Issues=https://github.com/PyCQA/pep8-naming/issues
Changelog=https://github.com/PyCQA/pep8-naming/blob/main/CHANGELOG.rst

[wheel]
universal = 1
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement
from setuptools import setup
from setuptools.command.test import test as TestCommand

Expand Down Expand Up @@ -60,6 +58,11 @@ def run_tests(self):
'Operating System :: OS Independent',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3 :: Only',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Software Development :: Quality Assurance',
],
Expand Down
58 changes: 25 additions & 33 deletions src/pep8ext_naming.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""Checker of PEP-8 Naming Conventions."""
import sys
from collections import deque
Expand Down Expand Up @@ -27,12 +26,16 @@
METACLASS_BASES = frozenset(('type', 'ABCMeta'))

# Node types which may contain class methods
METHOD_CONTAINER_NODES = {ast.If, ast.While, ast.For, ast.With, ast.Try}
FUNC_NODES = (ast.FunctionDef,)

if PYTHON_VERSION > (3, 5):
FUNC_NODES += (ast.AsyncFunctionDef,)
METHOD_CONTAINER_NODES |= {ast.AsyncWith, ast.AsyncFor}
METHOD_CONTAINER_NODES = {
ast.If,
ast.While,
ast.For,
ast.With,
ast.Try,
ast.AsyncWith,
ast.AsyncFor,
}
FUNC_NODES = (ast.FunctionDef, ast.AsyncFunctionDef)

if PYTHON_VERSION < (3, 8):
def get_arg_name_tuples(node):
Expand Down Expand Up @@ -66,7 +69,7 @@ def _err(self, node, code, **kwargs):
code_str = getattr(self, code)
if kwargs:
code_str = code_str.format(**kwargs)
return lineno, col_offset + 1, '%s %s' % (code, code_str), self
return lineno, col_offset + 1, f'{code} {code_str}', self


def _ignored(name, ignore):
Expand All @@ -77,7 +80,7 @@ def _ignored(name, ignore):
{'__doc__': "Base for AST Checks.", 'err': _err})


class _FunctionType(object):
class _FunctionType:
CLASSMETHOD = 'classmethod'
STATICMETHOD = 'staticmethod'
FUNCTION = 'function'
Expand Down Expand Up @@ -110,7 +113,7 @@ def _build_decorator_to_type(classmethod_decorators, staticmethod_decorators):
return decorator_to_type


class NamingChecker(object):
class NamingChecker:
"""Checker of PEP-8 Naming Conventions."""
name = 'naming'
version = __version__
Expand Down Expand Up @@ -175,12 +178,10 @@ def run(self):
return self.visit_tree(self._node) if self._node else ()

def visit_tree(self, node):
for error in self.visit_node(node):
yield error
yield from self.visit_node(node)
self.parents.append(node)
for child in iter_child_nodes(node):
for error in self.visit_tree(child):
yield error
yield from self.visit_tree(child)
self.parents.pop()

def visit_node(self, node):
Expand All @@ -196,8 +197,7 @@ def visit_node(self, node):
visitor_method = getattr(visitor, method, None)
if visitor_method is None:
continue
for error in visitor_method(node, parents, ignore_names):
yield error
yield from visitor_method(node, parents, ignore_names)

def tag_class_functions(self, cls_node):
"""Tag functions if they are methods, classmethods, staticmethods"""
Expand Down Expand Up @@ -458,40 +458,34 @@ def visit_assign(self, node, parents, ignore=None):
if self.is_namedtupe(node.value):
return
for target in node.targets:
for error in self._find_errors(target, parents, ignore):
yield error
yield from self._find_errors(target, parents, ignore)

def visit_namedexpr(self, node, parents, ignore):
if self.is_namedtupe(node.value):
return
for error in self._find_errors(node.target, parents, ignore):
yield error
yield from self._find_errors(node.target, parents, ignore)

visit_annassign = visit_namedexpr

def visit_with(self, node, parents, ignore):
for item in node.items:
for error in self._find_errors(
item.optional_vars, parents, ignore):
yield error
yield from self._find_errors(
item.optional_vars, parents, ignore)

visit_asyncwith = visit_with

def visit_for(self, node, parents, ignore):
for error in self._find_errors(node.target, parents, ignore):
yield error
yield from self._find_errors(node.target, parents, ignore)

visit_asyncfor = visit_for

def visit_excepthandler(self, node, parents, ignore):
if node.name:
for error in self._find_errors(node, parents, ignore):
yield error
yield from self._find_errors(node, parents, ignore)

def visit_generatorexp(self, node, parents, ignore):
for gen in node.generators:
for error in self._find_errors(gen.target, parents, ignore):
yield error
yield from self._find_errors(gen.target, parents, ignore)

visit_listcomp = visit_dictcomp = visit_setcomp = visit_generatorexp

Expand Down Expand Up @@ -525,11 +519,9 @@ def _extract_names(assignment_target):
if element_type is ast.Name:
yield element.id
elif element_type in (ast.Tuple, ast.List):
for n in _extract_names(element):
yield n
yield from _extract_names(element)
elif element_type is ast.Starred: # PEP 3132
for n in _extract_names(element.value):
yield n
yield from _extract_names(element.value)
elif target_type is ast.ExceptHandler:
yield assignment_target.name

Expand Down
30 changes: 24 additions & 6 deletions testsuite/N801.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#: N801
class notok(object):
class notok:
pass
#: Okay(--ignore-names=notok)
class notok(object):
class notok:
pass
#: Okay(--ignore-names=*ok)
class notok(object):
class notok:
pass
#: N801
class Good(object):
class notok(object):
class Good:
class notok:
pass
pass
#: Okay
class VeryGood(object):
class VeryGood:
pass
#: N801:1:7
class _:
Expand Down Expand Up @@ -52,3 +52,21 @@ class MEH__:
#: Okay
class __MEH__:
pass
#: Okay
class Γ:
pass
#: Okay
class ΓγΓγ:
pass
#: Okay
class ΓγΓ6:
pass
#: Okay
class _Γ:
pass
#: N801:1:7
class γ:
pass
#: N801
class _γ:
pass
19 changes: 0 additions & 19 deletions testsuite/N801_py3.py

This file was deleted.

28 changes: 22 additions & 6 deletions testsuite/N802.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@ def NotOK():
def _():
pass
#: Okay
class Foo(object):
class Foo:
def __method(self):
pass
#: Okay
class Foo(object):
class Foo:
def __method__(self):
pass
#: Okay
class ClassName(object):
class ClassName:
def __method__(self):
pass
#: N802
class ClassName(object):
class ClassName:
def notOk(self):
pass
#: Okay(--ignore-names=notOk)
class ClassName(object):
class ClassName:
def notOk(self):
pass
#: Okay(--ignore-names=*Ok)
class ClassName(object):
class ClassName:
def notOk(self):
pass
#: Okay
Expand Down Expand Up @@ -84,3 +84,19 @@ def asyncTearDown(self):
pass
def setUpTestData(self):
pass

#: Okay
def γ(x):
pass
#: Okay
def γ6(x):
pass

#: Okay
async def func(param1, param2):
do_stuff()
await some_coroutine()
#: N802
async def Func(param1, param2):
do_stuff()
await some_coroutine()
7 changes: 0 additions & 7 deletions testsuite/N802_py3.py

This file was deleted.

9 changes: 0 additions & 9 deletions testsuite/N802_py35.py

This file was deleted.

40 changes: 39 additions & 1 deletion testsuite/N803.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def b18(a, *_):
def b19(a, **_):
pass
#: N803:2:24
class Test(object):
class Test:
def __init__(self, BAD):
pass

Expand All @@ -75,3 +75,41 @@ def f(*I):
#: Okay(--ignore-names=I)
def f(*I):
I[''].think_therefore_i_am

#: Okay
def compare(a, b, *, key=None):
pass
#: N803
def compare(a, b, *, BAD=None):
pass
#: N803
def compare(a, b, *VERY, bad=None):
pass
#: N803
def compare(a, b, *ok, fine=None, **BAD):
pass
#: Okay
def foo(α, ß, γ):
pass
#: Okay
def foo(α, ß=''):
pass
#: Okay
def foo(**κ):
pass
#: Okay
def foo(*α):
pass
#: Okay
def foo(**κ2):
pass
#: Okay
def foo(*α2):
pass

#: Okay
async def compare(a, b, *, key=None):
pass
#: N803
async def compare(a, b, *, BAD=None):
pass
7 changes: 0 additions & 7 deletions testsuite/N803_py2.py

This file was deleted.

Loading