nutti / fake-bpy-module

Fake Blender Python API module collection for the code completion.
MIT License
1.35k stars 96 forks source link

Collections are not usable #100

Closed felixSchl closed 2 years ago

felixSchl commented 2 years ago

Expected behavior

I expected to be able to iterate over collections w/o running into type errors, however I am seeing this:

image

The problem is that the collection type is declared as a union as opposed to an intersection type. It's a bit more obvious in this error message:

image

I am new to mypy but from a bit of research it doesn't appear like they support intersection types just yet, but I played around with it and the desired type can be achieved by doing e.g.:

class ObjectCollection(
    typing.Sequence[bpy.types.Object],
    typing.Mapping[str, bpy.types.Object],
    # HOWEVER, cannot enable these due to incompatible .items() declarations
    # bpy.types.SceneObjects,
    # bpy.types.bpy_prop_collection
):
    pass

objects: ObjectCollection = ...
o = objects.get("foo")

So I am not sure what's the best approach but it looks like we'd have to generate class stubs based off all the things that go into the union and resolve mismatches...

nutti commented 2 years ago

@felixSchl

This relates to #97 . Current analysis algorithm does not support this kind of types. I think this should be fixed.

nutti commented 2 years ago

I applied @MicaelJarniac 's suggestion. Now, this issue is solved.