From 0d48602a219c47b916eae13d02fd4b7c3de75eb9 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Tue, 11 Jan 2022 18:22:59 -0500 Subject: [PATCH 1/4] Fix #5586: False positive for `used-before-assignment` with homonyms in filtered comprehensions and except blocks --- ChangeLog | 6 ++++++ doc/whatsnew/2.13.rst | 6 ++++++ pylint/checkers/variables.py | 4 ++++ .../use/used_before_assignment_filtered_comprehension.py | 9 +++++++++ 4 files changed, 25 insertions(+) create mode 100644 tests/functional/u/use/used_before_assignment_filtered_comprehension.py diff --git a/ChangeLog b/ChangeLog index 93292a4112..aaea35f784 100644 --- a/ChangeLog +++ b/ChangeLog @@ -237,6 +237,12 @@ Release date: 2021-11-25 Closes #5342 +* Fixed a false positive (affecting unreleased development) for + ``used-before-assignment`` involving homonyms between filtered comprehensions + and assignments in except blocks. + + Closes #5586 + * Specified that the ``ignore-paths`` option considers "\" to represent a windows directory delimiter instead of a regular expression escape character. diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst index 10e8c5b689..74499108d3 100644 --- a/doc/whatsnew/2.13.rst +++ b/doc/whatsnew/2.13.rst @@ -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 diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 66af3ec940..94fae2155c 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1316,6 +1316,10 @@ 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) + 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) diff --git a/tests/functional/u/use/used_before_assignment_filtered_comprehension.py b/tests/functional/u/use/used_before_assignment_filtered_comprehension.py new file mode 100644 index 0000000000..f638b0be9b --- /dev/null +++ b/tests/functional/u/use/used_before_assignment_filtered_comprehension.py @@ -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) From f02dbd2394c713aaf238d84c01a018e3b124ce31 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Tue, 11 Jan 2022 18:31:45 -0500 Subject: [PATCH 2/4] add comment --- pylint/checkers/variables.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 94fae2155c..7e3a31ee12 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1316,6 +1316,9 @@ 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, + # "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 From 84eead09fa905c19ca713ab0e42c4621b6dedfbf Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Tue, 11 Jan 2022 18:36:03 -0500 Subject: [PATCH 3/4] fix changelog location --- ChangeLog | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index aaea35f784..6d4239fa12 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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 @@ -237,12 +243,6 @@ Release date: 2021-11-25 Closes #5342 -* Fixed a false positive (affecting unreleased development) for - ``used-before-assignment`` involving homonyms between filtered comprehensions - and assignments in except blocks. - - Closes #5586 - * Specified that the ``ignore-paths`` option considers "\" to represent a windows directory delimiter instead of a regular expression escape character. From 4f385643bc24483dee9aef8f124498dd676fab85 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Wed, 12 Jan 2022 09:37:10 -0500 Subject: [PATCH 4/4] Improve comment Co-authored-by: Pierre Sassoulas --- pylint/checkers/variables.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 7e3a31ee12..bc291c1b9e 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -1317,7 +1317,7 @@ def _check_consumer( 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, - # "if x" in [x for x in expr() if x] + # (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)