dropbox / sqlalchemy-stubs

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

Missing __iter__ for ORM relationship #182

Open jd opened 4 years ago

jd commented 4 years ago

With a class attribute such as:

foobaz = sqlalchemy.orm.relationship(
    foobar,
    back_populates="relationxyz",
    cascade="all, delete, delete-orphan",
)

Iterating over the attributes raises:

[x for x in myinstance.foobaz]
error: "MyClass" has no attribute "__iter__" (not iterable)

It might need a workaround like: https://github.com/dropbox/sqlalchemy-stubs/commit/91e8ca9b369d8980459e5b673d40cae9d3d88b92

jd commented 4 years ago

That might be more than that. You can easily get other warnings such as:

error: Argument 1 to "len" has incompatible type "MyClass"; expected "Sized"
error: Value of type "MyClass" is not indexable

etc.

It sounds like the relationship type is not considered as a list at all whereas it can act like one.

mrcljx commented 3 years ago

The plugin requires uselist=True to be specified (probably for performance reasons).