Skip to content

F821: false positive when using del in .pyi files #23539

@loic-simon

Description

@loic-simon

Summary

Consider the following

# mre.pyi
class MyClass: ...
def f(cls: MyClass) -> None: ...
del MyClass
$ ruff check --isolated mre.pyi
F821 Undefined name `MyClass`
 --> mre.pyi:2:12
  |
1 | class MyClass: ...
2 | def f(cls: MyClass) -> None: ...
  |            ^^^^^^^
3 | del MyClass
  |

Found 1 error.

If I rename the file mre.py instead of .pyi, the check pass (playground).

AFAIK, del statements are allowed in .pyi files (and are useful to avoid exposing symbols needed for typing but not exposed at runtime, but maybe there is a better way to do that?)

Possibly related to

Version

ruff 0.15.2 (9d18ee9 2026-02-19)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions