Repository navigation
ref(typing): Re-type Any-valued locals that lost their casts - #132056
Draft
sentry-junior[bot] wants to merge 4 commits into
Draft
sentry-junior[bot] wants to merge 4 commits into
sentry-junior[bot] wants to merge 4 commits into
Conversation
#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>
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>
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
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.
Follow-up to #132050.
That PR removed every
typing.castthat mypy accepted without. Forreturn cast(...)that is harmless: the function's declared return type still types the value for callers. For a local assigned from anAnyvalue it is not.x = cast(T, any_value)becomesx = any_value,xturns intoAny, and mypy stops checking every later use of it in that scope. Nothing reports the loss, becauseAnyis compatible with everything and mypy never errors on it.The clearest case is DRF
validated_data, which the stubs type asAny. Once it is typed asAgentApprovalRequestData, a typo such asvalidated_data["sessionIdx"]is atypeddict-itemerror. Without that, it passes. #132050 also leftAgentApprovalRequestData,AgentTokenRequestData, the threeServiceAccount*RequestDataTypedDicts,BitbucketServerReposResponseand_ValuesResponsedefined 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 isAny, or a mapping or list withAnyvalues, and the old cast target was more precise.x: T = any_value). On anAnyvalue 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.get_repos,search_repositoriesand a new_get_values_pagehelper declare the response TypedDicts as their return types. They callself.get(which returnsAny) instead ofget_mapping, which only wraps the same value in a cast. The repo payload TypedDicts move toclient.py, next to the methods that return them.link.args. GitLab passescomparison["commits"]straight to_format_commits, whose parameter is typed. Explore export returns the upper-cased sampling mode, which the method's return type types.SeerRunCodingAgentHandoffExtrasand builds the update from that value, so mypy checks the key it adds.dictbefore deletingevidence_dataandevidence_display. This adds one shallow copy of a dict thatto_dict()has just built.These stay removed:
CombinedExpression→intorProvider→ a subclass;accept_project_transfer.py, the sessions and release-health paginators, andtest_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]