Skip to content

ref(typing): Re-type Any-valued locals that lost their casts - #132056

Draft
sentry-junior[bot] wants to merge 4 commits into
masterfrom
armenzg/restore-local-casts
Draft

sentry-junior[bot] wants to merge 4 commits into
masterfrom
armenzg/restore-local-casts

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #132050.

That PR removed every typing.cast that mypy accepted without. For return cast(...) that is harmless: the function's declared return type still types the value for callers. For a local assigned from an Any value it is not. x = cast(T, any_value) becomes x = any_value, x turns into Any, and mypy stops checking every later use of it in that scope. Nothing reports the loss, because Any is compatible with everything and mypy never errors on it.

The clearest case is DRF validated_data, which the stubs type as Any. Once it is typed as AgentApprovalRequestData, a typo such as validated_data["sessionIdx"] is a typeddict-item error. Without that, it passes. #132050 also left AgentApprovalRequestData, AgentTokenRequestData, the three ServiceAccount*RequestData TypedDicts, BitbucketServerReposResponse and _ValuesResponse defined but unused. This PR makes them used again.

This re-types 45 of the 96 assignments #132050 changed, without adding any cast. An assignment was picked only when mypy's revealed type of the right-hand side is Any, or a mapping or list with Any values, and the old cast target was more precise.

  • Most use a variable annotation (x: T = any_value). On an Any value the annotation gives the same checking as a cast. Unlike a cast, it errors if the source later gets a real type that does not match.
  • Bitbucket Server types the source instead. get_repos, search_repositories and a new _get_values_page helper declare the response TypedDicts as their return types. They call self.get (which returns Any) instead of get_mapping, which only wraps the same value in a cast. The repo payload TypedDicts move to client.py, next to the methods that return them.
  • Slack dashboard unfurls annotate the four fields read from link.args. GitLab passes comparison["commits"] straight to _format_commits, whose parameter is typed. Explore export returns the upper-cased sampling mode, which the method's return type types.
  • The coding agent handoff annotates the stored extras as SeerRunCodingAgentHandoffExtras and builds the update from that value, so mypy checks the key it adds.
  • Eventstream copies the occurrence TypedDict into a plain dict before deleting evidence_data and evidence_display. This adds one shallow copy of a dict that to_dict() has just built.

These stay removed:

  • casts whose source already had a real type, such as CombinedExpression → int or Provider → a subclass;
  • casts on exception constructors, and in tests where the value is only compared or is already a TypedDict;
  • casts in four files where re-typing would need a second cast to undo the first: accept_project_transfer.py, the sessions and release-health paginators, and test_notebook.py.

The longer-term fix is to type more sources the way Bitbucket Server now does, such as the other integration clients and a typed accessor for serializer validated_data.

via @armenzg.

--

View Session [Sentry]

#132050 removed every cast that mypy accepted without.
For a local or attribute assigned from an `Any` value, that turned the
variable into `Any`, so mypy stopped checking how it is used later, for
example the keys read from a DRF `validated_data` TypedDict. It also left
TypedDicts such as `AgentApprovalRequestData` defined but unused.

This restores the casts on assignments whose source is `Any`, or a mapping
or list with `Any` values, and whose cast target is more precise.
Removed casts on return values and arguments stay removed, as do pairs
where the restored cast would need another cast to undo it.

Co-Authored-By: Armen Zambrano Gasparnian <44410+armenzg@users.noreply.github.com>
@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 20, 2026
An annotation types an Any value the same way a cast does, without a cast.
Unlike a cast, it becomes an error if the source later gets a type that
does not match. Casts stay where the name is already bound in scope or
where the source has a real type, such as a dict[str, Any] read as a
TypedDict.

Co-Authored-By: Armen Zambrano Gasparnian <44410+armenzg@users.noreply.github.com>
@sentry-junior sentry-junior Bot changed the title ref(typing): Restore casts that type Any-valued locals ref(typing): Re-type Any-valued locals that lost their casts Nov 20, 2026
sentry-junior Bot and others added 2 commits November 20, 2026 21:13
Co-Authored-By: Armen Zambrano Gasparnian <44410+armenzg@users.noreply.github.com>
- Bitbucket Server: the client methods declare the response TypedDicts as
  their return types, so callers get typed data without casting. The repo
  payload TypedDicts move next to the client that returns them.
- GitLab: pass the compared commits straight to _format_commits, whose
  parameter is typed.
- Slack dashboards unfurl: annotate the fields read from the unfurl args.
- Coding agent handoff: annotate the stored extras and build the update
  from the typed value, so mypy checks the keys.
- Explore export: return the upper-cased sampling mode, typed by the
  method's return type.
- Eventstream: copy the occurrence TypedDict into a plain dict before
  deleting keys from it.
- test_snuba: the result is only compared, so it needs no type.

Co-Authored-By: Armen Zambrano Gasparnian <44410+armenzg@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

1 participant