Skip to content

Commit 064e030

Browse files
committed
Use argparse config handler in imports.py & exceptions.py
1 parent 56c8c80 commit 064e030

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

pylint/checkers/exceptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ class ExceptionsChecker(checkers.BaseChecker):
245245
),
246246
)
247247

248+
def __init__(self, linter: "PyLinter") -> None:
249+
super().__init__(linter, future_option_parsing=True)
250+
248251
def open(self):
249252
self._builtin_exceptions = _builtin_exceptions()
250253
super().open()
@@ -523,7 +526,7 @@ def visit_tryexcept(self, node: nodes.TryExcept) -> None:
523526
"bad-except-order", node=handler.type, args=msg
524527
)
525528
if (
526-
exception.name in self.config.overgeneral_exceptions
529+
exception.name in self.linter.namespace.overgeneral_exceptions
527530
and exception.root().name == utils.EXCEPTIONS_MODULE
528531
and not _is_raising(handler.body)
529532
):

pylint/checkers/format.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ class FormatChecker(BaseTokenChecker):
301301
)
302302

303303
def __init__(self, linter=None):
304-
super().__init__(linter)
304+
super().__init__(linter, future_option_parsing=True)
305305
self._lines = None
306306
self._visited_lines = None
307307
self._bracket_stack = [None]
@@ -498,7 +498,7 @@ def process_tokens(self, tokens):
498498
handler(tokens, idx)
499499

500500
line_num -= 1 # to be ok with "wc -l"
501-
if line_num > self.config.max_module_lines:
501+
if line_num > self.linter.namespace.max_module_lines:
502502
# Get the line where the too-many-lines (or its message id)
503503
# was disabled or default to 1.
504504
message_definition = self.linter.msgs_store.get_message_definitions(
@@ -511,7 +511,7 @@ def process_tokens(self, tokens):
511511
)
512512
self.add_message(
513513
"too-many-lines",
514-
args=(line_num, self.config.max_module_lines),
514+
args=(line_num, self.linter.namespace.max_module_lines),
515515
line=line,
516516
)
517517

@@ -532,7 +532,7 @@ def _check_line_ending(self, line_ending, line_num):
532532
self._last_line_ending = line_ending
533533

534534
# check if line ending is as expected
535-
expected = self.config.expected_line_ending_format
535+
expected = self.linter.namespace.expected_line_ending_format
536536
if expected:
537537
# reduce multiple \n\n\n\n to one \n
538538
line_ending = reduce(lambda x, y: x + y if x != y else x, line_ending, "")
@@ -601,13 +601,13 @@ def _check_multi_statement_line(self, node, line):
601601
if (
602602
isinstance(node.parent, nodes.If)
603603
and not node.parent.orelse
604-
and self.config.single_line_if_stmt
604+
and self.linter.namespace.single_line_if_stmt
605605
):
606606
return
607607
if (
608608
isinstance(node.parent, nodes.ClassDef)
609609
and len(node.parent.body) == 1
610-
and self.config.single_line_class_stmt
610+
and self.linter.namespace.single_line_class_stmt
611611
):
612612
return
613613

@@ -638,8 +638,8 @@ def check_line_ending(self, line: str, i: int) -> None:
638638

639639
def check_line_length(self, line: str, i: int, checker_off: bool) -> None:
640640
"""Check that the line length is less than the authorized value."""
641-
max_chars = self.config.max_line_length
642-
ignore_long_line = self.config.ignore_long_lines
641+
max_chars = self.linter.namespace.max_line_length
642+
ignore_long_line = self.linter.namespace.ignore_long_lines
643643
line = line.rstrip()
644644
if len(line) > max_chars and not ignore_long_line.search(line):
645645
if checker_off:
@@ -708,7 +708,7 @@ def check_lines(self, lines: str, lineno: int) -> None:
708708
# we'll also handle the line ending check here to avoid double-iteration
709709
# unless the line lengths are suspect
710710

711-
max_chars = self.config.max_line_length
711+
max_chars = self.linter.namespace.max_line_length
712712

713713
split_lines = self.specific_splitlines(lines)
714714

@@ -745,7 +745,7 @@ def check_lines(self, lines: str, lineno: int) -> None:
745745

746746
def check_indent_level(self, string, expected, line_num):
747747
"""Return the indent level of the string."""
748-
indent = self.config.indent_string
748+
indent = self.linter.namespace.indent_string
749749
if indent == "\\t": # \t is not interpreted in the configuration file
750750
indent = "\t"
751751
level = 0

pylint/config/argument.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,18 @@ def _yn_transformer(value: str) -> bool:
3939
)
4040

4141

42+
def _non_empty_string_transformer(value: str) -> str:
43+
"""Check that a string is not empty and remove quotes."""
44+
if not value:
45+
raise argparse.ArgumentTypeError("Option cannot be an empty string.")
46+
return pylint_utils._unquote(value)
47+
48+
4249
_TYPE_TRANSFORMERS: Dict[str, Callable[[str], _ArgumentTypes]] = {
4350
"choice": str,
4451
"csv": _csv_transformer,
4552
"int": int,
53+
"non_empty_string": _non_empty_string_transformer,
4654
"regexp": re.compile,
4755
"string": str,
4856
"yn": _yn_transformer,

0 commit comments

Comments
 (0)