Skip to content

Shadowing and unshadowing a builtin makes pyflakes emit a F821 #548

Open

Description

@mgedmin

Consider this silly code:

int = 42
del int

x = int('42')
print(x)

This is perfectly valid Python: if you run it, it'll print 42.

flake8 emits a F821: undefined name 'int' on the penultimate line. I think it's because of the del int.

For a real-world example of this situation, consider a project that uses gettext and installs _ in builtins, but doesn't do that early enough to be available at import time of every module. Here's how linkchecker copes with that situation:

_ = lambda x: x
Fields = dict(
    realurl=_("Real URL"),
    cachekey=_("Cache key"),
...
    level=_("Level"),
    modified=_("Modified"),
)
del _

and this then causes flake8 to complain about _() being used elsewhere in the file:

./linkcheck/logger/__init__.py:278:19: F821 undefined name '_'
./linkcheck/logger/__init__.py:340:16: F821 undefined name '_'
./linkcheck/logger/__init__.py:377:22: F821 undefined name '_'
./linkcheck/logger/__init__.py:380:22: F821 undefined name '_'
./linkcheck/logger/__init__.py:382:22: F821 undefined name '_'
./linkcheck/logger/__init__.py:390:22: F821 undefined name '_'

flake8 --version reports

3.8.2 (flake8_2020: 1.6.0, mccabe: 0.6.1, pycodestyle: 2.6.0, pyflakes: 2.2.0) CPython 3.8.2 on Linux

Activity

  1. added a commit that references this issue on Oct 22, 2026
    9471c71
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions