bindings: python: configure and document dev dependencies
authorVincent Fazio <vfazio@xes-inc.com>
Thu, 14 Nov 2024 14:51:16 +0000 (08:51 -0600)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 19 Nov 2024 14:23:58 +0000 (15:23 +0100)
Mypy [0] is a popular static type checker that validates attribute and
variable use and ensures function arguments adhere to type annotations.

Ruff [1] is a popular Rust-based Python linter and code formatter. It
has support for a large set of linting rules [2] and largely complies
with the Black format [3].

Add documentation to README.md for how to run the tools.

[0]: https://mypy.readthedocs.io/en/stable/
[1]: https://docs.astral.sh/ruff/
[2]: https://docs.astral.sh/ruff/rules/
[3]: https://docs.astral.sh/ruff/formatter/#black-compatibility

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-24-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bindings/python/README.md
bindings/python/pyproject.toml

index cb5cee62cc46980ce2484bc85d8686ffb8622e59..89c824cebbd735624159a5b30a8bbee2c55e896e 100644 (file)
@@ -112,3 +112,20 @@ make python-tests-run
 
 from the `libgpiod/bindings/python` directory as root (necessary to be able
 to create the **gpio-sims** used for testing).
+
+## Linting/Formatting
+
+When making changes, ensure type checks and linting still pass:
+
+```
+python3 -m venv venv
+. venv/bin/activate
+pip install mypy ruff
+mypy; ruff format; ruff check
+```
+
+Ideally the gpiod library will continue to pass strict checks:
+
+```
+mypy --strict
+```
\ No newline at end of file
index f6bf43c0a20edc76bfa51a98e7d523c8dadefea1..d6f5f9b5908ce77e73c9cc1d928a35e78189b7e1 100644 (file)
@@ -3,3 +3,39 @@
 
 [build-system]
 requires = ["setuptools", "wheel", "packaging"]
+
+[tool.mypy]
+python_version = "3.9"
+files = [
+  "gpiod/",
+  "tests/",
+]
+
+[[tool.mypy.overrides]]
+module = "gpiod.line.*"
+strict_equality = false # Ignore Enum comparison-overlap: https://github.com/python/mypy/issues/17317
+
+[tool.ruff]
+target-version = "py39"
+include = [
+  "gpiod/**/*.py",
+  "gpiod/**/*.pyi",
+  "tests/**/*.py",
+  "tests/**/*.pyi",
+]
+
+[tool.ruff.lint]
+select = ["B", "E", "F", "I", "TCH", "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",
+  # Ignore new Union (|) syntax until we require 3.10+
+  "UP007",
+]
+
+[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
\ No newline at end of file