Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing: of_type method not properly generic. #11371

Open
2 tasks done
WAKayser opened this issue May 8, 2024 · 1 comment · May be fixed by #11416
Open
2 tasks done

Typing: of_type method not properly generic. #11371

WAKayser opened this issue May 8, 2024 · 1 comment · May be fixed by #11416
Labels
PRs (with tests!) welcome a fix or feature which is appropriate to be implemented by volunteers typing pep -484 typing issues. independent of "mypy"

Comments

@WAKayser
Copy link

WAKayser commented May 8, 2024

Ensure stubs packages are not installed

  • No sqlalchemy stub packages is installed (both sqlalchemy-stubs and sqlalchemy2-stubs are not compatible with v2)

Verify if the api is typed

  • The api is not in a module listed in #6810 so it should pass type checking

Describe the typing issue

of_type() method for QueryableAttribute is not properly made generic. This causes the type to not be set correctly and the result is not completely determined. This is only a minor nuisance as the correct type is not needed for any other checking.

To Reproduce

# pyright: strict
from typing import reveal_type

from sqlalchemy import create_engine, ForeignKey
from sqlalchemy.orm import (
    scoped_session,
    sessionmaker,
    DeclarativeBase,
    MappedAsDataclass,
    mapped_column,
    Mapped,
    relationship,
)

engine = create_engine("sqlite://")
db_session = scoped_session(sessionmaker(engine))


class Base(MappedAsDataclass, DeclarativeBase):
    pass


class Employee(Base):
    __tablename__ = "employee"
    id: Mapped[int] = mapped_column(primary_key=True)
    team_id: Mapped[int] = mapped_column(ForeignKey("team.id"))
    team: Mapped["Team"] = relationship(
        lazy="noload", init=False, back_populates="employees"
    )

    __mapper_args__ = {
        "polymorphic_on": "type",
        "polymorphic_identity": "employee",
    }


class Team(Base):
    __tablename__ = "team"
    id: Mapped[int] = mapped_column(primary_key=True)
    team_name: Mapped[str]
    employees: Mapped[list[Employee]] = relationship(
        "Employee", lazy="noload", init=False
    )


class Engineer(Employee):
    engineer_info: Mapped[str | None] = mapped_column(nullable=True, default=None)

    __mapper_args__ = {
        "polymorphic_identity": "engineer",
        "polymorphic_load": "inline",
    }


engineer_load = Team.employees.of_type(Engineer)
reveal_type(Team.employees.of_type(Engineer))

Error

MYPY

of_type_test.py:55: error: Need type annotation for "engineer_load" [var-annotated]
of_type_test.py:56: note: Revealed type is "sqlalchemy.orm.attributes.QueryableAttribute[]"

Pyright

.\of_type_test.py:55:1 - error: Type of "engineer_load" is partially unknown
  Type of "engineer_load" is "QueryableAttribute[Unknown]" (reportUnknownVariableType)
.\of_type_test.py:56:13 - information: Type of "Team.employees.of_type(Engineer)" is "QueryableAttribute[Unknown]"

Versions

  • OS: Windows 11
  • Python: 3.11 and 3.12
  • SQLAlchemy: 2.0.30
  • Type checker: mypy 1.10.0 and pyright 1.362

Additional context

Changing signature of the of_type method seems to fix it.

from:
def of_type(self, entity: _EntityType[Any]) -> QueryableAttribute[_T]:
to
def of_type(self, entity: _EntityType[_T]) -> QueryableAttribute[_T]:

Mypy:
of_type_test.py:56: note: Revealed type is "sqlalchemy.orm.attributes.QueryableAttribute[of_type_test.Engineer]"
Pyright:
\of_type_test.py:56:13 - information: Type of "Team.employees.of_type(Engineer)" is "QueryableAttribute[Engineer]"

@WAKayser WAKayser added requires triage New issue that requires categorization typing pep -484 typing issues. independent of "mypy" labels May 8, 2024
@CaselIT CaselIT added PRs (with tests!) welcome a fix or feature which is appropriate to be implemented by volunteers and removed requires triage New issue that requires categorization labels May 8, 2024
@CaselIT
Copy link
Member

CaselIT commented May 8, 2024

Hi,

Thanks for reporting. Could you create a PR?
The tests could go in test/typing/orm/relationship.

@WAKayser WAKayser linked a pull request May 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PRs (with tests!) welcome a fix or feature which is appropriate to be implemented by volunteers typing pep -484 typing issues. independent of "mypy"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants