Skip to content

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

Closed
@qsantos

Description

@qsantos

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions