Skip to content

Commit 7b28cf4

Browse files
Fixed conditional import x.y causing possibly-used-before-assignment (#10240)
1 parent abb8187 commit 7b28cf4

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-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
@@ -931,7 +931,9 @@ def _defines_name_raises_or_returns(name: str, node: nodes.NodeNG) -> bool:
931931
):
932932
return True
933933
if isinstance(node, (nodes.Import, nodes.ImportFrom)) and any(
934-
(node_name[1] and node_name[1] == name) or (node_name[0] == name)
934+
(node_name[1] and node_name[1] == name)
935+
or (node_name[0] == name)
936+
or (node_name[0].startswith(name + "."))
935937
for node_name in node.names
936938
):
937939
return True

tests/functional/u/used/used_before_assignment.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,11 @@ def inner_for():
289289
inner_try()
290290
inner_while()
291291
inner_for()
292+
293+
def conditional_import():
294+
if input():
295+
import os.path
296+
else:
297+
os = None
298+
if os:
299+
pass

0 commit comments

Comments
 (0)