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

Missing __iter__ for ORM relationship

Open jd opened this issue 3 years ago • 2 comments

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 avatar Oct 22 '20 06:10 jd

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.

jd avatar Oct 22 '20 10:10 jd

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

mrcljx avatar Dec 15 '20 09:12 mrcljx