PyCQA / PyCQA/pyflakes

"Local variable (defined in enclosing scope) referenced before assignment" wording is misleading

Open
#390 2 comments 1 reaction 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

def f():
    x = None
    def g():
        # Programmer forgot to add `nonlocal` here.
        if x is None:
            x = "hello"

foo.py:5:12: F823 local variable 'x' (defined in enclosing scope on line 2) referenced before assignment

But it's not the variable defined on line 2 which is being referenced before assignment, it's the one defined on line 6. The programmer thinks they're using the one on line 2, and the message only reinforces their mistake.

Suggest rephrase to something like (shadowing name from line 2 in enclosing scope).

(There's a similar example in #315 where the enclosing scope was global.)

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 locating the code that formats the F823 diagnostic, then compare its wording with the example in this issue and the related example in #315. Done means the message clearly describes the inner name as shadowing the enclosing-scope name rather than saying the enclosing variable was referenced before assignment.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.