From be9f330e92c8e1e7d0a896ab6cb318585215cb92 Mon Sep 17 00:00:00 2001 From: Omar Sandoval Date: Thu, 29 Aug 2024 16:08:07 -0700 Subject: [PATCH] python: annotate Object.from_bytes_() with collections.abc.Buffer 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 --- _drgn.pyi | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/_drgn.pyi b/_drgn.pyi index 22e96b02b..6b2ae7e64 100644 --- a/_drgn.pyi +++ b/_drgn.pyi @@ -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. @@ -1534,7 +1539,7 @@ class Object: cls, prog: Program, type: Union[str, Type], - bytes: bytes, + bytes: Buffer, *, bit_offset: IntegerLike = 0, bit_field_size: Optional[IntegerLike] = None, @@ -1542,6 +1547,9 @@ class 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.