bindings: python: fix LineRequest union-attr type errors
authorVincent Fazio <vfazio@xes-inc.com>
Thu, 14 Nov 2024 14:51:04 +0000 (08:51 -0600)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 19 Nov 2024 14:21:01 +0000 (15:21 +0100)
Since `LineRequest._req` is typed to be `Optional` and because type
checkers may not be able to infer that an object is not `None` from an
earlier call (such as `_check_released`) it is necessary to inform type
checkers of the state of the object to silence union-attr [0] errors.

Instead of littering the code with "# type: ignore" comments, use casts
to inform type checkers that objects are not `None`.

Using `assert` is another option, however this duplicates the logic in
`_check_released` so is redundant at best and, at worst, is not a safe
replacement as `assert` can be elided in optimized Python environments
and these checks need to be runtime enforced.

[0]: https://mypy.readthedocs.io/en/stable/error_code_list.html#check-that-attribute-exists-in-each-union-item-union-attr

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-12-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bindings/python/gpiod/line_request.py

index f8bbf643fb3bee53bf8e27bc42c45789b32f0318..81f2517778e82e309a8b1d7b78353c42987946ca 100644 (file)
@@ -3,7 +3,7 @@
 
 from __future__ import annotations
 
-from typing import TYPE_CHECKING, Optional, Union
+from typing import TYPE_CHECKING, Optional, Union, cast
 
 from . import _ext
 from ._internal import poll_fd
@@ -78,7 +78,7 @@ class LineRequest:
         not be used after a call to this method.
         """
         self._check_released()
-        self._req.release()
+        cast(_ext.Request, self._req).release()
         self._req = None
 
     def get_value(self, line: Union[int, str]) -> Value:
@@ -128,7 +128,7 @@ class LineRequest:
 
         buf = [None] * len(lines)
 
-        self._req.get_values(offsets, buf)
+        cast(_ext.Request, self._req).get_values(offsets, buf)
         return buf
 
     def set_value(self, line: Union[int, str], value: Value) -> None:
@@ -158,7 +158,7 @@ class LineRequest:
             for line in values
         }
 
-        self._req.set_values(mapped)
+        cast(_ext.Request, self._req).set_values(mapped)
 
     def reconfigure_lines(
         self,
@@ -193,7 +193,7 @@ class LineRequest:
             settings = line_settings.get(offset) or LineSettings()
             line_cfg.add_line_settings([offset], _line_settings_to_ext(settings))
 
-        self._req.reconfigure_lines(line_cfg)
+        cast(_ext.Request, self._req).reconfigure_lines(line_cfg)
 
     def wait_edge_events(
         self, timeout: Optional[Union[timedelta, float]] = None
@@ -227,7 +227,7 @@ class LineRequest:
         """
         self._check_released()
 
-        return self._req.read_edge_events(max_events)
+        return cast(_ext.Request, self._req).read_edge_events(max_events)
 
     def __str__(self) -> str:
         """
@@ -279,4 +279,4 @@ class LineRequest:
         File descriptor associated with this request.
         """
         self._check_released()
-        return self._req.fd
+        return cast(_ext.Request, self._req).fd