sizmailov / pybind11-stubgen

Generate stubs for python modules
Other
232 stars 47 forks source link

error: Name "buffer" is not defined #87

Closed mdorier closed 1 year ago

mdorier commented 1 year ago

Similar to issue https://github.com/sizmailov/pybind11-stubgen/issues/86 but when using pybind11::buffer. This time the solution is not as simple as adding a class buffer, because a pybind11::buffer represents any class that satisfies the buffer protocol, and there doesn't seem to be a generic class in typing for that (see https://github.com/python/typing/issues/593).

Not sure what the solution should be. For now I have manually added buffer = typing.Any just to silence errors.

sizmailov commented 1 year ago

Hi! Thanks for the report.

I don't think that the solution you propose would work for everyone. And probably there would be none until python provides a counterpart to buffer protocol. The alternatives I'm thinking of are not great either, so I'm unsure how to proceed.

By the way, are there other similar protocols/structures not exposed to the python interpreter? There should be just a handful of them.

sizmailov commented 1 year ago

PEP 688 – Making the buffer protocol accessible in Python: https://peps.python.org/pep-0688/

sizmailov commented 1 year ago

In the meantime, PEP 688 was accepted. I've added the renaming buffer -> typing_extenensions.Buffer

Here are the relevant parts of tests and implementation.

https://github.com/sizmailov/pybind11-stubgen/blob/a1cd5faab09e642d4c0692474790ca737a271baf/tests/stubs/demo/_bindings/typing.pyi#L5-L9

https://github.com/sizmailov/pybind11-stubgen/blob/a1cd5faab09e642d4c0692474790ca737a271baf/tests/py-demo/bindings/src/modules/typing.cpp#L5

https://github.com/sizmailov/pybind11-stubgen/blob/a1cd5faab09e642d4c0692474790ca737a271baf/pybind11_stubgen/parser/mixins/fix.py#L309-L330