Skip to content

Commit cd83ef4

Browse files
Deprecate BaseChecker.config (#6278)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 5c112dd commit cd83ef4

File tree

8 files changed

+53
-26
lines changed

8 files changed

+53
-26
lines changed

ChangeLog

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ Release date: TBA
2323

2424
Closes #5224
2525

26+
* The ``config`` attribute of ``BaseChecker`` has been deprecated. You can use ``checker.linter.config``
27+
to access the global configuration object instead of a checker-specific object.
28+
29+
Ref #5392
30+
31+
* The ``config`` attribute of ``PyLinter`` is now of the ``argparse.Namespace`` type instead of
32+
``optparse.Values``.
33+
34+
Ref #5392
35+
2636
* ``OptionsManagerMixIn`` has been replaced with ``ArgumentsManager``. ``ArgumentsManager`` is considered
2737
private API and most methods that were public on ``OptionsManagerMixIn`` have now been deprecated and will
2838
be removed in a future release.

doc/whatsnew/2.14.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ Other Changes
7373
Closes #5283
7474
Closes #1887
7575

76+
* The ``config`` attribute of ``BaseChecker`` has been deprecated. You can use ``checker.linter.config``
77+
to access the global configuration object instead of a checker-specific object.
78+
79+
Ref #5392
80+
81+
* The ``config`` attribute of ``PyLinter`` is now of the ``argparse.Namespace`` type instead of
82+
``optparse.Values``.
83+
84+
Ref #5392
85+
7686
* ``OptionsManagerMixIn`` has been replaced with ``ArgumentsManager``. ``ArgumentsManager`` is considered
7787
private API and most methods that were public on ``OptionsManagerMixIn`` have now been deprecated and will
7888
be removed in a future release.

pylint/config/arguments_manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ def __init__(self, prog: str, usage: Optional[str] = None) -> None:
8686
# verbosity
8787
self._maxlevel: int = 0
8888

89+
@property
90+
def config(self) -> argparse.Namespace:
91+
return self.namespace
92+
8993
def _register_options_provider(self, provider: "_ArgumentsProvider") -> None:
9094
"""Register an options provider and load its defaults."""
9195
for opt, optdict in provider.options:

pylint/config/arguments_provider.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,21 @@ def __init__(self, arguments_manager: _ArgumentsManager) -> None:
4040
# pylint: disable=fixme
4141
# TODO: Optparse: Added to keep API parity with OptionsProvider
4242
# They should be removed/deprecated when refactoring the copied methods
43-
self.config = optparse.Values()
43+
self._config = optparse.Values()
4444
with warnings.catch_warnings():
4545
warnings.filterwarnings("ignore", category=DeprecationWarning)
4646
self.load_defaults()
4747
self.level = 0
4848

49+
@property
50+
def config(self) -> optparse.Values:
51+
warnings.warn(
52+
"The checker-specific config attribute has been deprecated. Please use "
53+
"'linter.config' to access the global configuration object.",
54+
DeprecationWarning,
55+
)
56+
return self._config
57+
4958
def load_defaults(self) -> None:
5059
"""DEPRECATED: Initialize the provider using default values."""
5160
warnings.warn(

pylint/lint/pylinter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def _load_reporter_by_class(reporter_class: str) -> type:
195195

196196

197197
# pylint: disable=too-many-instance-attributes,too-many-public-methods
198-
class PyLinter(
198+
class PyLinter( # type: ignore[misc]
199199
_ArgumentsManager,
200200
reporters.ReportsHandlerMixIn,
201201
checkers.BaseTokenChecker,

tests/test_func.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,17 @@ def gen_tests(filter_rgx):
123123
gen_tests(FILTER_RGX),
124124
ids=[o[0] for o in gen_tests(FILTER_RGX)],
125125
)
126-
def test_functionality(module_file, messages_file, dependencies, recwarn):
126+
def test_functionality(
127+
module_file, messages_file, dependencies, recwarn: pytest.WarningsRecorder
128+
) -> None:
127129
__test_functionality(module_file, messages_file, dependencies)
128-
warning = None
129-
try:
130-
# Catch <unknown>:x: DeprecationWarning: invalid escape sequence
131-
# so it's not shown during tests
132-
warning = recwarn.pop()
133-
except AssertionError:
134-
pass
135-
if warning is not None:
130+
if recwarn.list:
136131
if module_file in TEST_WITH_EXPECTED_DEPRECATION and sys.version_info.minor > 5:
137-
assert issubclass(warning.category, DeprecationWarning)
138-
assert "invalid escape sequence" in str(warning.message)
132+
assert any(
133+
"invalid escape sequence" in str(i.message)
134+
for i in recwarn.list
135+
if issubclass(i.category, DeprecationWarning)
136+
)
139137

140138

141139
def __test_functionality(

tests/test_functional.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,16 @@ def test_functional(
5353
lint_test = testutils.LintModuleTest(test_file, pytestconfig)
5454
lint_test.setUp()
5555
lint_test.runTest()
56-
warning = None
57-
try:
58-
# Catch <unknown>:x: DeprecationWarning: invalid escape sequence,
59-
# so, it's not shown during tests
60-
warning = recwarn.pop()
61-
except AssertionError:
62-
pass
63-
if warning is not None:
56+
if recwarn.list:
6457
if (
6558
test_file.base in TEST_WITH_EXPECTED_DEPRECATION
6659
and sys.version_info.minor > 5
6760
):
68-
assert issubclass(warning.category, DeprecationWarning)
69-
assert "invalid escape sequence" in str(warning.message)
61+
assert any(
62+
"invalid escape sequence" in str(i.message)
63+
for i in recwarn.list
64+
if issubclass(i.category, DeprecationWarning)
65+
)
7066

7167

7268
if __name__ == "__main__":

tests/test_regr.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ def test_pylint_config_attr() -> None:
132132
assert [c.name for c in pylinter.ancestors()] == expect
133133
assert list(astroid.Instance(pylinter).getattr("config"))
134134
inferred = list(astroid.Instance(pylinter).igetattr("config"))
135-
assert len(inferred) == 1
136-
assert inferred[0].root().name == "optparse"
137-
assert inferred[0].name == "Values"
135+
assert len(inferred) >= 1
136+
assert inferred[0].root().name == "argparse"
137+
assert inferred[0].name == "Namespace"
138138

139139

140140
@pytest.mark.timeout(30)

0 commit comments

Comments
 (0)