From: Vincent Fazio <vfazio@gmail.com>
To: linux-gpio@vger.kernel.org
Cc: Vincent Fazio <vfazio@gmail.com>
Subject: [libgpiod][PATCH 6/9] bindings: python: examples: add type annotations
Date: Tue, 31 Mar 2026 19:14:55 -0500 [thread overview]
Message-ID: <20260401001459.19159-6-vfazio@gmail.com> (raw)
In-Reply-To: <20260401001459.19159-1-vfazio@gmail.com>
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
next prev parent reply other threads:[~2026-04-01 0:18 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-01 0:14 [libgpiod][PATCH 1/9] bindings: python: build_tests: do not fallback to distutils Vincent Fazio
2026-04-01 0:14 ` [libgpiod][PATCH 2/9] bindings: python: build_tests: simplify the Distribution Vincent Fazio
2026-04-01 0:14 ` [libgpiod][PATCH 3/9] bindings: python: setup: add type annotations Vincent Fazio
2026-04-01 0:14 ` [libgpiod][PATCH 4/9] bindings: python: setup: apply linter recommendations Vincent Fazio
2026-04-01 0:14 ` [libgpiod][PATCH 5/9] bindings: python: setup: use logging module Vincent Fazio
2026-04-01 0:14 ` Vincent Fazio [this message]
2026-04-01 0:14 ` [libgpiod][PATCH 7/9] bindings: python: examples: apply linter recommendations Vincent Fazio
2026-04-01 0:14 ` [libgpiod][PATCH 8/9] bindings: python: add a lint dependency group Vincent Fazio
2026-04-01 0:14 ` [libgpiod][PATCH 9/9] bindings: python: update linter configuration Vincent Fazio
2026-04-02 14:37 ` Bartosz Golaszewski
2026-04-02 15:55 ` Vincent Fazio
2026-04-02 16:42 ` Bartosz Golaszewski
2026-04-02 17:01 ` Vincent Fazio
2026-04-03 9:02 ` Bartosz Golaszewski
2026-04-03 13:09 ` Vincent Fazio
2026-04-03 9:01 ` [libgpiod][PATCH 1/9] bindings: python: build_tests: do not fallback to distutils Bartosz Golaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260401001459.19159-6-vfazio@gmail.com \
--to=vfazio@gmail.com \
--cc=linux-gpio@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox