Skip to contentSkip to conversation

bindings: python: chip: use literals for dict/list and declare types

PATCH Applied Vincent Fazio 2 participants 7 messages Oct 24 – Oct 28 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH v2 9/14 bindings: python: chip: use literals for dict/list and declare types

Per linter recommendation [0], use the literal form of the dict and list constructors. The long form is not common in typical Python code so it stays as readable for someone familiar with the short form syntax and it has a very negligible performance benefit.

While making this change, specify type annotations for the collections to reflect their contents so type checkers do not try to determine their contents implicitly based on assignments.

[0]: https://docs.astral.sh/ruff/rules/unnecessary-collection-call/

---
Changes in v2:
- Reword commit message to provide better rationale (Bartosz)

---
 bindings/python/gpiod/chip.py | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
bindings/python/gpiod/chip.py +5 -5
index 8712b2b..94c432a 100644
@@ -275,13 +275,13 @@ class Chip:
                 for line, value in output_values.items()
             }
 
-        name_map = dict()
-        requested_lines = list()
-        global_output_values = list()
-        seen_offsets = set()
+        name_map: dict[str, int] = {}
+        requested_lines: list[int | str] = []
+        global_output_values: list[Value] = []
+        seen_offsets: set[int] = set()
 
         for line, settings in config_iter(config):
-            offsets = list()
+            offsets: list[int] = []
 
             offset = self.line_offset_from_id(line)
             # don't allow offset repetitions or offset-name conflicts.
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 10/14 bindings: python: bump ruff linter dependency

Bump ruff to the recently released 0.17. Most of the important changes are from 0.16, but there were no incompatibilities found during testing.

Of note is the expansion of the default ruleset [0] which is now leveraged over the previously selected rules and simplifies the tool configuration section significantly.

Two things are explicitly disabled as part of this migration:

* Formating of Python blocks in markdown files is disabled [1]
* The typing-extensions flag is set to false since the bindings do not depend on this package [2] so appropriate lint rules get disabled

As part of this change:

* Unused error suppression flags were removed from examples/toggle_multiple_line_values.py
* tests/tests_line_requests.py was reformatted to bring it in line with the rest of the bindings
* The readlines call in setup.py has FURB129 [3] suppressed since it only affects builds (edge case) on 3.10 (now EOL). When the minimal version is bumped to 3.11, the entire block will go away.

[0]: https://astral.sh/blog/ruff-v0.16.0#better-default-rule-set
[1]: https://docs.astral.sh/ruff/formatter/#markdown-code-formatting
[2]: https://docs.astral.sh/ruff/settings/#lint_typing-extensions
[3]: https://docs.astral.sh/ruff/rules/readlines-in-for/#readlines-in-for-furb129

---
 .../examples/toggle_multiple_line_values.py     |  4 ++--
 bindings/python/pyproject.toml                  | 17 ++++++-----------
 bindings/python/setup.py                        |  2 +-
 bindings/python/tests/tests_line_request.py     |  3 ++-
 4 files changed, 11 insertions(+), 15 deletions(-)
bindings/python/examples/toggle_multiple_line_values.py +2 -2
index 8c973f8..e5fbcd9 100755
@@ -29,9 +29,9 @@ def toggle_multiple_line_values(
     )
 
     while True:
-        print(" ".join(f"{l}={value_str[v]}" for (l, v) in line_values.items()))  # noqa: E741
+        print(" ".join(f"{l}={value_str[v]}" for (l, v) in line_values.items()))
         time.sleep(1)
-        for l, v in line_values.items():  # noqa: E741
+        for l, v in line_values.items():
             line_values[l] = toggle_value(v)
         request.set_values(line_values)
 
bindings/python/pyproject.toml +6 -11
index 3337306..7d1c777 100644
@@ -36,7 +36,7 @@ Issues = "https://github.com/brgl/libgpiod/issues/"
 Changelog = "https://github.com/brgl/libgpiod/blob/master/bindings/python/CHANGELOG.md"
 
 [dependency-groups]
-lint = ["mypy>=1.18.1", "ruff~=0.15.0", "types-setuptools", "packaging"]
+lint = ["mypy>=1.18.1", "ruff~=0.17.0", "types-setuptools", "packaging"]
 
 [tool.setuptools]
 platforms = ["linux"]
@@ -56,19 +56,14 @@ files = [
   ".",
 ]
 
+[tool.ruff]
+extend-exclude = ["*.md"]
+
 [tool.ruff.lint]
-select = ["B", "E", "F", "I", "TC", "UP"]
-ignore=[
-  # Ignore chained exception warnings for now: https://docs.astral.sh/ruff/rules/raise-without-from-inside-except/
-  "B904",
-  # Never enforce line length violations. Let the formatter handle it: https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
-  "E501",
-]
+typing-extensions = false
 
 [tool.ruff.lint.per-file-ignores]
-"gpiod/__init__.py" = ["F403", "F405"]  # ignore warnings about star imports
-"tests/__main__.py" = ["F403"]
-"tests/**.py" = ["F841"]  # ignore warnings about unused variables
+"tests/**.py" = ["F841", "SIM117"]  # ignore warnings about unused variables and chaining context managers
 
 [tool.cibuildwheel]
 build = "cp*"
bindings/python/setup.py +1 -1
index 4297615..af922c8 100644
@@ -167,7 +167,7 @@ def fetch_tarball(func: Callable[[T], None]) -> Callable[[T], None]:
                     project = cast("dict[str,str]", tomllib.load(f).get("project"))
                     license_file = project.get("license")
                 else:  # tomllib isn't standard, fall back to parsing by line
-                    for line in f.readlines():
+                    for line in f.readlines():  # noqa: FURB129
                         _line = line.decode()
                         if _line.startswith("license = "):
                             license_file = _line.split("=")[1].strip(' \r\n"')
bindings/python/tests/tests_line_request.py +2 -1
index bc85eba..8e74490 100644
@@ -692,7 +692,8 @@ class UsingMoreLinesThanRequestedNotAllowed(TestCase):
 
     def test_line_set_more_values_than_requested_lines(self) -> None:
         with gpiod.request_lines(
-            self.sim.dev_path, config={0: gpiod.LineSettings(direction=Direction.OUTPUT)}
+            self.sim.dev_path,
+            config={0: gpiod.LineSettings(direction=Direction.OUTPUT)},
         ) as req:
             with self.assertRaises(ValueError):
                 req.set_values({i: Value.ACTIVE for i in range(64)})
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 11/14 bindings: python: include Python.h before other headers

In reply to Vincent Fazio

Per the Python C API reference manual [0], Python.h should be included before other headers since it specifies some pre-processor definitions that may differ from system defaults.

This was noticed when testing on CPython 3.15 [1] which emitted warnings due to redefinitions of _XOPEN_SOURCE and _POSIX_C_SOURCE during builds of the Python bindings.

Note: despite the manual stating that PY_SSIZE_T_CLEAN should also be defined, it is intentionally not included. The Python bindings do not use the # format specifier when parsing any arguments so defining it provides no value [2] and it is deprecated in future versions.

[0]: https://docs.python.org/3/c-api/intro.html#include-files
[1]: https://github.com/python/cpython/commit/927eb448aad6436d794ec9f55780013e25609600
[2]: https://docs.python.org/3/c-api/arg.html#strings-and-buffers

---
Changes in v2:
- Add an blank line between Python.h and other includes (Bartosz)

---
 bindings/python/gpiod/ext/internal.h | 3 ++-
 bindings/python/gpiod/ext/module.c   | 3 ++-
 bindings/python/tests/gpiosim/ext.c  | 3 ++-
 bindings/python/tests/system/ext.c   | 1 +
 4 files changed, 7 insertions(+), 3 deletions(-)
bindings/python/gpiod/ext/internal.h +2 -1
index 2996884..b600071 100644
@@ -4,9 +4,10 @@
 #ifndef __LIBGPIOD_PYTHON_MODULE_H__
 #define __LIBGPIOD_PYTHON_MODULE_H__
 
-#include <gpiod.h>
 #include <Python.h>
 
+#include <gpiod.h>
+
 PyObject *Py_gpiod_SetErrFromErrno(void);
 unsigned int Py_gpiod_PyLongAsUnsignedInt(PyObject *pylong);
 void Py_gpiod_dealloc(PyObject *self);
bindings/python/gpiod/ext/module.c +2 -1
index 21ea519..f13b86e 100644
@@ -1,9 +1,10 @@
 // SPDX-License-Identifier: LGPL-2.1-or-later
 // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]>
 
-#include <gpiod.h>
 #include <Python.h>
 
+#include <gpiod.h>
+
 struct module_const {
 	const char *name;
 	long val;
bindings/python/tests/gpiosim/ext.c +2 -1
index cc7bd37..e3cb2ef 100644
@@ -1,9 +1,10 @@
 // SPDX-License-Identifier: LGPL-2.1-or-later
 // SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]>
 
-#include <gpiosim.h>
 #include <Python.h>
 
+#include <gpiosim.h>
+
 struct module_const {
 	const char *name;
 	long val;
bindings/python/tests/system/ext.c +1 -0
index 8b307f6..7d8304d 100644
@@ -3,6 +3,7 @@
 // SPDX-FileCopyrightText: 2024 Bartosz Golaszewski <[email protected]>
 
 #include <Python.h>
+
 #include <linux/version.h>
 #include <sys/prctl.h>
 #include <sys/utsname.h>
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 12/14 bindings: python: advertise Python 3.15 support

In reply to Vincent Fazio

Add a trove classifier to advertise that the bindings have support for Python 3.15.

---
 bindings/python/pyproject.toml | 1 +
 1 file changed, 1 insertion(+)
bindings/python/pyproject.toml +1 -0
index 7d1c777..32a4fe3 100644
@@ -27,6 +27,7 @@ classifiers = [
   "Programming Language :: Python :: 3.12",
   "Programming Language :: Python :: 3.13",
   "Programming Language :: Python :: 3.14",
+  "Programming Language :: Python :: 3.15",
   "Programming Language :: Python :: Free Threading :: 3 - Stable",
 ]
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 13/14 bindings: python: fix directory validation in artifact script

In reply to Vincent Fazio

The PyPI artifact generation script had two issues with its directory validation:

* When validating the source directory, it erroneously tested whether output_dir was a directory instead of source_dir.
* When validating the output directory, it only tested for writability (-w), which would allow an existing writable regular file to pass validation and only fail later when copying build artifacts.

Fix the source_dir check to inspect source_dir, and ensure output_dir is verified to be a directory in addition to being writable.

Fixes 72d2fa01 ("bindings: python: add script to generate sdist and wheels")
---
 bindings/python/generate_pypi_artifacts.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
bindings/python/generate_pypi_artifacts.sh +3 -3
index 22dad9f..bc8aa05 100755
@@ -58,13 +58,13 @@ while getopts :hv:o:s:cf value; do
 	esac
 done
 
-if [ -z "${source_dir}" ] || [ ! -d "${output_dir}" ]; then
+if [ -z "${source_dir}" ] || [ ! -d "${source_dir}" ]; then
 	printf "Invalid source directory %s.\n" "${source_dir}"
 	exit 1
 fi
 
-if [ -z "${output_dir}" ] || [ ! -w "${output_dir}" ]; then
-	printf "Output directory %s is not writable.\n" "${output_dir}"
+if [ -z "${output_dir}" ] || [ ! -d "${output_dir}" ] || [ ! -w "${output_dir}" ]; then
+	printf "Output directory %s is not writable directory.\n" "${output_dir}"
 	exit 1
 fi
 
Signature · 1 line
2.43.0
#5 lore raw

PATCH v2 14/14 bindings: python: bump dependencies in artifact script

In reply to Vincent Fazio

Bump the build and cibuildwheel dependencies to take recent fixes.

The new version of cibuildwheel enables building wheels for the recently release Python 3.15. It also contains fixes that address recent eMudhra CA concerns [0].

[0]: https://cibuildwheel.pypa.io/en/stable/changelog/#v434

---
Changes in v2:
- Bump cibuildwheel to 4.3.4 to include eMudhra fixes

---
 bindings/python/generate_pypi_artifacts.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
bindings/python/generate_pypi_artifacts.sh +1 -1
index bc8aa05..c2febe6 100755
@@ -108,7 +108,7 @@ python3 -m "${venv_module}" .venv
 venv_python="${temp_dir}/.venv/bin/python"
 
 # Install build dependencies
-${venv_python} -m pip install build==1.3.0 cibuildwheel==3.2.1
+${venv_python} -m pip install build==1.6.1 cibuildwheel==4.3.4
 
 LIBGPIOD_VERSION=${src_version} ${venv_python} -m build --sdist --outdir ./dist "${source_dir}"
 sdist=$(find ./dist -name '*.tar.gz')
Signature · 1 line
2.43.0
#6 lore raw
In reply to Vincent Fazio
Quoted 10 lines · Vincent Fazio

Per linter recommendation [0], use the literal form of the dict and list constructors. The long form is not common in typical Python code so it stays as readable for someone familiar with the short form syntax and it has a very negligible performance benefit.

While making this change, specify type annotations for the collections to reflect their contents so type checkers do not try to determine their contents implicitly based on assignments.

[...]

Applied, thanks!

[09/14] bindings: python: chip: use literals for dict/list and declare types

        https://git.kernel.org/brgl/c/bc08d15e3070c6c18617019f324e30b76a142e0e
[10/14] bindings: python: bump ruff linter dependency
        https://git.kernel.org/brgl/c/984547d54d4c4f7270bfd83dbfb6d46412bc362f
[11/14] bindings: python: include Python.h before other headers
        https://git.kernel.org/brgl/c/ebd9313686c221cf87c8b1b420135200670cf1ca
[12/14] bindings: python: advertise Python 3.15 support
        https://git.kernel.org/brgl/c/b82f782b6d1d30e43dae71073703f1a5769e2f24
[13/14] bindings: python: fix directory validation in artifact script
        https://git.kernel.org/brgl/c/c8fc2496ead59f4a5b3c9d1d4892c370447f8342
[14/14] bindings: python: bump dependencies in artifact script
        https://git.kernel.org/brgl/c/0d95a5ffcfe3f813984d3dec46bc714b328d95d7

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>