feat(api): tighten Response on 3 endpoints + add as_validation_errors helper#116736
Merged
Conversation
afc8b40 to
68a7a42
Compare
68a7a42 to
13b228c
Compare
… helper Continues the Response[T] rollout (#116717 was round 1). Three more endpoints tightened via source typing — no `cast()` calls. Two patterns demonstrated: 1. **Typed local variable** for locally-built dicts: - `project_filters.py`: `results: list[ProjectFilterResponse] = []` - `project_replay_jobs_delete.py` POST success path: `response: ReplayDeletionJobDetailResponse = {"data": response_data}` 2. **Validation-error union arm** for DRF `serializer.errors` paths. Added a shared `ValidationErrorResponse` type alias and `as_validation_errors(serializer)` helper to `apidocs/response_types.py`, alongside the existing `DetailResponse`. The helper projects DRF's `ReturnDict[Any, Any]` into a plain `dict[str, list[str]]` so a `Response[ValidationErrorResponse]` union arm is structurally satisfied without `cast()`: def post(...) -> Response[FooResponse] | Response[ValidationErrorResponse]: if not serializer.is_valid(): return Response(as_validation_errors(serializer), status=400) Applied to `organization_trace_item_attributes.py` and `project_replay_jobs_delete.py` POST. The `project_replay_jobs_delete.py` GET endpoints are tightened to `Response[ReplayDeletionJobListResponse]` and `Response[ReplayDeletionJobDetailResponse]` — clean tightenings. Verification: - mypy: 0 errors across all touched files - prek: clean - `sentry django spectacular`: byte-identical OpenAPI vs master Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
13b228c to
f2646c6
Compare
gricha
approved these changes
Jun 2, 2026
Contributor
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit f2646c6. Configure here.
…dation_errors
The `{k: list(v) for k, v in serializer.errors.items()}` transform
collapsed nested error dicts (from nested serializers or
`validate()`-raised non-field errors) to just the key list, losing the
actual error messages. `ProjectReplayDeletionJobsIndexTest::test_post_validation_errors`
caught this — `ReplayDeletionJobCreateSerializer` wraps an inner
`ReplayDeletionJobCreateDataSerializer`, so its errors are
`{"data": {"non_field_errors": [...]}}` and `list({"non_field_errors": [...]})`
returned `["non_field_errors"]` instead of preserving the message.
Fix: pass DRF's structure through verbatim — `dict(serializer.errors)`
just narrows the static type without traversing. Widen the alias to
`dict[str, Any]` to be honest about the runtime shape (flat lists,
nested dicts, non-field-error dicts all occur in real serializers).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

Tighten three more endpoint return annotations from
-> Responseto-> Response[T], and add a sharedas_validation_errors()helper inapidocs/response_types.pyfor the recurringResponse(serializer.errors, status=400)pattern.Round 2 of the rollout that started with #116717. Continues the principle from that PR: no
cast()calls — every tightening is unblocked by typing the source (typed local variable, explicit serializer instance, or a new union arm for validation errors). The helper consolidates a 1-liner that was about to repeat across this PR and future ones.ValidationErrorResponseis intentionallydict[str, Any]andas_validation_errorsdoesdict(serializer.errors)(not a value-traversal) — DRF emits flat, nested, and non-field-error shapes interchangeably, and traversing the value collapses nested dicts to keys. Caught bytest_post_validation_errorson the nestedReplayDeletionJobCreateSerializer.