Skip to content

Commit b9f515f

Browse files
Fixed conditional import x.y causing possibly-used-before-assignment (#10240) (#10241)
(cherry picked from commit 7b28cf4) Co-authored-by: Alex Prabhat Bara <alexpbara@gmail.com>
1 parent c131f0e commit b9f515f

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/whatsnew/fragments/10081.bugfix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed conditional import x.y causing false positive possibly-used-before-assignment.
2+
3+
Closes #10081

pylint/checkers/variables.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,9 @@ def _defines_name_raises_or_returns(name: str, node: nodes.NodeNG) -> bool:
967967
):
968968
return True
969969
if isinstance(node, (nodes.Import, nodes.ImportFrom)) and any(
970-
(node_name[1] and node_name[1] == name) or (node_name[0] == name)
970+
(node_name[1] and node_name[1] == name)
971+
or (node_name[0] == name)
972+
or (node_name[0].startswith(name + "."))
971973
for node_name in node.names
972974
):
973975
return True

tests/functional/u/used/used_before_assignment.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,12 @@ def print_platform_specific_command(self):
222222
self.skip("only runs on Linux/Windows")
223223

224224
print(cmd)
225+
226+
227+
def conditional_import():
228+
if input():
229+
import os.path
230+
else:
231+
os = None
232+
if os:
233+
pass

0 commit comments

Comments
 (0)