Jonesus / pony-stubs

Python type stubs for Pony ORM
https://pypi.org/project/pony-stubs/
MIT License
9 stars 0 forks source link

Type[Foo] has no attribute __iter__ #8

Open fluffy-critter opened 2 years ago

fluffy-critter commented 2 years ago

It looks like one of the hairier type-inference things that Pony screws up mypy on is still happening even with this package installed. For example:

publ/path_alias.py:65: error: "Type[PathAlias]" has no attribute "__iter__" (not iterable)
publ/path_alias.py:74: error: "Type[PathAlias]" has no attribute "__iter__" (not iterable)
publ/path_alias.py:77: error: "Type[PathAlias]" has no attribute "__iter__" (not iterable)
publ/entry.py:746: error: "Type[Entry]" has no attribute "__iter__" (not iterable)
publ/entry.py:890: error: "Type[EntryAuth]" has no attribute "__iter__" (not iterable)
publ/entry.py:1008: error: "Type[PathAlias]" has no attribute "__iter__" (not iterable)
publ/entry.py:1011: error: "Type[Entry]" has no attribute "__iter__" (not iterable)
publ/category.py:72: error: "Type[Entry]" has no attribute "__iter__" (not iterable)

The respective lines of code are all simple generator-based queries, e.g.:

# path_alias.py:65
    orm.delete(p for p in model.PathAlias if p.path == path)

# entry.py:746
        limit = max(10, orm.get(orm.count(e) for e in model.Entry) * 5)

# caegory.py:72
        subcat_query = orm.select(e.category for e in model.Entry if e.visible)
Jonesus commented 2 years ago

Yeah I just ran into this in my more extensive mypy testing, for some reason if I nest a similar statement in a simple function it doesn't cause mypy to flag it:

# mypy finds error with __iter__
result = select(c for c in Customer if c.country != "USA")[:]

# mypy doesn't complain at all
def asd():
    result = select(c for c in Customer if c.country != "USA")[:]

I mainly use pyright on my personal projects so didn't catch this before, I figure it is because mypy also doesn't like our class Model(DbEntity) declarations, and so as we force them through with # type: ignore we also lose all the information of our metaclass, for example the __iter__... I'll try to see I can come up with something, it would probably also mean cleaner model declarations which would be really nice

fluffy-critter commented 2 years ago

Ah yeah the metaclass erasure makes sense, although the trivial function wrapping fixing it doesn't make any sense at all.