PyCQA / PyCQA/pyflakes

F841 reports misleading location in try/except block

Open
#378 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
190
Avg merge
8m
Merged PRs (30d)
13

Description

In this example pyflakes reports a misleading location for an unused variable:

$ cat example.py 
def foo():
    exc = None
    try:
        print('foo')
    except NameError as exc:
        print(exc)

$ pyflakes example.py 
example.py:5: local variable 'exc' is assigned to but never used

pyflakes reports exc on line 5 (the except line) as unused, which is misleading in my opinion since it's used within the exception handling. I guess that pyflakes recognizes that exc from line 2 (exc = None) as unused (which is unused actually), but determines line 5 as the last occuring declaration of exc.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the report with the example.py snippet and the pyflakes command. Trace how the unused assignment and the except binding are associated with source locations. Done means the genuinely unused assignment is reported at its own line without falsely pointing to the except line when the exception variable is used.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.