dropbox / sqlalchemy-stubs

Mypy plugin and stubs for SQLAlchemy
Apache License 2.0
573 stars 101 forks source link

[Column type] has no attribute "desc" #115

Closed qsantos closed 5 years ago

qsantos commented 5 years ago

Let's say you have several models and want to write a method that can work on several of them using duck-typing. I wanted to provide proper typing by explicitly listing the models that could be passed to the method. However, this breaks the use of the model's fields as columns.

In the example below, the error is "int" has no attribute "desc". In other words, the field is interpreted as int instead of Column[int] that one would expect. Maybe there is a better way to type this method?

from typing import Type, Union

from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class MyModel(Base):
    created = Column(Integer, nullable=False)

def f(model: Type[Union[MyModel, MyModel]]) -> None:
    model.created.desc

Note: for a MWE, I have used Union[MyModel, MyModel] but it could just as well be two different models both defining the created column

Note: using Union[Model] does not trigger the error

ilevkivskyi commented 5 years ago

I think this is actually a mypy bug, see https://github.com/python/mypy/issues/5570

qsantos commented 5 years ago

Thanks for taking the time of transferring the bug!