project-gemmi / gemmi

macromolecular crystallography library and utilities
https://project-gemmi.github.io/
Mozilla Public License 2.0
205 stars 42 forks source link

[FEATURE REQUEST] Add Python typing for `Vec3.__iter__` #306

Closed rjdirisio closed 3 months ago

rjdirisio commented 3 months ago

Looking at the built types in __init__.pyi for Vec3, there is no __iter__ method displayed. But, if one loops over Vec3 in python, one iterates over x, y, z.

>>> from gemmi import Vec3
>>> vec = Vec3(1.0, 2.0, 3.0)
>>> for i in vec: print(i)
... 
1.0
2.0
3.0
rjdirisio commented 3 months ago

For more detail, I believe iteration is currently possible because of __getitem__ implemented in Vec3. But, this means that iterating over these fields does not obey modern type checking.

If the answer is simply 'use .tolist()' I can do that, but I figured I would bring this to your attention.

wojdyr commented 3 months ago

That's the first feedback I got regarding Python typing – thanks!

This seems to be a limitation of Python type checking, so I'd just ignore (# type: ignore) it.

Currently, pyi is automatically generated by pybind11-stubgen. Soon (this or next month), I'll switch to nanobind and when nanobind 2.0 gets released, it should have internal support for generating pyi files. I won't be trying to improve the stubs until then.