Skip to content

F821 false positive for __class__ in lambda expression in class definition #20562

@dscorbett

Description

@dscorbett

Summary

undefined-name (F821) has a false positive for __class__ in a lambda expression within a class definition. Example:

$ cat >f821.py <<'# EOF'
class C:
    f = lambda self: __class__
print(C().f().__name__)
# EOF

$ python f821.py
C

$ ruff --isolated check f821.py --select F821 --output-format concise -q
f821.py:2:22: F821 Undefined name `__class__`

Version

ruff 0.13.1 (706be0a 2025-09-18)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions