sqlalchemy-stubs icon indicating copy to clipboard operation
sqlalchemy-stubs copied to clipboard

insert(model)

Open mehdigmira opened this issue 6 years ago • 9 comments

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.

mehdigmira avatar Jan 15 '19 09:01 mehdigmira

@bryanforbes Could you please check this?

ilevkivskyi avatar Jan 16 '19 17:01 ilevkivskyi

I'll take a look at this by the end of the week. @mehdigmira can you tell me what SAModel is?

bryanforbes avatar Jan 16 '19 17:01 bryanforbes

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.

bryanforbes avatar Jan 16 '19 22:01 bryanforbes

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.

Hultner avatar Mar 19 '21 15:03 Hultner

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

rjaduthie avatar May 19 '21 13:05 rjaduthie

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

dave-fitzgerald-bc avatar May 03 '22 22:05 dave-fitzgerald-bc

I'm also running in the same error 😞

marquesds avatar Aug 30 '22 00:08 marquesds

any updates?

nailele avatar Jul 12 '23 06:07 nailele

Same error. Had to # type: ignore the issue.

rodrigoreyes79 avatar Sep 14 '23 19:09 rodrigoreyes79