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
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 asint
instead ofColumn[int]
that one would expect. Maybe there is a better way to type this method?Note: for a MWE, I have used
Union[MyModel, MyModel]
but it could just as well be two different models both defining thecreated
columnNote: using
Union[Model]
does not trigger the error