Skip to content

Commit

Permalink
Use argparse config handler in similar.py
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Apr 2, 2022
1 parent 968b513 commit 57319fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pylint/checkers/base_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def __init__(
"""
if self.name is not None:
self.name = self.name.lower()
super().__init__()
self.linter = linter
super().__init__()

if future_option_parsing:
# We need a PyLinter object that subclasses _ArgumentsManager to register options
Expand Down
49 changes: 29 additions & 20 deletions pylint/checkers/similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,32 +781,49 @@ class SimilarChecker(BaseChecker, Similar, MapReduceMixin):
reports = (("RP0801", "Duplication", report_similarities),)

def __init__(self, linter=None) -> None:
BaseChecker.__init__(self, linter)
BaseChecker.__init__(self, linter, future_option_parsing=True)
Similar.__init__(
self,
min_lines=self.config.min_similarity_lines,
ignore_comments=self.config.ignore_comments,
ignore_docstrings=self.config.ignore_docstrings,
ignore_imports=self.config.ignore_imports,
ignore_signatures=self.config.ignore_signatures,
min_lines=self.linter.namespace.min_similarity_lines,
ignore_comments=self.linter.namespace.ignore_comments,
ignore_docstrings=self.linter.namespace.ignore_docstrings,
ignore_imports=self.linter.namespace.ignore_imports,
ignore_signatures=self.linter.namespace.ignore_signatures,
)

def set_option(self, optname, value, action=None, optdict=None):
"""Method called to set an option (registered in the options list).
Overridden to report options setting to Similar
"""
# pylint: disable-next=fixme
# TODO: Refactor after OptionProvider has been moved to argparse
BaseChecker.set_option(self, optname, value, action, optdict)
if optname == "min-similarity-lines":
self.min_lines = self.config.min_similarity_lines
self.min_lines = (
getattr(self.linter.namespace, "min_similarity_lines", None)
or self.config.min_similarity_lines
)
elif optname == "ignore-comments":
self.ignore_comments = self.config.ignore_comments
self.ignore_comments = (
getattr(self.linter.namespace, "ignore_comments", None)
or self.config.ignore_comments
)
elif optname == "ignore-docstrings":
self.ignore_docstrings = self.config.ignore_docstrings
self.ignore_docstrings = (
getattr(self.linter.namespace, "ignore_docstrings", None)
or self.config.ignore_docstrings
)
elif optname == "ignore-imports":
self.ignore_imports = self.config.ignore_imports
self.ignore_imports = (
getattr(self.linter.namespace, "ignore_imports", None)
or self.config.ignore_imports
)
elif optname == "ignore-signatures":
self.ignore_signatures = self.config.ignore_signatures
self.ignore_signatures = (
getattr(self.linter.namespace, "ignore_signatures", None)
or self.config.ignore_signatures
)

def open(self):
"""Init the checkers: reset linesets and statistics information."""
Expand Down Expand Up @@ -861,15 +878,7 @@ def reduce_map_data(self, linter, data):
The partner function of get_map_data()
"""
recombined = SimilarChecker(linter)
recombined.min_lines = self.min_lines
recombined.ignore_comments = self.ignore_comments
recombined.ignore_docstrings = self.ignore_docstrings
recombined.ignore_imports = self.ignore_imports
recombined.ignore_signatures = self.ignore_signatures
recombined.open()
Similar.combine_mapreduce_data(recombined, linesets_collection=data)
recombined.close()
Similar.combine_mapreduce_data(self, linesets_collection=data)


def register(linter: "PyLinter") -> None:
Expand Down
2 changes: 1 addition & 1 deletion tests/checkers/unittest_similar.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def test_get_map_data() -> None:

# Manually perform a 'map' type function
for source_fname in source_streams:
sim = similar.SimilarChecker(linter)
sim = similar.SimilarChecker(PyLinter())
with open(source_fname, encoding="utf-8") as stream:
sim.append_stream(source_fname, stream)
# The map bit, can you tell? ;)
Expand Down

0 comments on commit 57319fa

Please sign in to comment.