Skip to content

Commit d1fc4af

Browse files
authored
Use argparse config handler in design_analysis.py (#6118)
1 parent 064e030 commit d1fc4af

File tree

2 files changed

+37
-23
lines changed

2 files changed

+37
-23
lines changed

pylint/checkers/design_analysis.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ class MisdesignChecker(BaseChecker):
403403
)
404404

405405
def __init__(self, linter=None):
406-
super().__init__(linter)
406+
super().__init__(linter, future_option_parsing=True)
407407
self._returns = None
408408
self._branches = None
409409
self._stmts = None
@@ -435,21 +435,22 @@ def _ignored_argument_names(self):
435435
def visit_classdef(self, node: nodes.ClassDef) -> None:
436436
"""Check size of inheritance hierarchy and number of instance attributes."""
437437
parents = _get_parents(
438-
node, STDLIB_CLASSES_IGNORE_ANCESTOR.union(self.config.ignored_parents)
438+
node,
439+
STDLIB_CLASSES_IGNORE_ANCESTOR.union(self.linter.namespace.ignored_parents),
439440
)
440441
nb_parents = len(parents)
441-
if nb_parents > self.config.max_parents:
442+
if nb_parents > self.linter.namespace.max_parents:
442443
self.add_message(
443444
"too-many-ancestors",
444445
node=node,
445-
args=(nb_parents, self.config.max_parents),
446+
args=(nb_parents, self.linter.namespace.max_parents),
446447
)
447448

448-
if len(node.instance_attrs) > self.config.max_attributes:
449+
if len(node.instance_attrs) > self.linter.namespace.max_attributes:
449450
self.add_message(
450451
"too-many-instance-attributes",
451452
node=node,
452-
args=(len(node.instance_attrs), self.config.max_attributes),
453+
args=(len(node.instance_attrs), self.linter.namespace.max_attributes),
453454
)
454455

455456
@check_messages("too-few-public-methods", "too-many-public-methods")
@@ -466,11 +467,11 @@ def leave_classdef(self, node: nodes.ClassDef) -> None:
466467
# for classes such as unittest.TestCase, which provides
467468
# a lot of assert methods. It doesn't make sense to warn
468469
# when the user subclasses TestCase to add his own tests.
469-
if my_methods > self.config.max_public_methods:
470+
if my_methods > self.linter.namespace.max_public_methods:
470471
self.add_message(
471472
"too-many-public-methods",
472473
node=node,
473-
args=(my_methods, self.config.max_public_methods),
474+
args=(my_methods, self.linter.namespace.max_public_methods),
474475
)
475476

476477
# Stop here if the class is excluded via configuration.
@@ -491,11 +492,11 @@ def leave_classdef(self, node: nodes.ClassDef) -> None:
491492
# This checks all the methods defined by ancestors and
492493
# by the current class.
493494
all_methods = _count_methods_in_class(node)
494-
if all_methods < self.config.min_public_methods:
495+
if all_methods < self.linter.namespace.min_public_methods:
495496
self.add_message(
496497
"too-few-public-methods",
497498
node=node,
498-
args=(all_methods, self.config.min_public_methods),
499+
args=(all_methods, self.linter.namespace.min_public_methods),
499500
)
500501

501502
@check_messages(
@@ -523,19 +524,21 @@ def visit_functiondef(self, node: nodes.FunctionDef) -> None:
523524
)
524525

525526
argnum = len(args) - ignored_args_num
526-
if argnum > self.config.max_args:
527+
if argnum > self.linter.namespace.max_args:
527528
self.add_message(
528529
"too-many-arguments",
529530
node=node,
530-
args=(len(args), self.config.max_args),
531+
args=(len(args), self.linter.namespace.max_args),
531532
)
532533
else:
533534
ignored_args_num = 0
534535
# check number of local variables
535536
locnum = len(node.locals) - ignored_args_num
536-
if locnum > self.config.max_locals:
537+
if locnum > self.linter.namespace.max_locals:
537538
self.add_message(
538-
"too-many-locals", node=node, args=(locnum, self.config.max_locals)
539+
"too-many-locals",
540+
node=node,
541+
args=(locnum, self.linter.namespace.max_locals),
539542
)
540543
# init new statements counter
541544
self._stmts.append(1)
@@ -554,26 +557,26 @@ def leave_functiondef(self, node: nodes.FunctionDef) -> None:
554557
checks for max returns, branch, return in __init__
555558
"""
556559
returns = self._returns.pop()
557-
if returns > self.config.max_returns:
560+
if returns > self.linter.namespace.max_returns:
558561
self.add_message(
559562
"too-many-return-statements",
560563
node=node,
561-
args=(returns, self.config.max_returns),
564+
args=(returns, self.linter.namespace.max_returns),
562565
)
563566
branches = self._branches[node]
564-
if branches > self.config.max_branches:
567+
if branches > self.linter.namespace.max_branches:
565568
self.add_message(
566569
"too-many-branches",
567570
node=node,
568-
args=(branches, self.config.max_branches),
571+
args=(branches, self.linter.namespace.max_branches),
569572
)
570573
# check number of statements
571574
stmts = self._stmts.pop()
572-
if stmts > self.config.max_statements:
575+
if stmts > self.linter.namespace.max_statements:
573576
self.add_message(
574577
"too-many-statements",
575578
node=node,
576-
args=(stmts, self.config.max_statements),
579+
args=(stmts, self.linter.namespace.max_statements),
577580
)
578581

579582
leave_asyncfunctiondef = leave_functiondef
@@ -625,11 +628,11 @@ def _check_boolean_expressions(self, node):
625628
if not isinstance(condition, astroid.BoolOp):
626629
return
627630
nb_bool_expr = _count_boolean_expressions(condition)
628-
if nb_bool_expr > self.config.max_bool_expr:
631+
if nb_bool_expr > self.linter.namespace.max_bool_expr:
629632
self.add_message(
630633
"too-many-boolean-expressions",
631634
node=condition,
632-
args=(nb_bool_expr, self.config.max_bool_expr),
635+
args=(nb_bool_expr, self.linter.namespace.max_bool_expr),
633636
)
634637

635638
def visit_while(self, node: nodes.While) -> None:

pylint/config/argument.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
from pylint import utils as pylint_utils
1616

17-
_ArgumentTypes = Union[str, Sequence[str], int, Pattern[str], bool]
17+
_ArgumentTypes = Union[
18+
str, Sequence[str], int, Pattern[str], bool, Sequence[Pattern[str]]
19+
]
1820
"""List of possible argument types."""
1921

2022

@@ -46,12 +48,21 @@ def _non_empty_string_transformer(value: str) -> str:
4648
return pylint_utils._unquote(value)
4749

4850

51+
def _regexp_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
52+
"""Transforms a comma separated list of regular expressions."""
53+
patterns: List[Pattern[str]] = []
54+
for pattern in _csv_transformer(value):
55+
patterns.append(re.compile(pattern))
56+
return patterns
57+
58+
4959
_TYPE_TRANSFORMERS: Dict[str, Callable[[str], _ArgumentTypes]] = {
5060
"choice": str,
5161
"csv": _csv_transformer,
5262
"int": int,
5363
"non_empty_string": _non_empty_string_transformer,
5464
"regexp": re.compile,
65+
"regexp_csv": _regexp_csv_transfomer,
5566
"string": str,
5667
"yn": _yn_transformer,
5768
}

0 commit comments

Comments
 (0)