[libgpiod][PATCH 6/9] bindings: python: examples: add type annotations

Vincent Fazio
2026-04-01T00:18:09Z

Add type annotations to help with lint checks.

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
---
bindings/python/examples/async_watch_line_value.py | 8 +++++---
bindings/python/examples/find_line_by_name.py | 5 +++--
bindings/python/examples/get_chip_info.py | 2 +-
bindings/python/examples/get_line_info.py | 2 +-
bindings/python/examples/get_line_value.py | 2 +-
bindings/python/examples/get_multiple_line_values.py | 4 +++-
bindings/python/examples/reconfigure_input_to_output.py | 2 +-
bindings/python/examples/toggle_line_value.py | 4 ++--
bindings/python/examples/toggle_multiple_line_values.py | 6 ++++--
bindings/python/examples/watch_line_info.py | 4 +++-
bindings/python/examples/watch_line_rising.py | 2 +-
bindings/python/examples/watch_line_value.py | 5 +++--
bindings/python/examples/watch_multiple_line_values.py | 8 +++++---
13 files changed, 33 insertions(+), 21 deletions(-)

diff --git a/bindings/python/examples/async_watch_line_value.py b/bindings/python/examples/async_watch_line_value.py
index ec42e04..640a4c5 100755
--- a/bindings/python/examples/async_watch_line_value.py
+++ b/bindings/python/examples/async_watch_line_value.py
@@ -8,10 +8,12 @@ import gpiod
import select

from datetime import timedelta
+
+from gpiod.edge_event import EdgeEvent
from gpiod.line import Bias, Edge

-def edge_type_str(event):
+def edge_type_str(event: EdgeEvent) -> str:
if event.event_type is event.Type.RISING_EDGE:
return "Rising"
if event.event_type is event.Type.FALLING_EDGE:
@@ -19,7 +21,7 @@ def edge_type_str(event):
return "Unknown"

-def async_watch_line_value(chip_path, line_offset, done_fd):
+def async_watch_line_value(chip_path: str, line_offset: int, done_fd: int) -> None:
# Assume a button connecting the pin to ground,
# so pull it up and provide some debounce.
with gpiod.request_lines(
@@ -59,7 +61,7 @@ if __name__ == "__main__":
# run the async executor (select.poll) in a thread to demonstrate a graceful exit.
done_fd = os.eventfd(0)

- def bg_thread():
+ def bg_thread() -> None:
try:
async_watch_line_value("/dev/gpiochip0", 5, done_fd)
except OSError as ex:
diff --git a/bindings/python/examples/find_line_by_name.py b/bindings/python/examples/find_line_by_name.py
index ac798a9..4ed3848 100755
--- a/bindings/python/examples/find_line_by_name.py
+++ b/bindings/python/examples/find_line_by_name.py
@@ -6,15 +6,16 @@

import gpiod
import os
+from collections.abc import Generator

-def generate_gpio_chips():
+def generate_gpio_chips() -> Generator[str]:
for entry in os.scandir("/dev/"):
if gpiod.is_gpiochip_device(entry.path):
yield entry.path

-def find_line_by_name(line_name):
+def find_line_by_name(line_name: str) -> None:
# Names are not guaranteed unique, so this finds the first line with
# the given name.
for path in generate_gpio_chips():
diff --git a/bindings/python/examples/get_chip_info.py b/bindings/python/examples/get_chip_info.py
index 7dc76fb..037e424 100755
--- a/bindings/python/examples/get_chip_info.py
+++ b/bindings/python/examples/get_chip_info.py
@@ -7,7 +7,7 @@
import gpiod

-def get_chip_info(chip_path):
+def get_chip_info(chip_path: str) -> None:
with gpiod.Chip(chip_path) as chip:
info = chip.get_info()
print("{} [{}] ({} lines)".format(info.name, info.label, info.num_lines))
diff --git a/bindings/python/examples/get_line_info.py b/bindings/python/examples/get_line_info.py
index cd4ebcc..8e7283e 100755
--- a/bindings/python/examples/get_line_info.py
+++ b/bindings/python/examples/get_line_info.py
@@ -7,7 +7,7 @@
import gpiod

-def get_line_info(chip_path, line_offset):
+def get_line_info(chip_path: str, line_offset: int) -> None:
with gpiod.Chip(chip_path) as chip:
info = chip.get_line_info(line_offset)
is_input = info.direction == gpiod.line.Direction.INPUT
diff --git a/bindings/python/examples/get_line_value.py b/bindings/python/examples/get_line_value.py
index f3ca13b..5ca952b 100755
--- a/bindings/python/examples/get_line_value.py
+++ b/bindings/python/examples/get_line_value.py
@@ -9,7 +9,7 @@ import gpiod
from gpiod.line import Direction

-def get_line_value(chip_path, line_offset):
+def get_line_value(chip_path: str, line_offset: int) -> None:
with gpiod.request_lines(
chip_path,
consumer="get-line-value",
diff --git a/bindings/python/examples/get_multiple_line_values.py b/bindings/python/examples/get_multiple_line_values.py
index 46cf0b2..f70369f 100755
--- a/bindings/python/examples/get_multiple_line_values.py
+++ b/bindings/python/examples/get_multiple_line_values.py
@@ -4,12 +4,14 @@

"""Minimal example of reading multiple lines."""

+from collections.abc import Iterable
+
import gpiod

from gpiod.line import Direction

-def get_multiple_line_values(chip_path, line_offsets):
+def get_multiple_line_values(chip_path: str, line_offsets: Iterable[int]) -> None:
with gpiod.request_lines(
chip_path,
consumer="get-multiple-line-values",
diff --git a/bindings/python/examples/reconfigure_input_to_output.py b/bindings/python/examples/reconfigure_input_to_output.py
index 0f2e358..feb5f0b 100755
--- a/bindings/python/examples/reconfigure_input_to_output.py
+++ b/bindings/python/examples/reconfigure_input_to_output.py
@@ -9,7 +9,7 @@ import gpiod
from gpiod.line import Direction, Value

-def reconfigure_input_to_output(chip_path, line_offset):
+def reconfigure_input_to_output(chip_path: str, line_offset: int) -> None:
# request the line initially as an input
with gpiod.request_lines(
chip_path,
diff --git a/bindings/python/examples/toggle_line_value.py b/bindings/python/examples/toggle_line_value.py
index e0de8fb..bc948de 100755
--- a/bindings/python/examples/toggle_line_value.py
+++ b/bindings/python/examples/toggle_line_value.py
@@ -10,13 +10,13 @@ import time
from gpiod.line import Direction, Value

-def toggle_value(value):
+def toggle_value(value: Value) -> Value:
if value == Value.INACTIVE:
return Value.ACTIVE
return Value.INACTIVE

-def toggle_line_value(chip_path, line_offset):
+def toggle_line_value(chip_path: str, line_offset: int) -> None:
value_str = {Value.ACTIVE: "Active", Value.INACTIVE: "Inactive"}
value = Value.ACTIVE

diff --git a/bindings/python/examples/toggle_multiple_line_values.py b/bindings/python/examples/toggle_multiple_line_values.py
index 12b968d..74ff587 100755
--- a/bindings/python/examples/toggle_multiple_line_values.py
+++ b/bindings/python/examples/toggle_multiple_line_values.py
@@ -10,13 +10,15 @@ import time
from gpiod.line import Direction, Value

-def toggle_value(value):
+def toggle_value(value: Value) -> Value:
if value == Value.INACTIVE:
return Value.ACTIVE
return Value.INACTIVE

-def toggle_multiple_line_values(chip_path, line_values):
+def toggle_multiple_line_values(
+ chip_path: str, line_values: dict[int | str, Value]
+) -> None:
value_str = {Value.ACTIVE: "Active", Value.INACTIVE: "Inactive"}

request = gpiod.request_lines(
diff --git a/bindings/python/examples/watch_line_info.py b/bindings/python/examples/watch_line_info.py
index e49a669..8ae5ac0 100755
--- a/bindings/python/examples/watch_line_info.py
+++ b/bindings/python/examples/watch_line_info.py
@@ -4,10 +4,12 @@

"""Minimal example of watching for info changes on particular lines."""

+from collections.abc import Iterable
+
import gpiod

-def watch_line_info(chip_path, line_offsets):
+def watch_line_info(chip_path: str, line_offsets: Iterable[int]) -> None:
with gpiod.Chip(chip_path) as chip:
for offset in line_offsets:
chip.watch_line_info(offset)
diff --git a/bindings/python/examples/watch_line_rising.py b/bindings/python/examples/watch_line_rising.py
index 7b1c6b0..2f7f25f 100755
--- a/bindings/python/examples/watch_line_rising.py
+++ b/bindings/python/examples/watch_line_rising.py
@@ -9,7 +9,7 @@ import gpiod
from gpiod.line import Edge

-def watch_line_rising(chip_path, line_offset):
+def watch_line_rising(chip_path: str, line_offset: int) -> None:
with gpiod.request_lines(
chip_path,
consumer="watch-line-rising",
diff --git a/bindings/python/examples/watch_line_value.py b/bindings/python/examples/watch_line_value.py
index 171a67c..9b84d29 100755
--- a/bindings/python/examples/watch_line_value.py
+++ b/bindings/python/examples/watch_line_value.py
@@ -7,10 +7,11 @@
import gpiod

from datetime import timedelta
+from gpiod.edge_event import EdgeEvent
from gpiod.line import Bias, Edge

-def edge_type_str(event):
+def edge_type_str(event: EdgeEvent) -> str:
if event.event_type is event.Type.RISING_EDGE:
return "Rising"
if event.event_type is event.Type.FALLING_EDGE:
@@ -18,7 +19,7 @@ def edge_type_str(event):
return "Unknown"

-def watch_line_value(chip_path, line_offset):
+def watch_line_value(chip_path: str, line_offset: int) -> None:
# Assume a button connecting the pin to ground,
# so pull it up and provide some debounce.
with gpiod.request_lines(
diff --git a/bindings/python/examples/watch_multiple_line_values.py b/bindings/python/examples/watch_multiple_line_values.py
index 5906b7d..d0ee35a 100755
--- a/bindings/python/examples/watch_multiple_line_values.py
+++ b/bindings/python/examples/watch_multiple_line_values.py
@@ -4,12 +4,14 @@

"""Minimal example of watching for edges on multiple lines."""

-import gpiod
+from collections.abc import Iterable

+import gpiod
+from gpiod.edge_event import EdgeEvent
from gpiod.line import Edge

-def edge_type_str(event):
+def edge_type_str(event: EdgeEvent) -> str:
if event.event_type is event.Type.RISING_EDGE:
return "Rising"
if event.event_type is event.Type.FALLING_EDGE:
@@ -17,7 +19,7 @@ def edge_type_str(event):
return "Unknown"

-def watch_multiple_line_values(chip_path, line_offsets):
+def watch_multiple_line_values(chip_path: str, line_offsets: Iterable[int]) -> None:
with gpiod.request_lines(
chip_path,
consumer="watch-multiple-line-values",
--
2.43.0