sqlalchemy-stubs
sqlalchemy-stubs copied to clipboard
insert(model)
Hello,
The latest version of the stubs makes mypy log an error on insert(SAModel).from_select(...):
Argument 1 to "Insert" has incompatible type "Type[SAModel]"; expected "Union[str, Selectable]"
while this is something that works with SQLAlchemy.
@bryanforbes Could you please check this?
I'll take a look at this by the end of the week. @mehdigmira can you tell me what SAModel is?
I'm assuming that SAModel is a class that uses declarative_base(). If that's the case, this will be hard to do with stubs alone because models are registered through sqlalchemy.inspection._inspects. @ilevkivskyi suggested in chat that we could support it via the plugin, but I'd need to understand how it works before I would be able to update it.
I ran into this problem today as well, I have the SQLAlchemy plugin for mypy. I'm using a declarative base class with the @as_declarative decorator.
I've come across a related problem, but with update.
I have a database model create like:
from sqlalchemy.ext.automap import automap_base
ModelBase: Any = automap_base()
class MyTable(ModelBase):
"""A table"""
id = Column(Integer, primary_key=True)
name = Column(String)
Then attempting to create an SQL statement:
from sqlalchemy import update
statement = update(MyTable).where(id == some_value).value(**some_values)
The result from mypy is:
Argument 1 to "Update" has incompatible type "Type[MyTable]"; expected "Union[str, Selectable]"
SQLAlchemy == 1.4.15 sqlalchemy-stubs == 0.4 mypy == 0.800 mypy-extensions == 0.4.3
Getting the same thing as @rjaduthie:
table
class MyTable(Base):
id = Column(Integer, primary_key=True)
fk = Column(UUID(as_uuid=True), ForeignKey(ForeignTable.id), nullable=False)
property = Column(Text, nullable=False)
created_at = Column(TIMESTAMP(timezone=True), server_default=func.now(), nullable=False)
updated_at = Column(TIMESTAMP(timezone=True), onupdate=func.now())
statement
statement = (
update(MyTable)
.where(MyTable.fk == fk)
.values(property="property")
)
await db_session.execute(statement)
mypy error
Argument 1 to "Update" has incompatible type "Type[MyTable]"; expected "Union[str, Selectable]"
P.s, and yes, Base is declarative_base.
SQLAlchemy == 1.4.23 sqlalchemy-stubs == 0.4 mypy == 0.9500 mypy-extensions == 0.4.3 typing-extensions==3.10.0.2
I'm also running in the same error 😞
any updates?
Same error. Had to # type: ignore the issue.