Skip to content

N815: Ignore TypedDict class variable casing #189

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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:
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

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