Skip to content

Commit 3ec65fd

Browse files
author
Daniel Patrick
committed
Implement new check, N819: TypedDict variable naming
1 parent 9d199f1 commit 3ec65fd

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

README.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ These error codes are emitted:
7373
+---------+-----------------------------------------------------------------+
7474
| _`N818` | error suffix in exception names (`exceptions`_) |
7575
+---------+-----------------------------------------------------------------+
76+
| _`N819` | mixedCase variable in TypedDict subclass |
77+
| | (distinct from `N815`_ for selective enforcement) |
78+
+---------+-----------------------------------------------------------------+
7679

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

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

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

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

src/pep8ext_naming.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def add_options(cls, parser):
169169
help='List of method decorators pep8-naming plugin '
170170
'should consider staticmethods (Defaults to '
171171
'%default)')
172-
parser.extend_default_ignore(['N818'])
172+
parser.extend_default_ignore(['N818', 'N819'])
173173

174174
@classmethod
175175
def parse_options(cls, options):
@@ -449,13 +449,15 @@ class VariablesCheck(BaseASTCheck):
449449
N806 = "variable '{name}' in function should be lowercase"
450450
N815 = "variable '{name}' in class scope should not be mixedCase"
451451
N816 = "variable '{name}' in global scope should not be mixedCase"
452+
N819 = "variable '{name}' in TypedDict should not be mixedCase"
452453

453454
def _find_errors(self, assignment_target, parents, ignore):
454455
for parent_func in reversed(parents):
455456
if isinstance(parent_func, ast.ClassDef):
456457
if "TypedDict" in parent_func.superclasses:
457-
return
458-
checker = self.class_variable_check
458+
checker = self.typeddict_variable_check
459+
else:
460+
checker = self.class_variable_check
459461
break
460462
if isinstance(parent_func, FUNC_NODES):
461463
checker = partial(self.function_variable_check, parent_func)
@@ -544,6 +546,11 @@ def function_variable_check(func, var_name):
544546
return None
545547
return 'N806'
546548

549+
@staticmethod
550+
def typeddict_variable_check(name):
551+
if is_mixed_case(name):
552+
return 'N819'
553+
547554

548555
def _extract_names(assignment_target):
549556
"""Yield assignment_target ids."""

testsuite/N815.py

+6
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ class C:
2525
#: Okay(--ignore-names=*Case)
2626
class C:
2727
mixed_Case = 0
28+
#: N815
29+
class TypedDict:
30+
mixedCase: str
31+
#: N815
32+
class TypedDict:
33+
more_Mixed_Case: str

testsuite/N815_py38.py

-12
This file was deleted.

testsuite/N819.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#: Okay
2+
class MyDict(TypedDict):
3+
snake_case: str
4+
#: N819
5+
class MyDict(TypedDict):
6+
mixedCase: str
7+
#: N819
8+
class MyDict(TypedDict):
9+
snake_case: str
10+
class MyOtherDict(MyDict):
11+
more_Mixed_Case: str
12+
#: Okay(--ignore-names=mixedCase)
13+
class MyDict(TypedDict):
14+
mixedCase: str
15+
#: Okay(--ignore-names=*Case)
16+
class MyDict(TypedDict):
17+
mixedCase: str

0 commit comments

Comments
 (0)