Skip to content

Commit ae524f3

Browse files
committed
Use the argparse config handler in imports.py
1 parent b7a9499 commit ae524f3

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
lines changed

pylint/checkers/imports.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import collections
88
import copy
99
import os
10-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set, Tuple, Union
10+
from typing import TYPE_CHECKING, Any, Dict, List, Set, Tuple, Union
1111

1212
import astroid
1313
from astroid import nodes
@@ -383,8 +383,8 @@ class ImportsChecker(DeprecatedMixin, BaseChecker):
383383
),
384384
)
385385

386-
def __init__(self, linter: Optional["PyLinter"] = None) -> None:
387-
BaseChecker.__init__(self, linter)
386+
def __init__(self, linter: "PyLinter") -> None:
387+
BaseChecker.__init__(self, linter, future_option_parsing=True)
388388
self.import_graph: collections.defaultdict = collections.defaultdict(set)
389389
self._imports_stack: List[Tuple[Any, Any]] = []
390390
self._first_non_import_node = None
@@ -408,10 +408,10 @@ def open(self):
408408
# Build a mapping {'module': 'preferred-module'}
409409
self.preferred_modules = dict(
410410
module.split(":")
411-
for module in self.config.preferred_modules
411+
for module in self.linter.namespace.preferred_modules
412412
if ":" in module
413413
)
414-
self._allow_any_import_level = set(self.config.allow_any_import_level)
414+
self._allow_any_import_level = set(self.linter.namespace.allow_any_import_level)
415415

416416
def _import_graph_without_ignored_edges(self):
417417
filtered_graph = copy.deepcopy(self.import_graph)
@@ -429,7 +429,7 @@ def close(self):
429429

430430
def deprecated_modules(self):
431431
"""Callback returning the deprecated modules."""
432-
return self.config.deprecated_modules
432+
return self.linter.namespace.deprecated_modules
433433

434434
@check_messages(*MSGS)
435435
def visit_import(self, node: nodes.Import) -> None:
@@ -651,7 +651,7 @@ def _check_imports_order(self, _module_node):
651651
third_party_not_ignored = []
652652
first_party_not_ignored = []
653653
local_not_ignored = []
654-
isort_driver = IsortDriver(self.config)
654+
isort_driver = IsortDriver(self.linter.namespace)
655655
for node, modname in self._imports_stack:
656656
if modname.startswith("."):
657657
package = "." + modname.split(".")[1]
@@ -748,8 +748,9 @@ def _get_imported_module(self, importnode, modname):
748748
return None
749749
if _ignore_import_failure(importnode, modname, self._ignored_modules):
750750
return None
751-
if not self.config.analyse_fallback_blocks and is_from_fallback_block(
752-
importnode
751+
if (
752+
not self.linter.namespace.analyse_fallback_blocks
753+
and is_from_fallback_block(importnode)
753754
):
754755
return None
755756

@@ -865,18 +866,18 @@ def _report_dependencies_graph(self, sect, _, _dummy):
865866
"""Write dependencies as a dot (graphviz) file."""
866867
dep_info = self.linter.stats.dependencies
867868
if not dep_info or not (
868-
self.config.import_graph
869-
or self.config.ext_import_graph
870-
or self.config.int_import_graph
869+
self.linter.namespace.import_graph
870+
or self.linter.namespace.ext_import_graph
871+
or self.linter.namespace.int_import_graph
871872
):
872873
raise EmptyReportError()
873-
filename = self.config.import_graph
874+
filename = self.linter.namespace.import_graph
874875
if filename:
875876
_make_graph(filename, dep_info, sect, "")
876-
filename = self.config.ext_import_graph
877+
filename = self.linter.namespace.ext_import_graph
877878
if filename:
878879
_make_graph(filename, self._external_dependencies_info(), sect, "external ")
879-
filename = self.config.int_import_graph
880+
filename = self.linter.namespace.int_import_graph
880881
if filename:
881882
_make_graph(filename, self._internal_dependencies_info(), sect, "internal ")
882883

@@ -917,7 +918,7 @@ def _check_wildcard_imports(self, node, imported_module):
917918

918919
def _wildcard_import_is_allowed(self, imported_module):
919920
return (
920-
self.config.allow_wildcard_with_all
921+
self.linter.namespace.allow_wildcard_with_all
921922
and imported_module is not None
922923
and "__all__" in imported_module.locals
923924
)

pylint/config/argument.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def _yn_transformer(value: str) -> bool:
3434
return True
3535
if value in NO_VALUES:
3636
return False
37-
raise argparse.ArgumentError(
37+
raise argparse.ArgumentTypeError(
3838
None, f"Invalid yn value '{value}', should be in {*YES_VALUES, *NO_VALUES}"
3939
)
4040

@@ -44,6 +44,7 @@ def _yn_transformer(value: str) -> bool:
4444
"csv": _csv_transformer,
4545
"int": int,
4646
"regexp": re.compile,
47+
"string": str,
4748
"yn": _yn_transformer,
4849
}
4950
"""Type transformers for all argument types.

pylint/testutils/unittest_linter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66

77
from astroid import nodes
88

9+
from pylint import config
910
from pylint.interfaces import UNDEFINED, Confidence
1011
from pylint.testutils.global_test_linter import linter
1112
from pylint.testutils.output_line import MessageTest
1213
from pylint.utils import LinterStats
1314

1415

15-
class UnittestLinter:
16+
class UnittestLinter(config._ArgumentsManager):
1617
"""A fake linter class to capture checker messages."""
1718

1819
# pylint: disable=unused-argument
1920

2021
def __init__(self):
2122
self._messages = []
2223
self.stats = LinterStats()
24+
config._ArgumentsManager.__init__(self)
2325

2426
def release_messages(self):
2527
try:

pylint/utils/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
HAS_ISORT_5 = False
1313

14+
import argparse
1415
import codecs
1516
import os
1617
import re
@@ -382,7 +383,7 @@ def _ini_format(stream: TextIO, options: List[Tuple]) -> None:
382383
class IsortDriver:
383384
"""A wrapper around isort API that changed between versions 4 and 5."""
384385

385-
def __init__(self, config) -> None:
386+
def __init__(self, config: argparse.Namespace) -> None:
386387
if HAS_ISORT_5:
387388
self.isort5_config = isort.api.Config(
388389
# There is no typo here. EXTRA_standard_library is

tests/test_import_graph.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ def remove_files() -> Iterator:
8181
pass
8282

8383

84+
# pylint: disable-next=fixme
85+
# TODO: Fix these tests after all necessary options support the argparse framework
86+
@pytest.mark.xfail(reason="Not all options support argparse parsing")
8487
@pytest.mark.usefixtures("remove_files")
8588
def test_checker_dep_graphs(linter: PyLinter) -> None:
8689
linter.global_set_option("persistent", False)

0 commit comments

Comments
 (0)