Skip to content

Commit b7a9499

Browse files
authored
Use the argparse config handler in class_checker.py (#6109)
1 parent f3cca6e commit b7a9499

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

pylint/checkers/classes/class_checker.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ class ClassChecker(BaseChecker):
760760
)
761761

762762
def __init__(self, linter=None):
763-
super().__init__(linter)
763+
super().__init__(linter, future_option_parsing=True)
764764
self._accessed = ScopeAccessMap()
765765
self._first_attrs = []
766766
self._meth_could_be_func = None
@@ -1019,7 +1019,7 @@ def _check_attribute_defined_outside_init(self, cnode: nodes.ClassDef) -> None:
10191019
# checks attributes are defined in an allowed method such as __init__
10201020
if not self.linter.is_message_enabled("attribute-defined-outside-init"):
10211021
return
1022-
defining_methods = self.config.defining_attr_methods
1022+
defining_methods = self.linter.namespace.defining_attr_methods
10231023
current_module = cnode.root()
10241024
for attr, nodes_lst in cnode.instance_attrs.items():
10251025
# Exclude `__dict__` as it is already defined.
@@ -1629,7 +1629,7 @@ def _check_protected_attribute_access(self, node: nodes.Attribute):
16291629

16301630
if (
16311631
is_attr_protected(attrname)
1632-
and attrname not in self.config.exclude_protected
1632+
and attrname not in self.linter.namespace.exclude_protected
16331633
):
16341634

16351635
klass = node_frame_class(node)
@@ -1700,7 +1700,7 @@ def _check_protected_attribute_access(self, node: nodes.Attribute):
17001700

17011701
licit_protected_member = not attrname.startswith("__")
17021702
if (
1703-
not self.config.check_protected_access_in_special_methods
1703+
not self.linter.namespace.check_protected_access_in_special_methods
17041704
and licit_protected_member
17051705
and self._is_called_inside_special_method(node)
17061706
):
@@ -1855,8 +1855,9 @@ def _check_first_arg_for_type(self, node, metaclass=0):
18551855
if node.type == "staticmethod":
18561856
if (
18571857
first_arg == "self"
1858-
or first_arg in self.config.valid_classmethod_first_arg
1859-
or first_arg in self.config.valid_metaclass_classmethod_first_arg
1858+
or first_arg in self.linter.namespace.valid_classmethod_first_arg
1859+
or first_arg
1860+
in self.linter.namespace.valid_metaclass_classmethod_first_arg
18601861
):
18611862
self.add_message("bad-staticmethod-argument", args=first, node=node)
18621863
return
@@ -1870,7 +1871,7 @@ def _check_first_arg_for_type(self, node, metaclass=0):
18701871
if node.type == "classmethod":
18711872
self._check_first_arg_config(
18721873
first,
1873-
self.config.valid_metaclass_classmethod_first_arg,
1874+
self.linter.namespace.valid_metaclass_classmethod_first_arg,
18741875
node,
18751876
"bad-mcs-classmethod-argument",
18761877
node.name,
@@ -1879,7 +1880,7 @@ def _check_first_arg_for_type(self, node, metaclass=0):
18791880
else:
18801881
self._check_first_arg_config(
18811882
first,
1882-
self.config.valid_classmethod_first_arg,
1883+
self.linter.namespace.valid_classmethod_first_arg,
18831884
node,
18841885
"bad-mcs-method-argument",
18851886
node.name,
@@ -1888,7 +1889,7 @@ def _check_first_arg_for_type(self, node, metaclass=0):
18881889
elif node.type == "classmethod" or node.name == "__class_getitem__":
18891890
self._check_first_arg_config(
18901891
first,
1891-
self.config.valid_classmethod_first_arg,
1892+
self.linter.namespace.valid_classmethod_first_arg,
18921893
node,
18931894
"bad-classmethod-argument",
18941895
node.name,

pylint/config/argument.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
"""
99

1010

11+
import argparse
1112
import re
1213
from typing import Callable, Dict, List, Optional, Pattern, Sequence, Union
1314

1415
from pylint import utils as pylint_utils
1516

16-
_ArgumentTypes = Union[str, Sequence[str], int, Pattern[str]]
17+
_ArgumentTypes = Union[str, Sequence[str], int, Pattern[str], bool]
1718
"""List of possible argument types."""
1819

1920

@@ -22,11 +23,28 @@ def _csv_transformer(value: str) -> Sequence[str]:
2223
return pylint_utils._check_csv(value)
2324

2425

26+
YES_VALUES = {"y", "yes", "true"}
27+
NO_VALUES = {"n", "no", "false"}
28+
29+
30+
def _yn_transformer(value: str) -> bool:
31+
"""Transforms a yes/no or stringified bool into a bool."""
32+
value = value.lower()
33+
if value in YES_VALUES:
34+
return True
35+
if value in NO_VALUES:
36+
return False
37+
raise argparse.ArgumentError(
38+
None, f"Invalid yn value '{value}', should be in {*YES_VALUES, *NO_VALUES}"
39+
)
40+
41+
2542
_TYPE_TRANSFORMERS: Dict[str, Callable[[str], _ArgumentTypes]] = {
2643
"choice": str,
2744
"csv": _csv_transformer,
2845
"int": int,
2946
"regexp": re.compile,
47+
"yn": _yn_transformer,
3048
}
3149
"""Type transformers for all argument types.
3250

0 commit comments

Comments
 (0)