@@ -54,8 +54,11 @@ def err(self, node, code: str, **kwargs):
54
54
return lineno , col_offset + 1 , f'{ code } { code_str } ' , self
55
55
56
56
57
- def _ignored (name , ignore ):
58
- return any (fnmatchcase (name , i ) for i in ignore )
57
+ class NameSet (frozenset [str ]):
58
+ """A set of names that are matched using fnmatchcase."""
59
+
60
+ def __contains__ (self , item ) -> bool :
61
+ return any (fnmatchcase (item , name ) for name in self )
59
62
60
63
61
64
class _FunctionType :
@@ -65,7 +68,7 @@ class _FunctionType:
65
68
METHOD = 'method'
66
69
67
70
68
- _default_ignore_names = [
71
+ _default_ignored_names = [
69
72
'setUp' ,
70
73
'tearDown' ,
71
74
'setUpClass' ,
@@ -98,7 +101,7 @@ class NamingChecker:
98
101
visitors = BaseASTCheck .all
99
102
decorator_to_type = _build_decorator_to_type (
100
103
_default_classmethod_decorators , _default_staticmethod_decorators )
101
- ignore_names = frozenset ( _default_ignore_names )
104
+ ignored = NameSet ( _default_ignored_names )
102
105
103
106
def __init__ (self , tree , filename ):
104
107
self .tree = tree
@@ -107,7 +110,7 @@ def __init__(self, tree, filename):
107
110
def add_options (cls , parser ):
108
111
parser .add_option (
109
112
'--ignore-names' ,
110
- default = _default_ignore_names ,
113
+ default = _default_ignored_names ,
111
114
parse_from_config = True ,
112
115
comma_separated_list = True ,
113
116
help = 'List of names or glob patterns the pep8-naming '
@@ -135,14 +138,14 @@ def add_options(cls, parser):
135
138
136
139
@classmethod
137
140
def parse_options (cls , options ):
138
- cls .ignore_names = frozenset (options .ignore_names )
141
+ cls .ignored = NameSet (options .ignore_names )
139
142
cls .decorator_to_type = _build_decorator_to_type (
140
143
options .classmethod_decorators ,
141
144
options .staticmethod_decorators )
142
145
143
- # Build a list of node visitors based the error codes that have been
144
- # selected in the style guide. Only the checks that have been selected
145
- # will be evaluated as a performance optimization.
146
+ # Rebuild the list of node visitors based the error codes that have
147
+ # been selected in the style guide. Only the checks that have been
148
+ # selected will be evaluated as a performance optimization.
146
149
engine = style_guide .DecisionEngine (options )
147
150
cls .visitors = frozenset (
148
151
visitor for visitor in BaseASTCheck .all for code in visitor .codes
@@ -166,12 +169,12 @@ def visit_node(self, node, parents: Iterable):
166
169
self .find_global_defs (node )
167
170
168
171
method = 'visit_' + node .__class__ .__name__ .lower ()
169
- ignore_names = self .ignore_names
172
+ ignored = self .ignored
170
173
for visitor in self .visitors :
171
174
visitor_method = getattr (visitor , method , None )
172
175
if visitor_method is None :
173
176
continue
174
- yield from visitor_method (node , parents , ignore_names )
177
+ yield from visitor_method (node , parents , ignored )
175
178
176
179
def tag_class_functions (self , cls_node ):
177
180
"""Tag functions if they are methods, classmethods, staticmethods"""
@@ -273,9 +276,9 @@ def superclass_names(cls, name, parents: Iterable, _names=None):
273
276
names .update (cls .superclass_names (base .id , parents , names ))
274
277
return names
275
278
276
- def visit_classdef (self , node , parents : Iterable , ignore = None ):
279
+ def visit_classdef (self , node , parents : Iterable , ignored : NameSet ):
277
280
name = node .name
278
- if _ignored ( name , ignore ) :
281
+ if name in ignored :
279
282
return
280
283
name = name .strip ('_' )
281
284
if not name [:1 ].isupper () or '_' in name :
@@ -308,10 +311,10 @@ def has_override_decorator(node):
308
311
return True
309
312
return False
310
313
311
- def visit_functiondef (self , node , parents : Iterable , ignore = None ):
314
+ def visit_functiondef (self , node , parents : Iterable , ignored : NameSet ):
312
315
function_type = getattr (node , 'function_type' , _FunctionType .FUNCTION )
313
316
name = node .name
314
- if _ignored ( name , ignore ) :
317
+ if name in ignored :
315
318
return
316
319
if name in ('__dir__' , '__getattr__' ):
317
320
return
@@ -339,18 +342,18 @@ class FunctionArgNamesCheck(BaseASTCheck):
339
342
N804 = "first argument of a classmethod should be named 'cls'"
340
343
N805 = "first argument of a method should be named 'self'"
341
344
342
- def visit_functiondef (self , node , parents : Iterable , ignore = None ):
345
+ def visit_functiondef (self , node , parents : Iterable , ignored : NameSet ):
343
346
args = node .args .posonlyargs + node .args .args + node .args .kwonlyargs
344
347
345
348
# Start by applying checks that are specific to the first argument.
346
349
#
347
- # Note: The `ignore ` check shouldn't be necessary here because we'd
350
+ # Note: The `ignored ` check shouldn't be necessary here because we'd
348
351
# expect users to explicitly ignore N804/N805 when using names
349
352
# other than `self` and `cls` rather than ignoring names like
350
353
# `klass` to get around these checks. However, a previous
351
354
# implementation allowed for that, so we retain that behavior
352
355
# for backwards compatibility.
353
- if args and (name := args [0 ].arg ) and not _ignored ( name , ignore ) :
356
+ if args and (name := args [0 ].arg ) and name not in ignored :
354
357
function_type = getattr (node , 'function_type' , None )
355
358
if function_type == _FunctionType .METHOD and name != 'self' :
356
359
yield self .err (args [0 ], 'N805' )
@@ -368,7 +371,7 @@ def visit_functiondef(self, node, parents: Iterable, ignore=None):
368
371
369
372
for arg in args :
370
373
name = arg .arg
371
- if name .lower () != name and not _ignored ( name , ignore ) :
374
+ if name .lower () != name and name not in ignored :
372
375
yield self .err (arg , 'N803' , name = name )
373
376
374
377
visit_asyncfunctiondef = visit_functiondef
@@ -384,7 +387,7 @@ class ImportAsCheck(BaseASTCheck):
384
387
N814 = "camelcase '{name}' imported as constant '{asname}'"
385
388
N817 = "camelcase '{name}' imported as acronym '{asname}'"
386
389
387
- def visit_importfrom (self , node , parents : Iterable , ignore = None ):
390
+ def visit_importfrom (self , node , parents : Iterable , ignored : NameSet ):
388
391
for name in node .names :
389
392
asname = name .asname
390
393
if not asname :
@@ -416,7 +419,7 @@ class VariablesCheck(BaseASTCheck):
416
419
N815 = "variable '{name}' in class scope should not be mixedCase"
417
420
N816 = "variable '{name}' in global scope should not be mixedCase"
418
421
419
- def _find_errors (self , assignment_target , parents : Iterable , ignore ):
422
+ def _find_errors (self , assignment_target , parents : Iterable , ignored : NameSet ):
420
423
for parent_func in reversed (parents ):
421
424
if isinstance (parent_func , ast .ClassDef ):
422
425
checker = self .class_variable_check
@@ -427,7 +430,7 @@ def _find_errors(self, assignment_target, parents: Iterable, ignore):
427
430
else :
428
431
checker = self .global_variable_check
429
432
for name in _extract_names (assignment_target ):
430
- if _ignored ( name , ignore ) :
433
+ if name in ignored :
431
434
continue
432
435
error_code = checker (name )
433
436
if error_code :
@@ -444,38 +447,38 @@ def is_namedtupe(node_value):
444
447
return True
445
448
return False
446
449
447
- def visit_assign (self , node , parents : Iterable , ignore = None ):
450
+ def visit_assign (self , node , parents : Iterable , ignored : NameSet ):
448
451
if self .is_namedtupe (node .value ):
449
452
return
450
453
for target in node .targets :
451
- yield from self ._find_errors (target , parents , ignore )
454
+ yield from self ._find_errors (target , parents , ignored )
452
455
453
- def visit_namedexpr (self , node , parents : Iterable , ignore ):
456
+ def visit_namedexpr (self , node , parents : Iterable , ignored : NameSet ):
454
457
if self .is_namedtupe (node .value ):
455
458
return
456
- yield from self ._find_errors (node .target , parents , ignore )
459
+ yield from self ._find_errors (node .target , parents , ignored )
457
460
458
461
visit_annassign = visit_namedexpr
459
462
460
- def visit_with (self , node , parents : Iterable , ignore ):
463
+ def visit_with (self , node , parents : Iterable , ignored : NameSet ):
461
464
for item in node .items :
462
465
yield from self ._find_errors (
463
- item .optional_vars , parents , ignore )
466
+ item .optional_vars , parents , ignored )
464
467
465
468
visit_asyncwith = visit_with
466
469
467
- def visit_for (self , node , parents : Iterable , ignore ):
468
- yield from self ._find_errors (node .target , parents , ignore )
470
+ def visit_for (self , node , parents : Iterable , ignored : NameSet ):
471
+ yield from self ._find_errors (node .target , parents , ignored )
469
472
470
473
visit_asyncfor = visit_for
471
474
472
- def visit_excepthandler (self , node , parents : Iterable , ignore ):
475
+ def visit_excepthandler (self , node , parents : Iterable , ignored : NameSet ):
473
476
if node .name :
474
- yield from self ._find_errors (node , parents , ignore )
477
+ yield from self ._find_errors (node , parents , ignored )
475
478
476
- def visit_generatorexp (self , node , parents : Iterable , ignore ):
479
+ def visit_generatorexp (self , node , parents : Iterable , ignored : NameSet ):
477
480
for gen in node .generators :
478
- yield from self ._find_errors (gen .target , parents , ignore )
481
+ yield from self ._find_errors (gen .target , parents , ignored )
479
482
480
483
visit_listcomp = visit_dictcomp = visit_setcomp = visit_generatorexp
481
484
@@ -502,7 +505,7 @@ class TypeVarNameCheck(BaseASTCheck):
502
505
N808 = "type variable name '{name}' should use CapWords convention " \
503
506
"and an optional '_co' or '_contra' suffix"
504
507
505
- def visit_module (self , node , parents : Iterable , ignore = None ):
508
+ def visit_module (self , node , parents : Iterable , ignored : NameSet ):
506
509
for body in node .body :
507
510
try :
508
511
if len (body .targets ) != 1 :
@@ -514,7 +517,7 @@ def visit_module(self, node, parents: Iterable, ignore=None):
514
517
except AttributeError :
515
518
continue
516
519
517
- if func_name != "TypeVar" or _ignored ( name , ignore ) :
520
+ if func_name != "TypeVar" or name in ignored :
518
521
continue
519
522
520
523
if len (args ) == 0 or args [0 ] != name :
0 commit comments