Skip to content

ref: Lint PUBLIC endpoints for typed return annotations#117356

Merged
azulus merged 5 commits into
masterfrom
jeremy/enforce-public-typed-responses
Jun 11, 2026
Merged

ref: Lint PUBLIC endpoints for typed return annotations#117356
azulus merged 5 commits into
masterfrom
jeremy/enforce-public-typed-responses

Conversation

@azulus

@azulus azulus commented Jun 10, 2026

Copy link
Copy Markdown
Member

Adds a check to the apidocs response-annotation linter: any HTTP method declared ApiPublishStatus.PUBLIC in publish_status must annotate its return with Response[T], a union of Response[T_i] arms (optionally mixed with non-Response Django response types), or a concrete non-DRF type. Bare -> Response and missing annotations are errors.

PUBLIC endpoints are documented in OpenAPI and consumed by SDK generators; an untyped body lets the runtime drift from the declared schema. The diagnostic includes a copy-able fix template and a pointer to sentry.apidocs.response_types for standard error shapes.

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Jun 10, 2026
@azulus azulus marked this pull request as ready for review June 10, 2026 19:32
@azulus azulus requested a review from a team as a code owner June 10, 2026 19:32
Adds a check to the apidocs response-annotation linter: any HTTP method
declared `ApiPublishStatus.PUBLIC` in `publish_status` must annotate its
return with `Response[T]`, a union of `Response[T_i]` arms (optionally
mixed with non-`Response` Django response types), or a concrete non-DRF
type. Bare `-> Response` and missing annotations are errors.

PUBLIC endpoints are documented in OpenAPI and consumed by SDK
generators; an untyped body lets the runtime drift from the declared
schema. The diagnostic includes a copy-able fix template and a pointer
to `sentry.apidocs.response_types` for standard error shapes.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@azulus azulus changed the title ref: Enforce typed return annotations on PUBLIC endpoints ref: Lint PUBLIC endpoints for typed return annotations Jun 10, 2026
@azulus azulus force-pushed the jeremy/enforce-public-typed-responses branch from 2f7e645 to 970358e Compare June 10, 2026 19:34

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 970358e. Configure here.

Comment thread src/sentry/apidocs/_check_response_annotation_matches_schema.py
Locks down that `from __future__ import annotations` doesn't change
what the AST-based bare-`Response` check sees — the future import is
a runtime-evaluation directive, not a parse-time one, so `ast.parse()`
still produces a `Name` node for `Response` regardless.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Comment thread src/sentry/apidocs/_check_response_annotation_matches_schema.py
Comment thread src/sentry/apidocs/_check_response_annotation_matches_schema.py
Extends the PUBLIC-untyped check to handle two additional AST shapes:

- `publish_status: dict[str, ApiPublishStatus] = {...}` (annotated
  assignment) is now read alongside the plain-assignment form.
- `Union[Response, Response[T]]` (typing.Union syntax) is now walked
  the same way `X | Y` BinOps already were; a bare-`Response` arm in
  either form fires the diagnostic.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Comment thread src/sentry/apidocs/_check_response_annotation_matches_schema.py Outdated
azulus and others added 2 commits June 10, 2026 14:39
Adds `Optional` to the union-subscript walker so `-> Optional[Response]`
is recognized as a bare-`Response` arm. `Optional[X]` is `Union[X, None]`
semantically, so unwrapping the `X` arm is sufficient (`None` can't
introduce a bare `Response`).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@azulus azulus merged commit 9cbb88f into master Jun 11, 2026
64 checks passed
@azulus azulus deleted the jeremy/enforce-public-typed-responses branch June 11, 2026 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants