Skip to content

Commit

Permalink
python: annotate Object.from_bytes_() with collections.abc.Buffer
Browse files Browse the repository at this point in the history
I had some code that passed a ctypes.create_string_buffer() to
Object.from_bytes_(), which is valid at runtime but mypy didn't like. We
can use collections.abc.Buffer instead, but we need the
typing_extensions backport before Python 3.12.

Signed-off-by: Omar Sandoval <[email protected]>
  • Loading branch information
osandov committed Aug 29, 2024
1 parent 2dd01b1 commit be9f330
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion _drgn.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ if sys.version_info < (3, 10):
else:
from typing import TypeAlias

if sys.version_info < (3, 12):
from typing_extensions import Buffer
else:
from collections.abc import Buffer

# This is effectively typing.SupportsIndex without @typing.runtime_checkable
# (both of which are only available since Python 3.8), with a more
# self-explanatory name.
Expand Down Expand Up @@ -1534,14 +1539,17 @@ class Object:
cls,
prog: Program,
type: Union[str, Type],
bytes: bytes,
bytes: Buffer,
*,
bit_offset: IntegerLike = 0,
bit_field_size: Optional[IntegerLike] = None,
) -> Object:
"""
Return a value object from its binary representation.
>>> print(Object.from_bytes_(prog, "int", b"\x10\x00\x00\x00"))
(int)16
:param prog: Program to create the object in.
:param type: Type of the object.
:param bytes: Buffer containing value of the object.
Expand Down

0 comments on commit be9f330

Please sign in to comment.