Repository navigation
fix(sdk): make the Python SDK pass its own strict mypy configuration - #296
Conversation
`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>
|
Reviewed head No blocking findings in this revision. The Validation: Coverage: 4/4 changed files reviewed; 0 skipped (100%). |
|
lgtm Re-reviewed 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. |
Summary
pyproject.tomlsetsstrict = truewithfiles = ["src"], butmypy srcreported 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:
valid-typelist, shadowing the builtin inside the class body, soadds: list[str] | Noneresolved to the methodbuiltins.list[...]— renaming the methods would break the public APIno-any-return_request/_arequestreturnAny, returned straight out of functions declared-> dict[str, Any](and-> floatin_backoff)cast;_backoffconverts theAny-typedint.__pow__resultunused-ignore# type: ignore[return-value]comments that never matched the real code (no-any-return)union-attrsnapshots.listbuilt items viadata.get("snapshots", data.get("items", []))— typedAny | None— then iterated itThe
union-attrpair was a latent runtime bug, not just a typing complaint: an empty body (204) or an explicitnullfield would have made the comprehension iterateNoneand raiseTypeError.Test plan
mypy src→Success: no issues found in 11 source files(was 31 errors)pytest tests/unit→ 122 passed, no behavior changeruff check src→ all checks passed;ruff check src testsback to the 7 pre-existing findings that are identical on unmodifiedmain(they come from a newer ruff than the project's>=0.4floor and all sit in test files)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