Skip to content

F821 false-positive for SQLAlchemy mappings with not yet declared classes #10451

@aberres

Description

@aberres

The described behavior happens since 0.3.3. It might be related to #10340

It is triggered throughout my SQLAlchemy models in cases where a model is not defined yet while used in a type annotation

Minimal repro:

from __future__ import annotations

from sqlalchemy.orm import DeclarativeBase, Mapped


class Base(DeclarativeBase):
    some_mapping: Mapped[list[Bar]] | None = None # F821 Undefined name `Bar`
    simplified: list[Bar] | None = None # F821 Undefined name `Bar`


class Bar:
    pass

I can only trigger the error when inheriting from DeclarativeBase. Without this inheritance or when inheriting from another class, things are fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglinterRelated to the linterruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions