contribute checking for "miscaptured" variables in loops
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by reading the linked check-miscaptures.py script and its example output to understand the warnings it produces for lambdas, named functions, loops, and comprehensions. Then inspect pyflakes to find the existing source-checking entry points; done means the same suspicious captures are reported by pyflakes, while the shown default-argument form is not.
Written by the indexing model from the issue text.
Description
Original report by daira (@daira?) on Launchpad:
Tahoe-LAFS, a large Python codebase implementing a distributed filesystem, uses a script called "check-miscaptures" ( https://tahoe-lafs.org/trac/tahoe-lafs/browser/trunk/misc/coding_tools/check-miscaptures.py) to catch a class of errors resulting in unexpected behaviour when capturing variables in loops.
An example of incorrect code that it would catch is:
a = [None]*10
for i in range(0, 10):
a[i] = lambda x: x+i
# The programmer expected a[x](y) to be equivalent to x+y
print a[2](2) # not 4, but 11
This happens because all of the a[i] capture the same mutable variable i, which is 9 after the loop when the lambda is called.
For this example, the output of check-miscaptures is:
Checking miscapture.py...
miscapture.py:3 captures 'i' assigned at line 2
1 suspiciously captured variables in 1 out of 1 file(s).
Changing the loop body to "a[i] = lambda x, i=i: x+i" fixes the problem, and check-miscaptures will not report a warning for the fixed code (it rarely gives false positives, and when it does they are easy to suppress).
The same problem can happen with named functions rather than lambdas, and with other kinds of loop or list comprehensions. More realistic examples are common in programs that define lots of callback functions, for example when using asynchronous libraries such as Twisted.
This ticket is to integrate the checking done by this script into pyflakes.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 207
- Avg merge
- 4m
- Merged PRs (30d)
- 1
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PyCQA/pyflakes
-
F542 is not a crimeOpen
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 52/100
-
Use of await in a function that is not async should be shown as a syntax error by flake8May be free again A pull request for this issue was closed without being merged. Open
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 85/100
NatLabRockies/openstudio-mcp#286 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
intersystems/ipm#1338 ·
Maintainers usually reply within 1 day
-
separate_packages splits imports from the same explicitly configured packagePossibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
The v0-through-v1 path derives ShutdownDuration its own way, silently overriding a user's definitionOpenv1
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day