Skip to content

Commit efca390

Browse files
committed
Fix confidence argument type
1 parent 3b8a020 commit efca390

5 files changed

Lines changed: 25 additions & 6 deletions

File tree

pylint/config/argument.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import re
1414
from typing import Callable, Dict, List, Optional, Pattern, Sequence, Tuple, Union
1515

16+
from pylint import interfaces
1617
from pylint import utils as pylint_utils
1718

1819
_ArgumentTypes = Union[
@@ -28,6 +29,17 @@
2829
"""List of possible argument types."""
2930

3031

32+
def _confidence_transformer(value: str) -> Sequence[str]:
33+
"""Transforms a comma separated string of confidence values."""
34+
values = pylint_utils._check_csv(value)
35+
for confidence in values:
36+
if confidence not in interfaces.CONFIDENCE_LEVEL_NAMES:
37+
raise argparse.ArgumentTypeError(
38+
f"{value} should be in {*interfaces.CONFIDENCE_LEVEL_NAMES,}"
39+
)
40+
return values
41+
42+
3143
def _csv_transformer(value: str) -> Sequence[str]:
3244
"""Transforms a comma separated string."""
3345
return pylint_utils._check_csv(value)
@@ -94,7 +106,7 @@ def _regexp_paths_csv_transfomer(value: str) -> Sequence[Pattern[str]]:
94106
"csv": _csv_transformer,
95107
"float": float,
96108
"int": int,
97-
"multiple_choice": _csv_transformer,
109+
"confidence": _confidence_transformer,
98110
"non_empty_string": _non_empty_string_transformer,
99111
"py_version": _py_version_transformer,
100112
"regexp": re.compile,

pylint/config/option.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ def _py_version_validator(_, name, value):
106106
"csv": _csv_validator,
107107
"yn": _yn_validator,
108108
"choice": lambda opt, name, value: _choice_validator(opt["choices"], name, value),
109+
"confidence": lambda opt, name, value: _multiple_choice_validator(
110+
opt["choices"], name, value
111+
),
109112
"multiple_choice": lambda opt, name, value: _multiple_choice_validator(
110113
opt["choices"], name, value
111114
),
@@ -148,6 +151,7 @@ class Option(optparse.Option):
148151
"regexp_paths_csv",
149152
"csv",
150153
"yn",
154+
"confidence",
151155
"multiple_choice",
152156
"non_empty_string",
153157
"py_version",
@@ -159,6 +163,7 @@ class Option(optparse.Option):
159163
TYPE_CHECKER["regexp_paths_csv"] = _regexp_paths_csv_validator
160164
TYPE_CHECKER["csv"] = _csv_validator
161165
TYPE_CHECKER["yn"] = _yn_validator
166+
TYPE_CHECKER["confidence"] = _multiple_choices_validating_option
162167
TYPE_CHECKER["multiple_choice"] = _multiple_choices_validating_option
163168
TYPE_CHECKER["non_empty_string"] = _non_empty_string_validator
164169
TYPE_CHECKER["py_version"] = _py_version_validator
@@ -169,7 +174,7 @@ def __init__(self, *opts, **attrs):
169174
self.help = optparse.SUPPRESS_HELP
170175

171176
def _check_choice(self):
172-
if self.type in {"choice", "multiple_choice"}:
177+
if self.type in {"choice", "multiple_choice", "confidence"}:
173178
if self.choices is None:
174179
raise optparse.OptionError(
175180
"must supply a list of choices for type 'choice'", self

pylint/interfaces.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"INFERENCE_FAILURE",
2525
"UNDEFINED",
2626
"CONFIDENCE_LEVELS",
27+
"CONFIDENCE_LEVEL_NAMES",
2728
)
2829

2930
Confidence = namedtuple("Confidence", ["name", "description"])
@@ -39,6 +40,7 @@
3940
UNDEFINED = Confidence("UNDEFINED", "Warning without any associated confidence level.")
4041

4142
CONFIDENCE_LEVELS = [HIGH, CONTROL_FLOW, INFERENCE, INFERENCE_FAILURE, UNDEFINED]
43+
CONFIDENCE_LEVEL_NAMES = [i.name for i in CONFIDENCE_LEVELS]
4244

4345

4446
class Interface:

pylint/lint/pylinter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,10 @@ def make_options() -> Tuple[Tuple[str, OptionDict], ...]:
355355
(
356356
"confidence",
357357
{
358-
"type": "multiple_choice",
358+
"type": "confidence",
359359
"metavar": "<levels>",
360-
"default": "",
361-
"choices": [c.name for c in interfaces.CONFIDENCE_LEVELS],
360+
"default": interfaces.CONFIDENCE_LEVEL_NAMES,
361+
"choices": interfaces.CONFIDENCE_LEVEL_NAMES,
362362
"group": "Messages control",
363363
"help": "Only show warnings with the listed confidence levels."
364364
f" Leave empty to show all. Valid levels: {', '.join(c.name for c in interfaces.CONFIDENCE_LEVELS)}.",

pylintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fail-on=
7474

7575
# Only show warnings with the listed confidence levels. Leave empty to show
7676
# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED
77-
confidence=
77+
# confidence=
7878

7979
# Enable the message, report, category or checker with the given id(s). You can
8080
# either give multiple identifier separated by comma (,) or put this option

0 commit comments

Comments
 (0)