Skip to content

Commit 8d4c687

Browse files
authored
Fix false negative for deprecated-module when a __import__ method is used instead of import sentence. (#10454)
Using the following python code style ```python __import__("deprecated_module") ``` The deprecated-module is ignored since that it expects ```python import deprecated_module ``` This change adds the posibility to process these kind of style too This style is used from popular auto-code snippets: - https://github.com/honza/vim-snippets/blob/f0a3184/snippets/python.snippets#L209 Fix #10453
1 parent e4f49e2 commit 8d4c687

File tree

7 files changed

+32
-0
lines changed

7 files changed

+32
-0
lines changed

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ contributors:
9191
* Fix consider-using-ternary for 'True and True and True or True' case
9292
* Add bad-docstring-quotes and docstring-first-line-empty
9393
* Add missing-timeout
94+
* Fix false negative for `deprecated-module` when a `__import__` method is used instead of `import` sentence
9495
- Frank Harrison <[email protected]> (doublethefish)
9596
- Zen Lee <[email protected]>
9697
- Pierre-Yves David <[email protected]>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix false negative for `deprecated-module` when a `__import__` method is used instead of `import` sentence.
2+
3+
Refs #10453

pylint/checkers/deprecated.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ def visit_attribute(self, node: astroid.Attribute) -> None:
9797
"deprecated-method",
9898
"deprecated-argument",
9999
"deprecated-class",
100+
"deprecated-module",
100101
)
101102
def visit_call(self, node: nodes.Call) -> None:
102103
"""Called when a :class:`nodes.Call` node is visited."""
@@ -105,6 +106,15 @@ def visit_call(self, node: nodes.Call) -> None:
105106
# Calling entry point for deprecation check logic.
106107
self.check_deprecated_method(node, inferred)
107108

109+
if (
110+
isinstance(inferred, nodes.FunctionDef)
111+
and inferred.qname() == "builtins.__import__"
112+
and len(node.args) == 1
113+
and (mod_path_node := utils.safe_infer(node.args[0]))
114+
and isinstance(mod_path_node, astroid.Const)
115+
):
116+
self.check_deprecated_module(node, mod_path_node.value)
117+
108118
@utils.only_required_for_messages(
109119
"deprecated-module",
110120
"deprecated-class",

script/.contributors_aliases.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@
109109
"mails": ["[email protected]"],
110110
"name": "Hayden Richards"
111111
},
112+
113+
114+
"name": "Moisés Lopez"
115+
},
112116
113117
"comment": ": Fix for invalid toml config",
114118
"mails": ["[email protected]"],
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Test deprecated modules using '__import__' builtin method."""
2+
# pylint: disable=invalid-name
3+
4+
imp = __import__("mypackage") # [deprecated-module]
5+
6+
imp_meth = __import__("mypackage").meth() # [deprecated-module]
7+
8+
lib = "mypackage"
9+
infer_imp = __import__(lib) # [deprecated-module]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[Imports]
2+
deprecated-modules=mypackage
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
deprecated-module:4:6:4:29::Deprecated module 'mypackage':UNDEFINED
2+
deprecated-module:6:11:6:34::Deprecated module 'mypackage':UNDEFINED
3+
deprecated-module:9:12:9:27::Deprecated module 'mypackage':UNDEFINED

0 commit comments

Comments
 (0)