Skip to content

Commit 11a35f7

Browse files
authored
Added linters to pyo3 tests (#3728)
This runs `black`, `isort`, `mypy`, and `pylint` on all python targets for `PyO3` to keep tests and examples as clean and correct as possible. Mypy notably tests the stub generation.
1 parent a3bb997 commit 11a35f7

19 files changed

+631
-5
lines changed

.bazelci/presubmit.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,8 +1160,20 @@ tasks:
11601160
shell_commands:
11611161
- sed -i 's/^# load("@bazel_ci_rules/load("@bazel_ci_rules/' WORKSPACE.bazel
11621162
- sed -i 's/^# rbe_preconfig/rbe_preconfig/' WORKSPACE.bazel
1163-
build_flags: *aspects_flags
1164-
test_flags: *aspects_flags
1163+
build_flags:
1164+
- "--config=rustfmt"
1165+
- "--config=clippy"
1166+
- "--config=no_mypy"
1167+
- "--config=no_black"
1168+
- "--config=no_pylint"
1169+
- "--config=no_isort"
1170+
test_flags:
1171+
- "--config=rustfmt"
1172+
- "--config=clippy"
1173+
- "--config=no_mypy"
1174+
- "--config=no_black"
1175+
- "--config=no_pylint"
1176+
- "--config=no_isort"
11651177
build_targets:
11661178
- "//..."
11671179
test_targets:
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
# This workflow will install Python dependencies, run tests and lint with a single version of Python
3+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
4+
5+
name: compile_pyo3_dev_requirements
6+
7+
on:
8+
workflow_dispatch:
9+
inputs:
10+
target:
11+
description: "The py_reqs_compiler target to run"
12+
default: "//tools/requirements:update"
13+
14+
jobs:
15+
build:
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
include:
20+
- os: linux_x86_64
21+
runs-on: ubuntu-latest
22+
- os: linux_aarch64
23+
runs-on: ubuntu-24.04-arm
24+
- os: macos_aarch64
25+
runs-on: macos-latest
26+
- os: windows_x86_64
27+
runs-on: windows-latest
28+
29+
runs-on: ${{ matrix.runs-on }}
30+
defaults:
31+
run:
32+
working-directory: ./extensions/pyo3
33+
name: ${{ matrix.os }}
34+
steps:
35+
- uses: actions/checkout@v4
36+
- name: Compile requirements
37+
if: runner.os == 'Windows'
38+
run: |
39+
bazel run "$env:TARGET" "--" "--upgrade" "--verbose"
40+
env:
41+
TARGET: ${{ github.event.inputs.target }}
42+
- name: Compile requirements
43+
if: runner.os != 'Windows'
44+
run: |
45+
bazel run "${TARGET}" "--" "--upgrade" "--verbose"
46+
env:
47+
TARGET: ${{ github.event.inputs.target }}
48+
- name: Display results
49+
shell: bash
50+
run: |
51+
set -e
52+
echo "<details>" >> "${GITHUB_STEP_SUMMARY}"
53+
echo "" >> "${GITHUB_STEP_SUMMARY}"
54+
echo '```' >> "${GITHUB_STEP_SUMMARY}"
55+
cat "tools/requirements/requirements_${{ matrix.os }}.txt" >> "${GITHUB_STEP_SUMMARY}"
56+
echo '```' >> "${GITHUB_STEP_SUMMARY}"
57+
echo "" >> "${GITHUB_STEP_SUMMARY}"
58+
echo "</details>" >> "${GITHUB_STEP_SUMMARY}"

extensions/pyo3/.bazelrc

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,34 @@ build --sandbox_default_allow_network=false
2323
# registered in the WORKSPACE.
2424
common --repo_env=BAZEL_DO_NOT_DETECT_PYTHON_TOOLCHAIN=1
2525

26-
# # Enable rustfmt for all targets in the workspace
26+
###############################################################################
27+
## Configuration Flags
28+
###############################################################################
29+
30+
# Enable black for all targets in the workspace
31+
build --aspects=@rules_venv//python/black:defs.bzl%py_black_aspect
32+
build:black --output_groups=+py_black_checks
33+
build:no_black --output_groups=-py_black_checks
34+
35+
# Enable isort for all targets in the workspace
36+
build --aspects=@rules_venv//python/isort:defs.bzl%py_isort_aspect
37+
build --@rules_venv//python/isort:config=//:.isort.cfg
38+
build:isort --output_groups=+py_isort_checks
39+
build:no_isort --output_groups=-py_isort_checks
40+
41+
# Enable mypy for all targets in the workspace
42+
build --aspects=@rules_venv//python/mypy:defs.bzl%py_mypy_aspect
43+
build --@rules_venv//python/mypy:config=//:.mypy.ini
44+
build:mypy --output_groups=+py_mypy_checks
45+
build:no_mypy --output_groups=-py_mypy_checks
46+
47+
# Enable pylint for all targets in the workspace
48+
build --aspects=@rules_venv//python/pylint:defs.bzl%py_pylint_aspect
49+
build --@rules_venv//python/pylint:config=//:.pylintrc.toml
50+
build:pylint --output_groups=+py_pylint_checks
51+
build:no_pylint --output_groups=-py_pylint_checks
52+
53+
# Enable rustfmt for all targets in the workspace
2754
build --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
2855
build:rustfmt --output_groups=+rustfmt_checks
2956
build:no_rustfmt --output_groups=-rustfmt_checks
@@ -44,8 +71,16 @@ build:unpretty --config=nightly
4471
# Convenience configs for enabling linting and formatting
4572
build:strict --config=rustfmt
4673
build:strict --config=clippy
74+
build:strict --config=black
75+
build:strict --config=isort
76+
build:strict --config=pylint
77+
build:strict --config=mypy
4778
build:no_strict --config=no_rustfmt
4879
build:no_strict --config=no_clippy
80+
build:no_strict --config=no_black
81+
build:no_strict --config=no_isort
82+
build:no_strict --config=no_pylint
83+
build:no_strict --config=no_mypy
4984

5085
# When running test enable all linters and formatters
5186
test --config=strict

extensions/pyo3/.isort.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[settings]
2+
profile = black

extensions/pyo3/.mypy.ini

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# https://mypy.readthedocs.io/en/stable/config_file.html
2+
[mypy]
3+
4+
# Improve strictness of checks
5+
strict = True
6+
7+
# Improve logging
8+
pretty = True
9+
10+
# Improve behavior in actions.
11+
explicit_package_bases = True

extensions/pyo3/.pylintrc.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[tool.pylint.main]
2+
3+
# Allow loading of arbitrary C extensions. Extensions are imported into the
4+
# active Python interpreter and may run arbitrary code.
5+
unsafe-load-any-extension = true
6+
7+
# Limit actions to using 1 core per action.
8+
jobs = 1
9+
10+
disable = [
11+
"fixme", # Developers should be allowed to leave TODO comments.
12+
"wrong-import-position", # isort is in charge of import ordering.
13+
"wrong-import-order", # isort is in charge of import ordering.
14+
"line-too-long", # Black is responsible for shortening where possible.
15+
]

extensions/pyo3/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
22
load("//private:pyo3_toolchain.bzl", "current_pyo3_toolchain")
33

44
exports_files([
5+
".isort.cfg",
6+
".mypy.ini",
7+
".pylintrc.toml",
58
"defs.bzl",
69
"MODULE.bazel",
710
"version.bzl",

extensions/pyo3/MODULE.bazel

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,26 @@ use_repo(
3737
rust_ext_dev,
3838
"buildkite_config",
3939
)
40+
41+
bazel_dep(name = "rules_venv", version = "0.7.0", dev_dependency = True)
42+
bazel_dep(name = "rules_req_compile", version = "1.0.0rc38", dev_dependency = True)
43+
44+
requirements = use_extension("@rules_req_compile//extensions:python.bzl", "requirements", dev_dependency = True)
45+
requirements.parse(
46+
name = "pip_deps",
47+
requirements_locks = {
48+
"//tools/requirements:requirements_linux_aarch64.txt": "@@//tools/requirements:linux_aarch64",
49+
"//tools/requirements:requirements_linux_x86_64.txt": "@@//tools/requirements:linux_x86_64",
50+
"//tools/requirements:requirements_macos_aarch64.txt": "@@//tools/requirements:macos_aarch64",
51+
"//tools/requirements:requirements_windows_x86_64.txt": "@@//tools/requirements:windows_x86_64",
52+
},
53+
)
54+
use_repo(requirements, "pip_deps")
55+
56+
register_toolchains(
57+
"//tools/toolchains:black_toolchain",
58+
"//tools/toolchains:isort_toolchain",
59+
"//tools/toolchains:mypy_toolchain",
60+
"//tools/toolchains:pylint_toolchain",
61+
dev_dependency = True,
62+
)

extensions/pyo3/settings/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ package(default_visibility = ["//visibility:public"])
1010
# agnostic or incorrect stubs will be generated.
1111
#
1212
# The use of this feature requires the `experimental-inspect` feature. For
13-
# details see: https://pyo3.rs/v0.23.4/features.html#experimental-inspect
13+
# details see: https://pyo3.rs/v0.26.0/features.html#experimental-inspect
1414
bool_flag(
1515
name = "experimental_stubgen",
1616
build_setting_default = True,

extensions/pyo3/test/string_sum/string_sum_import_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import unittest
44

5-
from string_sum_import import sum_as_string
5+
# TODO: https://github.com/periareon/rules_venv/issues/80
6+
from string_sum_import import sum_as_string # type: ignore
67

78

89
class StringSumTest(unittest.TestCase):

0 commit comments

Comments
 (0)