Skip to content

Use the argparse config handler in class_checker.py #6109

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

Merged
merged 4 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 10 additions & 9 deletions pylint/checkers/classes/class_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ class ClassChecker(BaseChecker):
)

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

if (
is_attr_protected(attrname)
and attrname not in self.config.exclude_protected
and attrname not in self.linter.namespace.exclude_protected
):

klass = node_frame_class(node)
Expand Down Expand Up @@ -1700,7 +1700,7 @@ def _check_protected_attribute_access(self, node: nodes.Attribute):

licit_protected_member = not attrname.startswith("__")
if (
not self.config.check_protected_access_in_special_methods
not self.linter.namespace.check_protected_access_in_special_methods
and licit_protected_member
and self._is_called_inside_special_method(node)
):
Expand Down Expand Up @@ -1855,8 +1855,9 @@ def _check_first_arg_for_type(self, node, metaclass=0):
if node.type == "staticmethod":
if (
first_arg == "self"
or first_arg in self.config.valid_classmethod_first_arg
or first_arg in self.config.valid_metaclass_classmethod_first_arg
or first_arg in self.linter.namespace.valid_classmethod_first_arg
or first_arg
in self.linter.namespace.valid_metaclass_classmethod_first_arg
):
self.add_message("bad-staticmethod-argument", args=first, node=node)
return
Expand All @@ -1870,7 +1871,7 @@ def _check_first_arg_for_type(self, node, metaclass=0):
if node.type == "classmethod":
self._check_first_arg_config(
first,
self.config.valid_metaclass_classmethod_first_arg,
self.linter.namespace.valid_metaclass_classmethod_first_arg,
node,
"bad-mcs-classmethod-argument",
node.name,
Expand All @@ -1879,7 +1880,7 @@ def _check_first_arg_for_type(self, node, metaclass=0):
else:
self._check_first_arg_config(
first,
self.config.valid_classmethod_first_arg,
self.linter.namespace.valid_classmethod_first_arg,
node,
"bad-mcs-method-argument",
node.name,
Expand All @@ -1888,7 +1889,7 @@ def _check_first_arg_for_type(self, node, metaclass=0):
elif node.type == "classmethod" or node.name == "__class_getitem__":
self._check_first_arg_config(
first,
self.config.valid_classmethod_first_arg,
self.linter.namespace.valid_classmethod_first_arg,
node,
"bad-classmethod-argument",
node.name,
Expand Down
20 changes: 19 additions & 1 deletion pylint/config/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
"""


import argparse
import re
from typing import Callable, Dict, List, Optional, Pattern, Sequence, Union

from pylint import utils as pylint_utils

_ArgumentTypes = Union[str, Sequence[str], int, Pattern[str]]
_ArgumentTypes = Union[str, Sequence[str], int, Pattern[str], bool]
"""List of possible argument types."""


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


YES_VALUES = {"y", "yes", "true"}
NO_VALUES = {"n", "no", "false"}


def _yn_transformer(value: str) -> bool:
"""Transforms a yes/no or stringified bool into a bool."""
value = value.lower()
if value in YES_VALUES:
return True
if value in NO_VALUES:
return False
raise argparse.ArgumentError(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be covered as soon as we flip the switch. optparse now raises a warning here.

None, f"Invalid yn value '{value}', should be in {*YES_VALUES, *NO_VALUES}"
)


_TYPE_TRANSFORMERS: Dict[str, Callable[[str], _ArgumentTypes]] = {
"choice": str,
"csv": _csv_transformer,
"int": int,
"regexp": re.compile,
"yn": _yn_transformer,
}
"""Type transformers for all argument types.

Expand Down