Skip to content

Commit

Permalink
Ignore TypedDict attribute casing
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Patrick committed Jan 20, 2022
1 parent 407fa0c commit 9d199f1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
31 changes: 19 additions & 12 deletions src/pep8ext_naming.py
Original file line number Diff line number Diff line change
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 @@ -448,6 +453,8 @@ class VariablesCheck(BaseASTCheck):
def _find_errors(self, assignment_target, parents, ignore):
for parent_func in reversed(parents):
if isinstance(parent_func, ast.ClassDef):
if "TypedDict" in parent_func.superclasses:
return
checker = self.class_variable_check
break
if isinstance(parent_func, FUNC_NODES):
Expand Down
12 changes: 12 additions & 0 deletions testsuite/N815_py38.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# python_version >= '3.8'
#: Okay
class MyDict(TypedDict):
mixedCase: str
class MyOtherDict(MyDict):
more_Mixed_Case: str
#: N815
class TypedDict:
mixedCase: str
#: N815
class TypedDict:
more_Mixed_Case: str

0 comments on commit 9d199f1

Please sign in to comment.