diff mbox series

[libgpiod,22/22] bindings: python: configure and document dev dependencies

Message ID 20240927-vfazio-mypy-v1-22-91a7c2e20884@xes-inc.com
State Superseded
Headers show
Series bindings: python: conform to mypy and ruff linter recommendations | expand

Commit Message

Vincent Fazio Sept. 27, 2024, 6:53 p.m. UTC
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 the README 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(+)

Comments

Bartosz Golaszewski Oct. 8, 2024, 1:35 p.m. UTC | #1
On Fri, Sep 27, 2024 at 8:57 PM Vincent Fazio <vfazio@xes-inc.com> wrote:
>
> 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 the README 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(+)
>
> diff --git a/bindings/python/README.md b/bindings/python/README.md
> index cb5cee62cc46980ce2484bc85d8686ffb8622e59..89c824cebbd735624159a5b30a8bbee2c55e896e 100644
> --- a/bindings/python/README.md
> +++ b/bindings/python/README.md
> @@ -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
> diff --git a/bindings/python/pyproject.toml b/bindings/python/pyproject.toml
> index f6bf43c0a20edc76bfa51a98e7d523c8dadefea1..43e85a72daaea50c07a527d7b388ac9a4396a3d8 100644
> --- a/bindings/python/pyproject.toml
> +++ b/bindings/python/pyproject.toml
> @@ -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", "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
>
> --
> 2.34.1
>

LGTM

Bart
diff mbox series

Patch

diff --git a/bindings/python/README.md b/bindings/python/README.md
index cb5cee62cc46980ce2484bc85d8686ffb8622e59..89c824cebbd735624159a5b30a8bbee2c55e896e 100644
--- a/bindings/python/README.md
+++ b/bindings/python/README.md
@@ -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
diff --git a/bindings/python/pyproject.toml b/bindings/python/pyproject.toml
index f6bf43c0a20edc76bfa51a98e7d523c8dadefea1..43e85a72daaea50c07a527d7b388ac9a4396a3d8 100644
--- a/bindings/python/pyproject.toml
+++ b/bindings/python/pyproject.toml
@@ -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", "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