Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate BaseChecker.config #6278

Merged
merged 3 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ Release date: TBA

Closes #5224

* The ``config`` attribute of ``BaseChecker`` has been deprecated. You can use ``checker.linter.config``
to access the global configuration object instead of a checker-specific object.

Ref #5392

* The ``config`` attribute of ``PyLinter`` is now of the ``argparse.Namespace`` type instead of
``optparse.Values``.

Ref #5392

* ``OptionsManagerMixIn`` has been replaced with ``ArgumentsManager``. ``ArgumentsManager`` is considered
private API and most methods that were public on ``OptionsManagerMixIn`` have now been deprecated and will
be removed in a future release.
Expand Down
10 changes: 10 additions & 0 deletions doc/whatsnew/2.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ Other Changes
Closes #5283
Closes #1887

* The ``config`` attribute of ``BaseChecker`` has been deprecated. You can use ``checker.linter.config``
to access the global configuration object instead of a checker-specific object.

Ref #5392

* The ``config`` attribute of ``PyLinter`` is now of the ``argparse.Namespace`` type instead of
``optparse.Values``.

Ref #5392

* ``OptionsManagerMixIn`` has been replaced with ``ArgumentsManager``. ``ArgumentsManager`` is considered
private API and most methods that were public on ``OptionsManagerMixIn`` have now been deprecated and will
be removed in a future release.
Expand Down
4 changes: 4 additions & 0 deletions pylint/config/arguments_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def __init__(self, prog: str, usage: Optional[str] = None) -> None:
# verbosity
self._maxlevel: int = 0

@property
def config(self) -> argparse.Namespace:
return self.namespace

def _register_options_provider(self, provider: "_ArgumentsProvider") -> None:
"""Register an options provider and load its defaults."""
for opt, optdict in provider.options:
Expand Down
11 changes: 10 additions & 1 deletion pylint/config/arguments_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,21 @@ def __init__(self, arguments_manager: _ArgumentsManager) -> None:
# pylint: disable=fixme
# TODO: Optparse: Added to keep API parity with OptionsProvider
# They should be removed/deprecated when refactoring the copied methods
self.config = optparse.Values()
self._config = optparse.Values()
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=DeprecationWarning)
self.load_defaults()
self.level = 0

@property
def config(self) -> optparse.Values:
warnings.warn(
"The checker-specific config attribute has been deprecated. Please use "
"'linter.config' to access the global configuration object.",
DeprecationWarning,
)
return self._config

def load_defaults(self) -> None:
"""DEPRECATED: Initialize the provider using default values."""
warnings.warn(
Expand Down
2 changes: 1 addition & 1 deletion pylint/lint/pylinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def _load_reporter_by_class(reporter_class: str) -> type:


# pylint: disable=too-many-instance-attributes,too-many-public-methods
class PyLinter(
class PyLinter( # type: ignore[misc]
_ArgumentsManager,
reporters.ReportsHandlerMixIn,
checkers.BaseTokenChecker,
Expand Down
20 changes: 9 additions & 11 deletions tests/test_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,17 @@ def gen_tests(filter_rgx):
gen_tests(FILTER_RGX),
ids=[o[0] for o in gen_tests(FILTER_RGX)],
)
def test_functionality(module_file, messages_file, dependencies, recwarn):
def test_functionality(
module_file, messages_file, dependencies, recwarn: pytest.WarningsRecorder
) -> None:
__test_functionality(module_file, messages_file, dependencies)
warning = None
try:
# Catch <unknown>:x: DeprecationWarning: invalid escape sequence
# so it's not shown during tests
warning = recwarn.pop()
except AssertionError:
pass
if warning is not None:
if recwarn.list:
if module_file in TEST_WITH_EXPECTED_DEPRECATION and sys.version_info.minor > 5:
assert issubclass(warning.category, DeprecationWarning)
assert "invalid escape sequence" in str(warning.message)
assert any(
"invalid escape sequence" in str(i.message)
for i in recwarn.list
if issubclass(i.category, DeprecationWarning)
)


def __test_functionality(
Expand Down
16 changes: 6 additions & 10 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,16 @@ def test_functional(
lint_test = testutils.LintModuleTest(test_file, pytestconfig)
lint_test.setUp()
lint_test.runTest()
warning = None
try:
# Catch <unknown>:x: DeprecationWarning: invalid escape sequence,
# so, it's not shown during tests
warning = recwarn.pop()
except AssertionError:
pass
if warning is not None:
if recwarn.list:
if (
test_file.base in TEST_WITH_EXPECTED_DEPRECATION
and sys.version_info.minor > 5
):
assert issubclass(warning.category, DeprecationWarning)
assert "invalid escape sequence" in str(warning.message)
assert any(
"invalid escape sequence" in str(i.message)
for i in recwarn.list
if issubclass(i.category, DeprecationWarning)
)


if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions tests/test_regr.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ def test_pylint_config_attr() -> None:
assert [c.name for c in pylinter.ancestors()] == expect
assert list(astroid.Instance(pylinter).getattr("config"))
inferred = list(astroid.Instance(pylinter).igetattr("config"))
assert len(inferred) == 1
assert inferred[0].root().name == "optparse"
assert inferred[0].name == "Values"
assert len(inferred) >= 1
assert inferred[0].root().name == "argparse"
assert inferred[0].name == "Namespace"


@pytest.mark.timeout(30)
Expand Down