bindings: python: add missing method type hints
authorVincent Fazio <vfazio@xes-inc.com>
Thu, 14 Nov 2024 14:50:59 +0000 (08:50 -0600)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 19 Nov 2024 14:21:01 +0000 (15:21 +0100)
Add type hints for all method arguments and return values.

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-7-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bindings/python/gpiod/chip.py
bindings/python/gpiod/chip_info.py
bindings/python/gpiod/edge_event.py
bindings/python/gpiod/exception.py
bindings/python/gpiod/info_event.py
bindings/python/gpiod/line.py
bindings/python/gpiod/line_info.py
bindings/python/gpiod/line_request.py
bindings/python/gpiod/line_settings.py

index 1db199e87364a9f41dffa78baa3024b3d0fe3bab..bf38c7f4210ced9c12529321c67c84e392bf3736 100644 (file)
@@ -1,6 +1,8 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
 
+from __future__ import annotations
+
 from collections import Counter
 from errno import ENOENT
 from typing import TYPE_CHECKING, Optional, Union
@@ -15,6 +17,7 @@ from .line_settings import LineSettings, _line_settings_to_ext
 if TYPE_CHECKING:
     from collections.abc import Iterable
     from datetime import timedelta
+    from types import TracebackType
 
     from .chip_info import ChipInfo
     from .info_event import InfoEvent
@@ -70,14 +73,19 @@ class Chip:
         """
         return True if self._chip else False
 
-    def __enter__(self):
+    def __enter__(self) -> Chip:
         """
         Controlled execution enter callback.
         """
         self._check_closed()
         return self
 
-    def __exit__(self, exc_type, exc_value, traceback) -> None:
+    def __exit__(
+        self,
+        exc_type: Optional[type[BaseException]],
+        exc_value: Optional[BaseException],
+        traceback: Optional[TracebackType],
+    ) -> None:
         """
         Controlled execution exit callback.
         """
index 884b910681abbc2069673669539d068a93f6aa72..eb585d6d7efa26492163dc0a731ba263d0232099 100644 (file)
@@ -17,7 +17,7 @@ class ChipInfo:
     label: str
     num_lines: int
 
-    def __str__(self):
+    def __str__(self) -> str:
         return '<ChipInfo name="{}" label="{}" num_lines={}>'.format(
             self.name, self.label, self.num_lines
         )
index a8b2378f9e3a9bdfabd8dde60d5c30fc73766f4c..0d401d896a79dc5204e7ea86f202e37acbd24758 100644 (file)
@@ -39,7 +39,7 @@ class EdgeEvent:
         object.__setattr__(self, "global_seqno", global_seqno)
         object.__setattr__(self, "line_seqno", line_seqno)
 
-    def __str__(self):
+    def __str__(self) -> str:
         return "<EdgeEvent type={} timestamp_ns={} line_offset={} global_seqno={} line_seqno={}>".format(
             self.event_type,
             self.timestamp_ns,
index f9a83c27b73c4ac3a0f4fac1c1b4421b22545645..54208e2ccd9996c0d3256a48ca6ef924a7cce027 100644 (file)
@@ -9,7 +9,7 @@ class ChipClosedError(Exception):
     Error raised when an already closed chip is used.
     """
 
-    def __init__(self):
+    def __init__(self) -> None:
         super().__init__("I/O operation on closed chip")
 
 
@@ -18,5 +18,5 @@ class RequestReleasedError(Exception):
     Error raised when a released request is used.
     """
 
-    def __init__(self):
+    def __init__(self) -> None:
         super().__init__("GPIO lines have been released")
index 7b544aa6436b34613a71ee06d9b675a63ad16989..d9e9564e21338cd8d1e28c567fa2ed2ac29ceb00 100644 (file)
@@ -30,7 +30,7 @@ class InfoEvent:
         object.__setattr__(self, "timestamp_ns", timestamp_ns)
         object.__setattr__(self, "line_info", line_info)
 
-    def __str__(self):
+    def __str__(self) -> str:
         return "<InfoEvent type={} timestamp_ns={} line_info={}>".format(
             self.event_type, self.timestamp_ns, self.line_info
         )
index 828385cbd84a95f207e808fe77022caad4056916..33c73682d2b3231c0754f398ad0a3f2a99854399 100644 (file)
@@ -15,7 +15,7 @@ class Value(Enum):
     INACTIVE = _ext.VALUE_INACTIVE
     ACTIVE = _ext.VALUE_ACTIVE
 
-    def __bool__(self):
+    def __bool__(self) -> bool:
         return self == self.ACTIVE
 
 
index 46e16533802e9c8ff57a697e5b51b8b028d0c061..5ea9568fd5c963c77f53c56ef748ac1e80872ed9 100644 (file)
@@ -58,7 +58,7 @@ class LineInfo:
             self, "debounce_period", timedelta(microseconds=debounce_period_us)
         )
 
-    def __str__(self):
+    def __str__(self) -> str:
         return '<LineInfo offset={} name="{}" used={} consumer="{}" direction={} active_low={} bias={} drive={} edge_detection={} event_clock={} debounced={} debounce_period={}>'.format(
             self.offset,
             self.name,
index c7b32f37cea0168956fb50dbf5ae354554732e3c..0846e6bcb8705db7758a241b6d5826c642c88923 100644 (file)
@@ -1,6 +1,8 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
 
+from __future__ import annotations
+
 from typing import TYPE_CHECKING, Optional, Union
 
 from . import _ext
@@ -11,6 +13,7 @@ from .line_settings import LineSettings, _line_settings_to_ext
 if TYPE_CHECKING:
     from collections.abc import Iterable
     from datetime import timedelta
+    from types import TracebackType
 
     from .edge_event import EdgeEvent
     from .line import Value
@@ -42,14 +45,19 @@ class LineRequest:
         """
         return True if self._req else False
 
-    def __enter__(self):
+    def __enter__(self) -> LineRequest:
         """
         Controlled execution enter callback.
         """
         self._check_released()
         return self
 
-    def __exit__(self, exc_type, exc_value, traceback):
+    def __exit__(
+        self,
+        exc_type: Optional[type[BaseException]],
+        exc_value: Optional[BaseException],
+        traceback: Optional[TracebackType],
+    ) -> None:
         """
         Controlled execution exit callback.
         """
@@ -81,7 +89,7 @@ class LineRequest:
         """
         return self.get_values([line])[0]
 
-    def _check_line_name(self, line):
+    def _check_line_name(self, line: Union[int, str]) -> bool:
         if isinstance(line, str):
             if line not in self._name_map:
                 raise ValueError("unknown line name: {}".format(line))
@@ -216,7 +224,7 @@ class LineRequest:
 
         return self._req.read_edge_events(max_events)
 
-    def __str__(self):
+    def __str__(self) -> str:
         """
         Return a user-friendly, human-readable description of this request.
         """
index f2811b288f4e832802217e9249a71a4db0eb1a2d..6c6518dbc958423393790a39d69e94802eda8547 100644 (file)
@@ -27,7 +27,7 @@ class LineSettings:
 
     # __repr__ generated by @dataclass uses repr for enum members resulting in
     # an unusable representation as those are of the form: <NAME: $value>
-    def __repr__(self):
+    def __repr__(self) -> str:
         return "gpiod.LineSettings(direction=gpiod.line.{}, edge_detection=gpiod.line.{}, bias=gpiod.line.{}, drive=gpiod.line.{}, active_low={}, debounce_period={}, event_clock=gpiod.line.{}, output_value=gpiod.line.{})".format(
             str(self.direction),
             str(self.edge_detection),
@@ -39,7 +39,7 @@ class LineSettings:
             str(self.output_value),
         )
 
-    def __str__(self):
+    def __str__(self) -> str:
         return "<LineSettings direction={} edge_detection={} bias={} drive={} active_low={} debounce_period={} event_clock={} output_value={}>".format(
             self.direction,
             self.edge_detection,