Skip to content

CI(lint): Enable basedpyright - #249

Merged
tykeal merged 2 commits into
lfreleng-actions:mainfrom
modeseven-lfreleng-actions:ci/enable-basedpyright
Oct 6, 2026
Merged

tykeal merged 2 commits into
lfreleng-actions:mainfrom
modeseven-lfreleng-actions:ci/enable-basedpyright

Conversation

@ModeSevenIndustrialSolutions

@ModeSevenIndustrialSolutions ModeSevenIndustrialSolutions commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

Summary

Brings basedpyright up to the organisation standard: configuration in
pyproject.toml, a hook that resolves every import and fails on
warnings, a scope that covers all real Python code, and zero findings.

Commits

  1. CI(lint): Enable basedpyright: migrates pyrightconfig.json into
    [tool.basedpyright] (and deletes it and its REUSE.toml annotation),
    configures the hook, and makes the fixes standard mode needs.
  2. CI(lint): Lint the type stubs with ruff: the ruff and
    ruff-format files: filters matched only .py under
    src|test|scripts, so the .pyi stubs for the compiled extension,
    which meson installs into nss/, were never linted. typings/nss/error.pyi
    was outside those directories too. The filters now match .pyi and
    typings/. ruff check finds nothing new; ruff format removes 8
    blank lines in error.pyi, following stub style. (This comes second so
    that both commits leave prek run --all-files clean; main already
    fails basedpyright.)

Counts

Check Before (main) After
basedpyright (existing config/hook) 2 errors, 30 warnings n/a
basedpyright (new config, before fixes) 11 errors, 43 warnings 0 errors, 0 warnings
ruff on newly in-scope .pyi 0 lint, 1 file to reformat 0

On main, the hook couldn't resolve pytest, and the 2 errors came
from that. The new configuration's findings before the fixes:

  • 43 × reportMissingModuleSource, all nss.nss / nss.io / nss.ssl / nss.error
  • 2 × reportMissingImports mypy.api (fixed: mypy is a declared test dependency, now a hook dependency)
  • 2 × reportMissingImports nss.deprecations (fixed: see below)
  • 7 × reportPossiblyUnboundVariable (fixed: see below)

Configuration

  • pythonVersion = "3.10": from requires-python = ">=3.10".
  • pythonPlatform = "All", typeCheckingMode = "standard" (was basic).
  • include = ["src", "test", "scripts", "typings", "import_files.py"],
    identical to the hook's arguments. pytest's testpaths = ["test"] is
    inside it. doc/examples (legacy Py2-era examples, which tests only
    syntax-check) and mutants (generated) stay out, as they already do
    for ruff.
  • exclude: the six typings/nss/ symlinks into src/. These are
    already analysed through src/, and scanning them again reported every
    finding twice. I checked that a defect is now reported once, and that
    typings/nss/error.pyi (a real file) is still checked.
  • stubPath = "typings" and the test execution environment with
    extraPaths = ["src"] are carried over, because tests put src/ on
    sys.path and import deprecations, nss_context and
    secure_logging by bare name.
  • Dropped from the old config:
    • venvPath / venv = ".venv", so the hook resolves imports from
      its own environment instead of whatever .venv a developer has.
    • The warning downgrades of reportMissingImports,
      reportGeneralTypeIssues, reportOptionalMemberAccess and
      reportOptionalSubscript, which are errors again.
    • Settings that standard mode already defaults:
      useLibraryCodeForTypes, the reportUnknown* / reportUntyped* /
      reportMissingTypeStubs / reportPrivateUsage overrides, and the
      src / scripts execution environments.

Hook

  • The mirror hook at the template's rev, in the template's position, with
    pass_filenames: false, always_run: true, stages: [pre-commit] and
    args: [--warnings, src, test, scripts, typings, import_files.py].
  • additional_dependencies: hypothesis==6.155.1, mypy==2.1.0 and
    pytest==9.0.3. These are the only third-party modules the scanned code
    imports, derived with git grep -h -E '^(import|from) '. They're pinned
    to uv.lock, because the repository doesn't pin its other hook
    dependencies. All three ship py.typed, so no types-* packages are
    needed.

Size and ci: skip

The local hook environment measures 350 MiB with du -sm (the bare
hook measures 274 MiB, so macOS overstates). pre-commit.ci measures the
bare hook at 248.7 MiB, against its 250 MiB per-hook limit, and any
additional_dependencies push it over. basedpyright therefore stays in
ci: skip, now with a comment giving that reason. No workflow runs
basedpyright, so it's enforced developer-side only, through the local
hook.

The compiled extension

The repository already ships stubs for the compiled nss module: the
src/*.pyi files are installed with the package, and
typings/nss/ gives them the nss.* package layout through symlinks,
plus a real error.pyi. With those, basedpyright types every use of
the compiled API. The tests import it as import nss.nss as nss,
from nss.error import NSPRError, and so on.

  • One suppression, reportMissingModuleSource = false. Each nss
    import finds its stub but not the compiled module, which needs a C
    build against NSS/NSPR and can't be installed in a hook environment.
    This warning is therefore expected and adds nothing, given that the
    stubs supply the types. The suppression is documented inline.
    reportMissingImports stays enabled, and is an error.
  • nss.deprecations. test_zzz_error_messages.py imports
    nss.deprecations. meson installs deprecations, nss_context and
    secure_logging into nss/ alongside the extension, but typings/nss/
    didn't mirror them. I added symlinks into src/ for all three,
    matching the existing stub symlinks, and updated the
    typings/nss/__init__.pyi docstring. pre-commit, reuse and the
    packaging all ignore typings/.

Code fixes

  • test/test_util.py: import subprocess moves from inside the
    try to module level. Previously, if find_nss_tool raised anything
    other than FileNotFoundError, evaluating except subprocess.TimeoutExpired
    raised UnboundLocalError and masked the real error. Passing runs
    behave the same.
  • test/test_client_server.py: in client(), sock was possibly
    unbound after the connect loop. It is now bound to None before the
    loop and reset to None after each failed attempt is closed, so None
    means no address connected. When every address fails, the function
    now prints client: could not connect to "<host>" and returns None.
    This matches how it already reports an unresolvable host or a lost
    connection, and the caller's reply assertion then fails the test.
    Previously it went on to send() on the last, already closed socket.
    Successful connections break with a live socket and behave exactly
    as before. Only the failure path changes.

Validation

  • prek run --all-files is clean after each commit.
  • The aislop gate (aislop-gate.sh upstream/main) reports clean.
  • The test suite was not run locally. It needs the C extension
    built, which the documented setup (uv pip install -e ".[test]")
    can't do offline here. The local pytest hook skips without a .venv.
    This PR's build-test.yaml run builds the extension and runs the
    suite. The only test-code changes are the two above.

For review

test/run_tests (an extensionless legacy runner with no shebang) and
doc/get_api are Python, but no Python hook covers them, before or after
this PR. I left them out of scope as legacy and documentation tooling,
not source, scripts or tests that pytest runs.

This comment was marked as outdated.

The basedpyright hook ran in basic mode from pyrightconfig.json,
downgraded most findings to warnings and exited 0 on them, and could
not resolve pytest, so it reported 2 errors and 30 warnings on main.

Move the configuration into [tool.basedpyright] in pyproject.toml
and delete pyrightconfig.json and its REUSE annotation. Use standard
mode for Python 3.10 (requires-python) on all platforms, scoped to
src, test, scripts, typings and import_files.py. Drop the severity
downgrades and the .venv lookup so the hook resolves imports from
its own environment, which now installs hypothesis, mypy and pytest
at their uv.lock versions. Run it with --warnings over the whole
scope on every commit, and keep it in ci: skip because any
additional_dependencies exceed the pre-commit.ci size limit.

Fixes needed in standard mode:
- Mirror the pure-Python modules meson installs into nss/ under
  typings/nss/ as symlinks, like the existing stubs, so the test
  import of nss.deprecations resolves. Exclude all the symlinks from
  analysis, since src/ is analysed and findings would appear twice.
- Disable reportMissingModuleSource: the compiled extension cannot
  be installed in the hook environment, so every nss import finds
  its stub but no source by design.
- test_util: import subprocess at module level. The local import
  left it unbound in the except clause, so any error other than
  FileNotFoundError surfaced as UnboundLocalError.
- test_client_server: bind sock before the connect loop, reset it to
  None after each failed connect, and return None with a message
  when every address fails, as the function does for other connection
  failures, instead of carrying on with a closed socket.

Co-authored-by: Claude <noreply@anthropic.com>
Signed-off-by: Matthew Watkins <mwatkins@linuxfoundation.org>
The ruff and ruff-format hooks matched only .py files under src,
test and scripts, so the .pyi stubs for the compiled extension,
which meson installs into the nss package, were never linted or
formatted, and typings/nss/error.pyi lives outside those directories.

Match .pyi files and add typings/ to both filters. The symlinked
stubs in typings/nss/ are not passed (pre-commit skips symlinks), so
each stub is checked once, via src/. ruff check finds nothing new;
ruff format removes blank lines in error.pyi, per stub style.

Co-authored-by: Claude <noreply@anthropic.com>
Signed-off-by: Matthew Watkins <mwatkins@linuxfoundation.org>
Copilot AI balanced review requested due to automatic review settings October 2, 2026 11:25

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

The extension-backed test matrix is still running, and the full suite was not run locally.

Review effort: Balanced
Findings: None

Resolved since last review (1)

@tykeal
tykeal merged commit 6b57ba4 into lfreleng-actions:main Oct 6, 2026
115 of 131 checks passed
@ModeSevenIndustrialSolutions
ModeSevenIndustrialSolutions deleted the ci/enable-basedpyright branch October 6, 2026 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants