Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N815: Ignore TypedDict class variable casing #189

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ These error codes are emitted:
+---------+-----------------------------------------------------------------+
| _`N818` | error suffix in exception names (`exceptions`_) |
+---------+-----------------------------------------------------------------+
| _`N819` | mixedCase variable in TypedDict subclass |
| | (distinct from `N815`_ for selective enforcement) |
+---------+-----------------------------------------------------------------+

.. _class names: https://www.python.org/dev/peps/pep-0008/#class-names
.. _constants: https://www.python.org/dev/peps/pep-0008/#constants
Expand All @@ -88,7 +91,7 @@ The following flake8 options are added:

--ignore-names Ignore errors for specific names or glob patterns.

Currently, this option can only be used for N802, N803, N804, N805, N806, N815, and N816 errors.
Currently, this option can only be used for N802, N803, N804, N805, N806, N815, N816 and N819 errors.

Default: ``setUp,tearDown,setUpClass,tearDownClass,asyncSetUp,asyncTearDown,setUpTestData,failureException,longMessage,maxDiff``.

Expand Down
42 changes: 28 additions & 14 deletions src/pep8ext_naming.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def add_options(cls, parser):
help='List of method decorators pep8-naming plugin '
'should consider staticmethods (Defaults to '
'%default)')
parser.extend_default_ignore(['N818'])
parser.extend_default_ignore(['N818', 'N819'])

@classmethod
def parse_options(cls, options):
Expand Down Expand Up @@ -202,6 +202,7 @@ def visit_tree(self, node):
def visit_node(self, node):
if isinstance(node, ast.ClassDef):
self.tag_class_functions(node)
self.tag_class_superclasses(node)
elif isinstance(node, FUNC_NODES):
self.find_global_defs(node)

Expand Down Expand Up @@ -264,6 +265,11 @@ def set_function_nodes_types(self, nodes, ismetaclass, late_decoration):
node.function_type = self.decorator_to_type[name]
break

def tag_class_superclasses(self, cls_node):
cls_node.superclasses = self.superclass_names(
cls_node.name, self.parents
)

@classmethod
def find_decorator_name(cls, d):
if isinstance(d, ast.Name):
Expand All @@ -286,16 +292,6 @@ def find_global_defs(func_def_node):
nodes_to_check.extend(iter_child_nodes(node))
func_def_node.global_names = global_names


class ClassNameCheck(BaseASTCheck):
"""
Almost without exception, class names use the CapWords convention.

Classes for internal use have a leading underscore in addition.
"""
N801 = "class name '{name}' should use CapWords convention"
N818 = "exception name '{name}' should be named with an Error suffix"

@classmethod
def get_classdef(cls, name, parents):
for parent in parents:
Expand All @@ -315,15 +311,24 @@ def superclass_names(cls, name, parents, _names=None):
names.update(cls.superclass_names(base.id, parents, names))
return names


class ClassNameCheck(BaseASTCheck):
"""
Almost without exception, class names use the CapWords convention.

Classes for internal use have a leading underscore in addition.
"""
N801 = "class name '{name}' should use CapWords convention"
N818 = "exception name '{name}' should be named with an Error suffix"

def visit_classdef(self, node, parents, ignore=None):
name = node.name
if _ignored(name, ignore):
return
name = name.strip('_')
if not name[:1].isupper() or '_' in name:
yield self.err(node, 'N801', name=name)
superclasses = self.superclass_names(name, parents)
if "Exception" in superclasses and not name.endswith("Error"):
if "Exception" in node.superclasses and not name.endswith("Error"):
yield self.err(node, 'N818', name=name)


Expand Down Expand Up @@ -444,11 +449,15 @@ class VariablesCheck(BaseASTCheck):
N806 = "variable '{name}' in function should be lowercase"
N815 = "variable '{name}' in class scope should not be mixedCase"
N816 = "variable '{name}' in global scope should not be mixedCase"
N819 = "variable '{name}' in TypedDict should not be mixedCase"

def _find_errors(self, assignment_target, parents, ignore):
for parent_func in reversed(parents):
if isinstance(parent_func, ast.ClassDef):
checker = self.class_variable_check
if "TypedDict" in parent_func.superclasses:
Copy link
Member

Choose a reason for hiding this comment

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

Why are we skipping this altogether? People may in fact want to check this. I would instead think we should add an error code for this case so it can be ignored/disabled on a case-by-case basis

Choose a reason for hiding this comment

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

Good question. Happy to have a go at implementing that. Would you go with something like N819: mixedCase variable in TypedDict? Do you think we should disable by default?

Choose a reason for hiding this comment

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

Have pushed a new commit to implement this new error code, let me know what you think

checker = self.typeddict_variable_check
else:
checker = self.class_variable_check
break
if isinstance(parent_func, FUNC_NODES):
checker = partial(self.function_variable_check, parent_func)
Expand Down Expand Up @@ -537,6 +546,11 @@ def function_variable_check(func, var_name):
return None
return 'N806'

@staticmethod
def typeddict_variable_check(name):
if is_mixed_case(name):
return 'N819'


def _extract_names(assignment_target):
"""Yield assignment_target ids."""
Expand Down
6 changes: 6 additions & 0 deletions testsuite/N815.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ class C:
#: Okay(--ignore-names=*Case)
class C:
mixed_Case = 0
#: N815
class TypedDict:
mixedCase = 0
#: N815
class TypedDict:
mixed_Case = 0
18 changes: 18 additions & 0 deletions testsuite/N819_py36.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# python_version >= '3.6'
#: Okay
class MyDict(TypedDict):
snake_case: str
#: N819
class MyDict(TypedDict):
mixedCase: str
#: N819
class MyDict(TypedDict):
snake_case: str
class MyOtherDict(MyDict):
more_Mixed_Case: str
#: Okay(--ignore-names=mixedCase)
class MyDict(TypedDict):
mixedCase: str
#: Okay(--ignore-names=*Case)
class MyDict(TypedDict):
mixedCase: str