Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#3791] propose a fix #4737

Merged
merged 4 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,5 @@ contributors:
- Documented Jupyter integration

* Yilei Yang: contributor

* Marcin Kurczewski (rr-): contributor
10 changes: 7 additions & 3 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
# Copyright (c) 2021 Lorena B <46202743+lorena-b@users.noreply.github.com>
# Copyright (c) 2021 haasea <44787650+haasea@users.noreply.github.com>
# Copyright (c) 2021 Alexander Kapshuna <kapsh@kap.sh>
# Copyright (c) 2021 Marcin Kurczewski <rr-@sakuya.pl>

# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
Expand Down Expand Up @@ -1006,9 +1007,12 @@ def visit_name(self, node):
# variable used outside the loop
# avoid the case where there are homonyms inside function scope and
# comprehension current scope (avoid bug #1731)
if name in current_consumer.consumed and not (
current_consumer.scope_type == "comprehension"
and self._has_homonym_in_upper_function_scope(node, i)
if name in current_consumer.consumed and (
utils.is_func_decorator(current_consumer.node)
or not (
current_consumer.scope_type == "comprehension"
and self._has_homonym_in_upper_function_scope(node, i)
)
):
defnode = utils.assign_parent(current_consumer.consumed[name][0])
self._check_late_binding_closure(node, defnode)
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/u/undefined/undefined_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,22 @@ def undefined_annotation(a:x): # [undefined-variable]
]
for item in lst
]


# 3791
@decorator(x for x in range(3))
def decorated1(x):
print(x)

@decorator(x * x for x in range(3))
def decorated2(x):
print(x)

@decorator(x) # [undefined-variable]
@decorator(x * x for x in range(3))
def decorated3(x):
print(x)

@decorator(x * x * y for x in range(3)) # [undefined-variable]
def decorated4(x):
print(x)
2 changes: 2 additions & 0 deletions tests/functional/u/undefined/undefined_variable.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ used-before-assignment:254:26:func_should_fail:Using variable 'datetime' before
undefined-variable:281:18:not_using_loop_variable_accordingly:Undefined variable 'iteree'
undefined-variable:292:27:undefined_annotation:Undefined variable 'x'
used-before-assignment:293:7:undefined_annotation:Using variable 'x' before assignment
undefined-variable:323:11:decorated3:Undefined variable 'x'
undefined-variable:328:19:decorated4:Undefined variable 'y'