Skip to content

Commit

Permalink
Fix #5586: False positive for used-before-assignment with homonyms …
Browse files Browse the repository at this point in the history
…in filtered comprehensions and except blocks (#5666)

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
jacobtylerwalls and Pierre-Sassoulas authored Jan 12, 2022
1 parent ca44a24 commit af974aa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ Release date: TBA

Closes #5360, #3877

* Fixed a false positive (affecting unreleased development) for
``used-before-assignment`` involving homonyms between filtered comprehensions
and assignments in except blocks.

Closes #5586

* Fixed crash with slots assignments and annotated assignments.

Closes #5479
Expand Down
6 changes: 6 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ Other Changes

Closes #5360, #3877

* Fixed a false positive (affecting unreleased development) for
``used-before-assignment`` involving homonyms between filtered comprehensions
and assignments in except blocks.

Closes #5586

* Fixed crash on list comprehensions that used ``type`` as inner variable name.

Closes #5461
Expand Down
7 changes: 7 additions & 0 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,13 @@ def _check_consumer(
if utils.is_func_decorator(current_consumer.node) or not (
current_consumer.scope_type == "comprehension"
and self._has_homonym_in_upper_function_scope(node, consumer_level)
# But don't catch homonyms against the filter of a comprehension,
# (like "if x" in "[x for x in expr() if x]")
# https://github.com/PyCQA/pylint/issues/5586
and not (
isinstance(node.parent.parent, nodes.Comprehension)
and node.parent in node.parent.parent.ifs
)
):
self._check_late_binding_closure(node)
self._loopvar_name(node)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Homonym between filtered comprehension and assignment in except block."""

def func():
"""https://github.com/PyCQA/pylint/issues/5586"""
try:
print(value for value in range(1 / 0) if isinstance(value, int))
except ZeroDivisionError:
value = 1
print(value)

0 comments on commit af974aa

Please sign in to comment.