diff mbox series

[libgpiod,v2,13/23] bindings: python: cast return value of LineRequest.get_values

Message ID 20241114145116.2123714-14-vfazio@xes-inc.com
State New
Headers show
Series bindings: python: conform to mypy and ruff linter recommendations | expand

Commit Message

Vincent Fazio Nov. 14, 2024, 2:51 p.m. UTC
The `values` argument of `_ext.Request.get_values` uses a preallocated
`list[None]` as a buffer that is populated with `Value`s by the external
module that are then returned from the function.

Use `cast` to inform the type checker it's a `list[Value]` despite how
it's allocated.

Also, as `lines` is typed as an `Iterable`, there is no guarantee it has
a `__len__` method. Instead, use the size of the `offsets` array to
allocate the buffer.

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
---
 bindings/python/gpiod/line_request.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/bindings/python/gpiod/line_request.py b/bindings/python/gpiod/line_request.py
index 3f181c9..7327274 100644
--- a/bindings/python/gpiod/line_request.py
+++ b/bindings/python/gpiod/line_request.py
@@ -8,6 +8,7 @@  from typing import TYPE_CHECKING, Optional, Union, cast
 from . import _ext
 from ._internal import poll_fd
 from .exception import RequestReleasedError
+from .line import Value
 from .line_settings import LineSettings, _line_settings_to_ext
 
 if TYPE_CHECKING:
@@ -16,7 +17,6 @@  if TYPE_CHECKING:
     from types import TracebackType
 
     from .edge_event import EdgeEvent
-    from .line import Value
 
 
 __all__ = ["LineRequest"]
@@ -124,7 +124,7 @@  class LineRequest:
 
         offsets = [self._line_to_offset(line) for line in lines]
 
-        buf = [None] * len(lines)
+        buf = cast(list[Value], [None] * len(offsets))
 
         cast(_ext.Request, self._req).get_values(offsets, buf)
         return buf