Skip to content

Commit af974aa

Browse files
Fix #5586: False positive for used-before-assignment with homonyms in filtered comprehensions and except blocks (#5666)
Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent ca44a24 commit af974aa

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

ChangeLog

+6
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ Release date: TBA
9696

9797
Closes #5360, #3877
9898

99+
* Fixed a false positive (affecting unreleased development) for
100+
``used-before-assignment`` involving homonyms between filtered comprehensions
101+
and assignments in except blocks.
102+
103+
Closes #5586
104+
99105
* Fixed crash with slots assignments and annotated assignments.
100106

101107
Closes #5479

doc/whatsnew/2.13.rst

+6
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ Other Changes
139139

140140
Closes #5360, #3877
141141

142+
* Fixed a false positive (affecting unreleased development) for
143+
``used-before-assignment`` involving homonyms between filtered comprehensions
144+
and assignments in except blocks.
145+
146+
Closes #5586
147+
142148
* Fixed crash on list comprehensions that used ``type`` as inner variable name.
143149

144150
Closes #5461

pylint/checkers/variables.py

+7
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,13 @@ def _check_consumer(
13161316
if utils.is_func_decorator(current_consumer.node) or not (
13171317
current_consumer.scope_type == "comprehension"
13181318
and self._has_homonym_in_upper_function_scope(node, consumer_level)
1319+
# But don't catch homonyms against the filter of a comprehension,
1320+
# (like "if x" in "[x for x in expr() if x]")
1321+
# https://github.com/PyCQA/pylint/issues/5586
1322+
and not (
1323+
isinstance(node.parent.parent, nodes.Comprehension)
1324+
and node.parent in node.parent.parent.ifs
1325+
)
13191326
):
13201327
self._check_late_binding_closure(node)
13211328
self._loopvar_name(node)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Homonym between filtered comprehension and assignment in except block."""
2+
3+
def func():
4+
"""https://github.com/PyCQA/pylint/issues/5586"""
5+
try:
6+
print(value for value in range(1 / 0) if isinstance(value, int))
7+
except ZeroDivisionError:
8+
value = 1
9+
print(value)

0 commit comments

Comments
 (0)