Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: Name "buffer" is not defined #87

Closed
mdorier opened this issue Sep 29, 2022 · 3 comments
Closed

error: Name "buffer" is not defined #87

mdorier opened this issue Sep 29, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@mdorier
Copy link

mdorier commented Sep 29, 2022

Similar to issue #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 python/typing#593).

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

@sizmailov sizmailov added the enhancement New feature or request label Sep 30, 2022
@sizmailov
Copy link
Owner

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
Copy link
Owner

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

@sizmailov
Copy link
Owner

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.

import typing_extensions
__all__ = ["get_buffer", "get_sequence"]
def get_buffer(arg0: typing_extensions.Buffer) -> typing_extensions.Buffer: ...

m.def("get_buffer", [](py::buffer &buffer) { return buffer; });

class FixTypingExtTypeNames(IParser):
__typing_names: set[Identifier] = set(
map(
Identifier,
["buffer"],
)
)
def parse_annotation_str(
self, annotation_str: str
) -> ResolvedType | InvalidExpression | Value:
result = super().parse_annotation_str(annotation_str)
if not isinstance(result, ResolvedType):
return result
assert len(result.name) > 0
word = result.name[0]
if word in self.__typing_names and result.parameters is None:
result.name = QualifiedName.from_str(
f"typing_extensions.{word[0].upper()}{word[1:]}"
)
return result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants