Skip to content

Commit

Permalink
Fix bug with cell-var-from-loop and kw_defaults (#5045)
Browse files Browse the repository at this point in the history
* Fix bug with cell-var-from-loop and kw_defaults
* Use more efficient generator for defaults and add issue link
* Fix test output for cellvar_escaping_loop

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
david-yz-liu and Pierre-Sassoulas authored Sep 20, 2021
1 parent 24cbf8c commit fb750d3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ Release date: TBA

Closes #5029

* Fixed false positive for ``cell-var-from-loop`` when variable is used as the default
value for a keyword-only parameter.

Closes #5012


What's New in Pylint 2.11.1?
============================
Expand Down
13 changes: 9 additions & 4 deletions pylint/checkers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,15 @@ def is_default_argument(
if not scope:
scope = node.scope()
if isinstance(scope, (nodes.FunctionDef, nodes.Lambda)):
for default_node in scope.args.defaults:
for default_name_node in default_node.nodes_of_class(nodes.Name):
if default_name_node is node:
return True
all_defaults = itertools.chain(
scope.args.defaults, (d for d in scope.args.kw_defaults if d is not None)
)
return any(
default_name_node is node
for default_node in all_defaults
for default_name_node in default_node.nodes_of_class(nodes.Name)
)

return False


Expand Down
16 changes: 16 additions & 0 deletions tests/functional/c/cellvar_escaping_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,22 @@ def good_case_issue3107():
next(filter(lambda j, ix=i[0]: j == ix, [1, 3]))


def good_case_issue_5012():
"""Eager binding of cell variable when used as the default value of a keyword-only argument.
https://github.com/PyCQA/pylint/issues/5012
"""
funs = []
for i in range(5):
def func(*, _i=i):
print(_i)
funs.append(func)

def func2(_i=i):
print(_i)
funs.append(func2)

return funs

def bad_case():
"""Closing over a loop variable."""
lst = []
Expand Down
26 changes: 13 additions & 13 deletions tests/functional/c/cellvar_escaping_loop.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
cell-var-from-loop:101:27:bad_case.<lambda>:Cell variable i defined in loop:HIGH
cell-var-from-loop:106:20:bad_case2.<lambda>:Cell variable i defined in loop:HIGH
cell-var-from-loop:114:27:bad_case3.<lambda>:Cell variable j defined in loop:HIGH
cell-var-from-loop:124:19:bad_case4.nested:Cell variable i defined in loop:HIGH
cell-var-from-loop:145:20:bad_case5.<lambda>:Cell variable i defined in loop:HIGH
cell-var-from-loop:153:27:bad_case6.<lambda>:Cell variable i defined in loop:HIGH
cell-var-from-loop:161:12:bad_case7.<lambda>:Cell variable x defined in loop:HIGH
cell-var-from-loop:162:14:bad_case7.<lambda>:Cell variable y defined in loop:HIGH
cell-var-from-loop:171:27:bad_case8.<lambda>:Cell variable j defined in loop:HIGH
cell-var-from-loop:181:27:bad_case9.<lambda>:Cell variable i defined in loop:HIGH
cell-var-from-loop:190:26:bad_case10.func.func2:Cell variable i defined in loop:HIGH
cell-var-from-loop:202:17:bad_case_issue2846.<lambda>:Cell variable n defined in loop:HIGH
cell-var-from-loop:207:18:bad_case_issue2846.<lambda>:Cell variable n defined in loop:HIGH
cell-var-from-loop:117:27:bad_case.<lambda>:Cell variable i defined in loop:HIGH
cell-var-from-loop:122:20:bad_case2.<lambda>:Cell variable i defined in loop:HIGH
cell-var-from-loop:130:27:bad_case3.<lambda>:Cell variable j defined in loop:HIGH
cell-var-from-loop:140:19:bad_case4.nested:Cell variable i defined in loop:HIGH
cell-var-from-loop:161:20:bad_case5.<lambda>:Cell variable i defined in loop:HIGH
cell-var-from-loop:169:27:bad_case6.<lambda>:Cell variable i defined in loop:HIGH
cell-var-from-loop:177:12:bad_case7.<lambda>:Cell variable x defined in loop:HIGH
cell-var-from-loop:178:14:bad_case7.<lambda>:Cell variable y defined in loop:HIGH
cell-var-from-loop:187:27:bad_case8.<lambda>:Cell variable j defined in loop:HIGH
cell-var-from-loop:197:27:bad_case9.<lambda>:Cell variable i defined in loop:HIGH
cell-var-from-loop:206:26:bad_case10.func.func2:Cell variable i defined in loop:HIGH
cell-var-from-loop:218:17:bad_case_issue2846.<lambda>:Cell variable n defined in loop:HIGH
cell-var-from-loop:223:18:bad_case_issue2846.<lambda>:Cell variable n defined in loop:HIGH

0 comments on commit fb750d3

Please sign in to comment.