@@ -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
@@ -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
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> --- bindings/python/README.md | 17 ++++++++++++++++ bindings/python/pyproject.toml | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+)