Codegen audit: tighten P1/P3/P4/P5 across SDK boundaries#4
Merged
Conversation
…ness hook Phase 0/1 of the cross-repo codegen audit. Aligns with the policy doc landing in monorepo at `cowork/design/040-codegen-policies.md`: - P1 (response extras=forbid) + P2 (request extras=forbid): inject `model_config = ConfigDict(extra='forbid')` into every generated model via a new `scripts/inject_strict_config.py` post-processor wired through `scripts/typegen.sh`. Skips RootModels (Pydantic rejects `extra` there) and tolerates StrEnum member-name shadowing. - P4 (three-class error taxonomy): split `DevhelmError` into `DevhelmValidationError` (local schema mismatches), `DevhelmApiError` (HTTP 4xx/5xx), and `DevhelmTransportError` (network/TLS/timeout) — same names that sdk-js, cli, mcp-server and the terraform-provider expose. `_http.py` now wraps the underlying `httpx` transport errors. No backwards-compat aliases (no customers). - P5 (zero casts outside generated): drop the mypy `_generated.py` exclusion and fix the resulting type errors by tightening the injector and the StrEnum-collision allowlist. Adds `scripts/regen-from.sh` so the upcoming spec-evolution harness in monorepo can regenerate this SDK from a mutated spec without needing to know the internal layout. Tests refreshed to assert against the new error hierarchy. Made-with: Cursor
Phase 2 cleanup: remove residual `cast()` calls in `_pagination.py` and `# type: ignore` comments in `_http.py` so the SDK has zero type-system escapes outside generated files (P5). Made-with: Cursor
* P1 (response strict): add `parse_strict_envelope` helper to `_validation.py` so endpoints with nullable `data` (today only GET /api/v1/deploy/lock) reject unknown top-level envelope keys without forcing the resource layer to declare a `BaseModel` subclass (which would trip the new `disallow_any_explicit`). * P3 (validation surfaces): narrow broad `except Exception` in `_pagination.py` to `ValidationError`, and pass the inner errors + cause into `DevhelmValidationError` so callers can route on field-level failures and stack traces survive. * P5 (no-Any in user-facing layer): enable `disallow_any_explicit = true` globally with explicit overrides for the JSON-boundary modules (`_errors`, `_http`, `_pagination`, `_validation`, `_generated`). Renamed the typing test to `test_resource_layer_is_any_free` and rewrote it against the new contract. * Lock in `RootModel` strictness with a focused suite covering both the scalar wrapper (`SubscribedEvent`) and the discriminated union (`CheckTypeDetailsDto`); these were silently skipped by `inject_strict_config` and thus never directly verified. * Lock in `parse_strict_envelope` semantics (extras forbidden, optional null, structured Pydantic error propagation) in a new `tests/test_validation_helpers.py`. Made-with: Cursor
CI's `ruff format --check` was failing on 7 files that hadn't been reformatted since the last formatter rule update. Applied the formatter. Also fixes a latent brittleness: `test_typing.py` allow-listed the one documented `# type: ignore` by full `path:lineno: line` triple, so any unrelated reflow above the suppression site (like this very format pass shifting line 126 → 122 in `_validation.py`) silently turns the test red on changes that have nothing to do with type ignores. The allow-list is now keyed on `(relative_path, stripped_line)` — adding/removing/moving suppressions still reds the test, but unrelated formatting churn doesn't. The error message keeps the full `file:line:` offender list for fast diagnosis. Verified: ruff format --check, ruff check, mypy, and `pytest` (700 passed) all green locally. Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of the cross-repo codegen audit (devhelmhq/mono#246). Aligns the Python SDK with policies P1 / P2 / P3 / P4 / P5 from
cowork/design/040-codegen-policies.md.Summary
Codegen pipeline (existing)
feat(codegen)+chore(p5)from earlier in the branch enabled strict policies on the generated Pydantic models, eliminated remainingcast()/# type: ignorein HTTP + pagination, and addedscripts/regen-from.shfor the spec-evolution hook.This commit (
77555a4) — final tighteningparse_strict_envelopehelper in_validation.pyenforces{data: T}envelopes with extras forbidden.DeployLock.current()migrated to it (replaces a one-off_CurrentLockEnvelopeBaseModel).RootModelinner variant enforcesextra='forbid', closing the gap where discriminated unions could silently accept unknown fields._pagination.pyswitched fromexcept Exceptiontoexcept ValidationErrorso transport / programmer errors surface intact instead of being miscategorized as validation failures.disallow_any_explicitglobally. Enabled inpyproject.toml(mypy strict mode). JSON-boundary modules carry minimal targeted overrides; the resource modules are nowAny-free, asserted bytests/test_typing.py.Tests
tests/test_validation_helpers.pyforparse_strict_envelope(data-present / data-null / extras / nested errors).tests/test_schemas.pygainedTestRootModelStrictnessclass.tests/test_typing.pyrefactored to assert resource modules are explicit-Any-free.Spec-evolution coverage (in monorepo PR)
tests/surfaces/evolution/surfaces/test_sdk_python.pyin mono#246 already pins P1 / P3 / P4 against this branch (response extras, oneOf variant drops, taxonomy class hierarchy + transport-error wrapping, etc.).Test plan
make typecheck— mypy strict +disallow_any_explicitcleanmake test— full unit suite passingmake lint+make format— cleantests/surfaces/sdk-python/passes against this branchtests/surfaces/evolution/surfaces/test_sdk_python.py— full pass + documented xfails preservedMade with Cursor