ScanMountGoat / ldr_tools_blender

LDraw Blender importer addon
MIT License
14 stars 4 forks source link

Create type stubs for `ldr_tools_py` #39

Closed The0x539 closed 3 weeks ago

The0x539 commented 4 weeks ago

Self-explanatory.

I don't know what's going on with the imports - this was the only approach I could find that satisfied mypy while also actually working at runtime, other than putting the .pyi file in ldr_tools_blender, contrary to PyO3's recommendations. There's probably a better way, and I wouldn't be surprised if it involves bundling the compiled library in a slightly different fashion.

ScanMountGoat commented 3 weeks ago

I would rather have the type annotations use the correct type (numpy.ndarray) for fields even if Blender's type annotations don't accept it. The import workaround makes sense for now since the ldr_tools_py module is only included as part of the build process. I normally wrap enum variants in ClassVar[], but that doesn't seem to make any practical difference at least with VSCode's Python plugin.

The0x539 commented 3 weeks ago

I would rather have the type annotations use the correct type

Huh, it looks like the ndarray issue even goes away on its own with Python 3.12. That's nice. I check my types with a direct CLI invocation of mypy, so I was trying to get the number of errors as low as I can. (I have some stashed changes that get it down to zero with some conditional stuff for the operator properties.)

I normally wrap enum variants in ClassVar[], but that doesn't seem to make any practical difference at least with VSCode's Python plugin.

That seems like a sensible thing to do - I just wasn't aware of it. It looks like ClassVar only prevents instance-level writes to the attribute. Final prevents all writes and nothing seems to prevent instance-level reads. The two wrappers are incompatible for some reason, so I'm leaning towards adding Final. (This is also advised by PEP-591.)

ScanMountGoat commented 3 weeks ago

Huh, it looks like the ndarray issue even goes away on its own with Python 3.12.

I expect Blender's Python version to be on Python 3.11 for a while. This PR is still a nice improvement.