Skip to content

fix(sdk): make the Python SDK pass its own strict mypy configuration - #296

Merged
loveRhythm1990 merged 1 commit into
matrixorigin:mainfrom
loveRhythm1990:fix/sdk-strict-mypy
Oct 10, 2026
Merged

loveRhythm1990 merged 1 commit into
matrixorigin:mainfrom
loveRhythm1990:fix/sdk-strict-mypy

Conversation

@loveRhythm1990

Copy link
Copy Markdown
Collaborator

Summary

pyproject.toml sets strict = true with files = ["src"], but mypy src reported 31 errors in 4 files. Reproduced exactly (mypy 2.4.0, Python 3.12): Found 31 errors in 4 files (checked 11 source files) — matching the issue's count.

Four groups:

Code Count Cause Fix
valid-type 16 Every resource class defines a method named list, shadowing the builtin inside the class body, so adds: list[str] | None resolved to the method Qualify as builtins.list[...] — renaming the methods would break the public API
no-any-return 7 _request/_arequest return Any, returned straight out of functions declared -> dict[str, Any] (and -> float in _backoff) Explicit cast; _backoff converts the Any-typed int.__pow__ result
unused-ignore 6 # type: ignore[return-value] comments that never matched the real code (no-any-return) Removed, along with the casts that make them unnecessary
union-attr 2 snapshots.list built items via data.get("snapshots", data.get("items", [])) — typed Any | None — then iterated it Build the list defensively

The union-attr pair was a latent runtime bug, not just a typing complaint: an empty body (204) or an explicit null field would have made the comprehension iterate None and raise TypeError.

Test plan

  • mypy src → Success: no issues found in 11 source files (was 31 errors)
  • pytest tests/unit → 122 passed, no behavior change
  • ruff check src → all checks passed; ruff check src tests back to the 7 pre-existing findings that are identical on unmodified main (they come from a newer ruff than the project's >=0.4 floor and all sit in test files)
  • Verified annotations still resolve at runtime (BranchesResource.apply → builtins.list[str] | None)

Not done here: the issue also suggests considering running the type check in CI. The release workflow runs unit tests but no type check, and there's no test-sdk-python.yml — adding one changes the PR gate for everyone, so I left that decision to you rather than bundling it into a bug fix. Happy to open a follow-up.

Fixes #266

🤖 Generated with Claude Code

`mypy src` reported 31 errors in 4 files despite pyproject.toml setting
strict = true, in four groups:

- valid-type (16): each resource class defines a method named `list`,
  which shadows the builtin inside the class body, so parameter
  annotations like `adds: list[str] | None` resolved to the method
  rather than the type. Qualified as `builtins.list[...]`; renaming the
  methods would break the public API.
- no-any-return (7): `_request`/`_arequest` return Any, so returning
  their result directly from a function declared `-> dict[str, Any]`
  (or `-> float` for _backoff) lost the type. Narrowed with an explicit
  cast, and _backoff now converts the Any-typed int pow result.
- unused-ignore (6): `# type: ignore[return-value]` comments that never
  matched the real error code (no-any-return). Removed along with the
  casts that make them unnecessary.
- union-attr (2): snapshots.list built its item list with
  `data.get("snapshots", data.get("items", []))`, which is Any | None,
  and then iterated it. An empty body (204) or an explicit null field
  would have raised TypeError at runtime, so this one was a latent bug
  rather than only a typing complaint; the list is now built defensively.

Fixes matrixorigin#266

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@loveRhythm1990 loveRhythm1990 changed the title fix(sdk/python): make the SDK pass its own strict mypy configuration fix(sdk): make the Python SDK pass its own strict mypy configuration Oct 10, 2026
@loveRhythm1990

Copy link
Copy Markdown
Collaborator Author

Reviewed head 2963d8fc26d41c25620d2e9ed62f2d80820e27ac.

No blocking findings in this revision.

The builtins.list annotations correctly avoid class-scope shadowing by resource methods named list. The explicit numeric conversion and casts address strict typing without changing the intended API results. The snapshot normalization consistently returns an empty list for the supported empty/null forms.

Validation: mypy src passed for all 11 source files, and the existing Python suite passed, 122/122 tests. Additional sync/async mocked checks covered HTTP 204, null snapshots/items, a bare empty list, and an empty snapshots list (10 checks total); runtime resolution of the affected branch annotations also succeeded. Local Python version: 3.14.7.

Coverage: 4/4 changed files reviewed; 0 skipped (100%).

@loveRhythm1990

Copy link
Copy Markdown
Collaborator Author

lgtm

Re-reviewed 2963d8fc26d41c25620d2e9ed62f2d80820e27ac, unchanged since the previous review. The strict-typing fixes and empty/null snapshot response handling look correct.

Coverage: 4/4 changed files reviewed; 0 skipped (100%). Previous validation on this same head passed mypy for all 11 source files, 122/122 unit tests, and the extra sync/async empty-response checks; these were not rerun in this pass.

@loveRhythm1990
loveRhythm1990 merged commit 5ec53a8 into matrixorigin:main Oct 10, 2026
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python SDK strict mypy check fails with 31 errors in 4 files

1 participant