Open Bryley opened 2 years ago
It's not a perfect solution, but you can use declared_attr
and column_property
:
from typing import cast
from sqlalchemy.orm import column_property, declared_attr
...
class ScoreTableBase(SQLModel):
__tablename__ = "scores" # type: ignore
id: Optional[int] = Field(default=None, primary_key=True)
name: str
score: int = Field(ge=0, le=100)
class Score(ScoreTableBase, table=True):
pass
class ScoreAggregate(ScoreTableBase, table=True):
__table_args__ = {"extend_existing": True}
@declared_attr
def max(cls) -> int:
return cast(int, column_property(func.max(cls.score)))
@declared_attr
def min(cls) -> int:
return cast(int, column_property(func.min(cls.score)))
...
with Session(engine) as session:
res = session.exec(
select(
ScoreAggregate
)
).one()
print(f"Minimum score: {res.min}")
print(f"Maximum score: {res.max}")
use
Session.exec
-> Session.execute
sqlmodel.select
-> sqlalchemy.select
ex:
from typing import ClassVar, Optional
from sqlalchemy import select
from sqlmodel import Field, Session, SQLModel, create_engine, func
class Score(SQLModel, table=True):
__tablename__: ClassVar[str] = "scores"
id: Optional[int] = Field(default=None, primary_key=True)
name: str
score: int = Field(ge=0, le=100)
engine = create_engine("sqlite:///")
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
session.add(Score(name="Bob", score=51))
session.add(Score(name="Alice", score=88))
session.add(Score(name="Greg", score=12))
session.add(Score(name="Patrick", score=100))
session.add(Score(name="Susie", score=45))
session.commit()
with Session(engine) as session:
res = session.execute(select(func.min(Score.score), func.max(Score.score))).one()
print(f"Minimum score: {res['min']}")
print(f"Maximum score: {res['max']}")
First Check
Commit to Help
Example Code
Description
The above code works as expected and outputs:
However the issue I'm having is the pyright static type checking around:
Pyright gives me the following error and the
res
variable has anUnkown
type according to Pyright.Expected Result / Solution Idea
The
res
variable should not have an unknown type. Given the following code, the type of res should be something likeCustomMinMax
whereCustomMinMax
is a generated pydantic model/named tuple/dataclass like the following:Not sure how possible that would be to do.
Has anybody faced the same issue or has any ideas on ways around this so the
res
variable has an appropriate type, or maybe I'm just missing a pyright config option that solves this problem?Operating System
Linux
Operating System Details
Arch
SQLModel Version
0.0.8
Python Version
3.9.13
Additional Context
Pyright version: 1.1.271