Skip to content

Commit 0d48602

Browse files
Fix pylint-dev#5586: False positive for used-before-assignment with homonyms in filtered comprehensions and except blocks
1 parent ca44a24 commit 0d48602

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

ChangeLog

+6
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ Release date: 2021-11-25
237237

238238
Closes #5342
239239

240+
* Fixed a false positive (affecting unreleased development) for
241+
``used-before-assignment`` involving homonyms between filtered comprehensions
242+
and assignments in except blocks.
243+
244+
Closes #5586
245+
240246
* Specified that the ``ignore-paths`` option considers "\" to represent a
241247
windows directory delimiter instead of a regular expression escape
242248
character.

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

+4
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,10 @@ 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+
and not (
1320+
isinstance(node.parent.parent, nodes.Comprehension)
1321+
and node.parent in node.parent.parent.ifs
1322+
)
13191323
):
13201324
self._check_late_binding_closure(node)
13211325
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)