Skip to content

Commit

Permalink
Use argparse config handler on three checkers
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Apr 5, 2022
1 parent 828a0d9 commit 48e6585
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
6 changes: 3 additions & 3 deletions pylint/checkers/base/docstring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ def visit_module(self, node: nodes.Module) -> None:

@utils.check_messages("missing-docstring", "empty-docstring")
def visit_classdef(self, node: nodes.ClassDef) -> None:
if self.config.no_docstring_rgx.match(node.name) is None:
if self.linter.namespace.no_docstring_rgx.match(node.name) is None:
self._check_docstring("class", node)

@utils.check_messages("missing-docstring", "empty-docstring")
def visit_functiondef(self, node: nodes.FunctionDef) -> None:
if self.config.no_docstring_rgx.match(node.name) is None:
if self.linter.namespace.no_docstring_rgx.match(node.name) is None:
ftype = "method" if node.is_method() else "function"
if (
is_property_setter(node)
Expand Down Expand Up @@ -176,7 +176,7 @@ def _check_docstring(
# If the module does not have a body, there's no reason
# to require a docstring.
return
max_lines = self.config.docstring_min_length
max_lines = self.linter.namespace.docstring_min_length

if node_type != "module" and max_lines > -1 and lines < max_lines:
return
Expand Down
13 changes: 8 additions & 5 deletions pylint/checkers/refactoring/refactoring_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ class RefactoringChecker(checkers.BaseTokenChecker):
{
"default": ("sys.exit", "argparse.parse_error"),
"type": "csv",
"metavar": "<members names>",
"help": "Complete name of functions that never returns. When checking "
"for inconsistent-return-statements if a never returning function is "
"called then it will be considered as an explicit return statement "
Expand All @@ -469,8 +470,8 @@ class RefactoringChecker(checkers.BaseTokenChecker):
),
)

def __init__(self, linter=None):
super().__init__(linter)
def __init__(self, linter):
super().__init__(linter, future_option_parsing=True)
self._return_nodes = {}
self._consider_using_with_stack = ConsiderUsingWithStack()
self._init()
Expand All @@ -486,7 +487,9 @@ def _init(self):

def open(self):
# do this in open since config not fully initialized in __init__
self._never_returning_functions = set(self.config.never_returning_functions)
self._never_returning_functions = set(
self.linter.namespace.never_returning_functions
)

@cached_property
def _dummy_rgx(self):
Expand Down Expand Up @@ -1125,11 +1128,11 @@ def _check_nested_blocks(self, node):
self._emit_nested_blocks_message_if_needed(nested_blocks)

def _emit_nested_blocks_message_if_needed(self, nested_blocks):
if len(nested_blocks) > self.config.max_nested_blocks:
if len(nested_blocks) > self.linter.namespace.max_nested_blocks:
self.add_message(
"too-many-nested-blocks",
node=nested_blocks[0],
args=(len(nested_blocks), self.config.max_nested_blocks),
args=(len(nested_blocks), self.linter.namespace.max_nested_blocks),
)

def _emit_consider_using_with_if_needed(self, stack: Dict[str, nodes.NodeNG]):
Expand Down
28 changes: 16 additions & 12 deletions pylint/checkers/spelling.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,44 +263,48 @@ class SpellingChecker(BaseTokenChecker):
),
)

def __init__(self, linter: "PyLinter") -> None:
super().__init__(linter, future_option_parsing=True)

def open(self):
self.initialized = False
self.private_dict_file = None

if enchant is None:
return
dict_name = self.config.spelling_dict
dict_name = self.linter.namespace.spelling_dict
if not dict_name:
return

self.ignore_list = [
w.strip() for w in self.config.spelling_ignore_words.split(",")
w.strip() for w in self.linter.namespace.spelling_ignore_words.split(",")
]
# "param" appears in docstring in param description and
# "pylint" appears in comments in pylint pragmas.
self.ignore_list.extend(["param", "pylint"])

self.ignore_comment_directive_list = [
w.strip() for w in self.config.spelling_ignore_comment_directives.split(",")
w.strip()
for w in self.linter.namespace.spelling_ignore_comment_directives.split(",")
]

# Expand tilde to allow e.g. spelling-private-dict-file = ~/.pylintdict
if self.config.spelling_private_dict_file:
self.config.spelling_private_dict_file = os.path.expanduser(
self.config.spelling_private_dict_file
if self.linter.namespace.spelling_private_dict_file:
self.linter.namespace.spelling_private_dict_file = os.path.expanduser(
self.linter.namespace.spelling_private_dict_file
)

if self.config.spelling_private_dict_file:
if self.linter.namespace.spelling_private_dict_file:
self.spelling_dict = enchant.DictWithPWL(
dict_name, self.config.spelling_private_dict_file
dict_name, self.linter.namespace.spelling_private_dict_file
)
self.private_dict_file = open( # pylint: disable=consider-using-with
self.config.spelling_private_dict_file, "a", encoding="utf-8"
self.linter.namespace.spelling_private_dict_file, "a", encoding="utf-8"
)
else:
self.spelling_dict = enchant.Dict(dict_name)

if self.config.spelling_store_unknown_words:
if self.linter.namespace.spelling_store_unknown_words:
self.unknown_words = set()

self.tokenizer = get_tokenizer(
Expand Down Expand Up @@ -374,14 +378,14 @@ def _check_spelling(self, msgid, line, line_num):
continue

# Store word to private dict or raise a message.
if self.config.spelling_store_unknown_words:
if self.linter.namespace.spelling_store_unknown_words:
if lower_cased_word not in self.unknown_words:
self.private_dict_file.write(f"{lower_cased_word}\n")
self.unknown_words.add(lower_cased_word)
else:
# Present up to N suggestions.
suggestions = self.spelling_dict.suggest(word)
del suggestions[self.config.max_spelling_suggestions :]
del suggestions[self.linter.namespace.max_spelling_suggestions :]
line_segment = line[word_start_at:]
match = re.search(rf"(\W|^)({word})(\W|$)", line_segment)
if match:
Expand Down

0 comments on commit 48e6585

Please sign in to comment.