Skip to content

Prevent useless-suppression on disables for stdlib deprecation checker #5876

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

Merged
Merged
Show file tree
Hide file tree
Changes from 14 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
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ Release date: TBA

Closes #5862

* Disables for ``deprecated-module`` and similar warnings for stdlib features deprecated
in newer versions of Python no longer raise ``useless-suppression`` when linting with
older Python interpreters where those features are not yet deprecated.

* Importing the deprecated stdlib module ``distutils`` now emits ``deprecated_module`` on Python 3.10+.

* ``missing-raises-doc`` will now check the class hierarchy of the raised exceptions
Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,10 @@ Other Changes
* The ``testutils`` for unittests now accept ``end_lineno`` and ``end_column``. Tests
without these will trigger a ``DeprecationWarning``.

* Disables for ``deprecated-module`` and similar warnings for stdlib features deprecated
in newer versions of Python no longer raise ``useless-suppression`` when linting with
older Python interpreters where those features are not yet deprecated.

* Importing the deprecated stdlib module ``xml.etree.cElementTree`` now emits ``deprecated_module``.

Closes #5862
Expand Down
20 changes: 11 additions & 9 deletions pylint/checkers/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

import sys
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Set
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple

import astroid
from astroid import nodes
Expand Down Expand Up @@ -473,24 +473,26 @@ class StdlibChecker(DeprecatedMixin, BaseChecker):

def __init__(self, linter: Optional["PyLinter"] = None) -> None:
BaseChecker.__init__(self, linter)
self._deprecated_methods: Set[Any] = set()
self._deprecated_methods.update(DEPRECATED_METHODS[0])
self._deprecated_methods: Set[str] = set()
self._deprecated_arguments: Dict[
str, Tuple[Tuple[Optional[int], str], ...]
] = {}
self._deprecated_classes: Dict[str, Set[str]] = {}
self._deprecated_modules: Set[str] = set()
self._deprecated_decorators: Set[str] = set()

for since_vers, func_list in DEPRECATED_METHODS[sys.version_info[0]].items():
if since_vers <= sys.version_info:
self._deprecated_methods.update(func_list)
self._deprecated_attributes = {}
for since_vers, func_list in DEPRECATED_ARGUMENTS.items():
if since_vers <= sys.version_info:
self._deprecated_attributes.update(func_list)
self._deprecated_classes = {}
self._deprecated_arguments.update(func_list)
for since_vers, class_list in DEPRECATED_CLASSES.items():
if since_vers <= sys.version_info:
self._deprecated_classes.update(class_list)
self._deprecated_modules = set()
for since_vers, mod_list in DEPRECATED_MODULES.items():
if since_vers <= sys.version_info:
self._deprecated_modules.update(mod_list)
self._deprecated_decorators = set()
for since_vers, decorator_list in DEPRECATED_DECORATORS.items():
if since_vers <= sys.version_info:
self._deprecated_decorators.update(decorator_list)
Expand Down Expand Up @@ -788,7 +790,7 @@ def deprecated_methods(self):
return self._deprecated_methods

def deprecated_arguments(self, method: str):
return self._deprecated_attributes.get(method, ())
return self._deprecated_arguments.get(method, ())

def deprecated_classes(self, module: str):
return self._deprecated_classes.get(module, ())
Expand Down
10 changes: 10 additions & 0 deletions pylint/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,13 @@ class DeletedMessage(NamedTuple):
# https://github.com/PyCQA/pylint/pull/3571
DeletedMessage("C0330", "bad-continuation"),
]


INCOMPATIBLE_WITH_USELESS_SUPPRESSION = [
"R0401", # cyclic-import
"W0402", # deprecated-module
"W1505", # deprecated-method
"W1511", # deprecated-argument
"W1512", # deprecated-class
"W1513", # deprecated-decorator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering how hard it would be to still raise useless-suppression if the function/class/module/decorator... is not deprecated in any python version. I don't think this checker is very performance intensive, maybe relaunching with the highest python version (3.10 right now) is possible ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though about something similar, but in the context of a bare disable once the deprecation has been removed.

I don't think this checker is very performance intensive, maybe relaunching with the highest python version (3.10 right now) is possible ?

Not sure that is practical. It would almost certainly change major parts of the application flow.
Maybe it's simpler to add a new config option to show all useless-suppression messages? We should mention however, that it should only be used temporarily and not added to pylintrc.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cdce8p I like your last suggestion!

]
11 changes: 9 additions & 2 deletions pylint/utils/file_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

from astroid import nodes

from pylint.constants import MSG_STATE_SCOPE_MODULE, WarningScope
from pylint.constants import (
INCOMPATIBLE_WITH_USELESS_SUPPRESSION,
MSG_STATE_SCOPE_MODULE,
WarningScope,
)

if sys.version_info >= (3, 8):
from typing import Literal
Expand Down Expand Up @@ -162,7 +166,10 @@ def iter_spurious_suppression_messages(
if not enable and (warning, line) not in self._ignored_msgs:
# ignore cyclic-import check which can show false positives
# here due to incomplete context
if warning != "R0401":
# and deprecated-{module, argument, class, method, decorator}
# which can cause false positives for multi-interpreter projects
# when linting with an interpreter on a lower python version
if warning not in INCOMPATIBLE_WITH_USELESS_SUPPRESSION:
yield "useless-suppression", line, (
msgs_store.get_msg_display_string(warning),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Test that versions below Py3.10 will not emit useless-suppression for
disabling deprecated-method (on a method deprecated in Py3.10.

This test can be run on all Python versions, but it will lack value when
Pylint drops support for 3.9."""
# pylint: disable=import-error, unused-import

import threading.current_thread # pylint: disable=deprecated-method
4 changes: 4 additions & 0 deletions tests/functional/d/deprecated/deprecated_methods_py39.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Test deprecated methods from Python 3.9."""

import binascii
binascii.b2a_hqx() # [deprecated-method]
2 changes: 2 additions & 0 deletions tests/functional/d/deprecated/deprecated_methods_py39.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testoptions]
min_pyver=3.9
1 change: 1 addition & 0 deletions tests/functional/d/deprecated/deprecated_methods_py39.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deprecated-method:4:0:4:18::Using deprecated method b2a_hqx():UNDEFINED
2 changes: 1 addition & 1 deletion tests/functional/d/deprecated/deprecated_module_py3.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
deprecated-module:4:::Uses of a deprecated module 'optparse'
deprecated-module:4:0:4:15::Uses of a deprecated module 'optparse':UNDEFINED
4 changes: 4 additions & 0 deletions tests/functional/d/deprecated/deprecated_module_py39.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Test deprecated modules from Python 3.9."""
# pylint: disable=unused-import

import binhex # [deprecated-module]
5 changes: 5 additions & 0 deletions tests/functional/d/deprecated/deprecated_module_py39.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[testoptions]
min_pyver=3.9

[Messages Control]
enable=deprecated-module
1 change: 1 addition & 0 deletions tests/functional/d/deprecated/deprecated_module_py39.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deprecated-module:4:0:4:13::Uses of a deprecated module 'binhex':UNDEFINED
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Test deprecated modules from Python 3.9,
but use an earlier --py-version and ensure a warning is still emitted.
"""
# pylint: disable=unused-import

import binhex # [deprecated-module]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[master]
py-version=3.8

[testoptions]
min_pyver=3.9
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deprecated-module:6:0:6:13::Uses of a deprecated module 'binhex':UNDEFINED