Skip to content

query_expression and column_property both included in pep-681 at type checking and runtime #9628

@zzzeek

Description

@zzzeek

Discussed in #9624

this mapping is impossible:

from __future__ import annotations

from sqlalchemy.orm import column_property
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import MappedAsDataclass
from sqlalchemy.orm import query_expression

class Base(MappedAsDataclass, DeclarativeBase):
    pass


class A(Base):
    __tablename__ = "a"

    id: Mapped[int] = mapped_column(primary_key=True, init=False)
    data: Mapped[str] = mapped_column()

    foo: Mapped[str] = query_expression()
    bar: Mapped[str] = column_property(data + "fasdf")


a1 = A(data='mydata')

query_expression and column_property at runtime are pulled in, with no way to pass init=False, so that's a runtime change. then also these two constructs are erroneously in the dataclass_transform decorator so they don't type-check correctly either. major issue has to be fixed ASAP

the workaround is as follows

class A(Base):
    __tablename__ = "a"

    id: Mapped[int] = mapped_column(primary_key=True, init=False)
    data: Mapped[str] = mapped_column()

    if TYPE_CHECKING:
        foo: Mapped[str] = mapped_column(init=False)
        bar: Mapped[str] = mapped_column(init=False)
    else:
        foo = query_expression()
        bar = column_property(data + "fasdf")

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdataclassesnear-term releaseaddition to the milestone which indicates this should be in a near-term releaseorm - annotated declarativeissues with the new annotations-based declarative ORM approach

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions