Skip to content

F821 false positive: from .foo import Bar doesn't mark foo as defined. #23233

@alona-em

Description

@alona-em

Summary

spam/__init__.py

from .foo import Bar

print(foo.Bar)

spam/foo.py

class Bar:
    pass

ruff things this is an error.

% uvx ruff check ./spam/__init__.py --select=F821
F821 Undefined name `foo`
 --> spam/__init__.py:3:7
  |
1 | from .foo import Bar
2 |
3 | print(foo.Bar)
  |       ^^^
  |

Found 1 error.

But it works fine:

% python3 -c 'import spam'
<class 'spam.foo.Bar'>

This works due to weird python behavior (https://docs.python.org/3/reference/import.html#submodules).

(See also microsoft/pyright#782, where pyright used to have this same bug).

Version

ruff 0.15.0 (ce5f7b6 2026-02-03)

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