Skip to content

ref(typing): Remove casts mypy does not need - #132050

Merged
armenzg merged 2 commits into
masterfrom
cursor/remove-sentry-casts-e73c
Nov 20, 2026
Merged

armenzg merged 2 commits into
masterfrom
cursor/remove-sentry-casts-e73c

Conversation

@armenzg

@armenzg armenzg commented Nov 20, 2026

Copy link
Copy Markdown
Member

Removes 292 of the repository's 1,607 typing.cast calls (178 files, Python only).

strict = true already reports exactly-redundant casts, so every cast here narrowed or widened a type, but mypy accepts the code without them. To find them, every typing.cast outside bin/ was removed, mypy was run, and the cast nearest each error was put back, repeating until the strict run passed. Imports used only by the removed casts were dropped.

A cast does nothing at runtime, so this changes no behavior. The casts that remain are needed by mypy. Most narrow Any, JSON, or a wider declared type. Replacing those with real types or runtime checks would change behavior or need declaration changes upstream, so that is left for focused follow-ups.

tools/migrations/drop_pending_deletions.py and src/sentry/integrations/gitlab/utils.py are left out because #132048 changes them.

Test plan

  • mypy on the whole repository: Success: no issues found in 8858 source files.
  • getsentry mypy against this branch reports no new errors. The only failures are in bin/deploy scripts that import a scripts package this machine does not have.
  • prek run --files on every changed file.
  • Imported all 134 changed src modules and 43 changed test modules with Django configured.
  • pytest was not run. Collection fails on unmodified master here too, because Django settings are not configured without devservices.

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

Open in Web Open in Cursor 

Every remaining typing.cast was removed and mypy was rerun, putting a cast
back wherever its removal produced an error, until the strict run was clean.
The 292 casts this leaves out did not change what mypy accepts. A cast does
nothing at runtime, so behavior is unchanged. Imports used only by those
casts are dropped.

Co-authored-by: Armen Zambrano G. <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

@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 prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Leftover no-op self-assignments
    • Removed the dead self-assignments queries = queries and project_id = project_id left behind after stripping typing.cast.

Create PR

Or push these changes by commenting:

@cursor push 1ca3f386d6
Preview (1ca3f386d6)
diff --git a/src/sentry/api/serializers/rest_framework/dashboard.py b/src/sentry/api/serializers/rest_framework/dashboard.py
--- a/src/sentry/api/serializers/rest_framework/dashboard.py
+++ b/src/sentry/api/serializers/rest_framework/dashboard.py
@@ -1159,7 +1159,6 @@ def validate_query_transformation_updates(self, widgets: list[dict[str, Any]]) -
         widget_errors: list[dict[str, Any]] = [{} for _ in widgets]
         for widget_index, widget in enumerate(widgets):
             queries = widget.get("queries", [])
-            queries = queries
             query_errors: list[dict[str, Any] | list[Any]] = [{} for _ in queries]
             for query_index, data in enumerate(queries):
                 if "aggregates" not in data and "transformations" not in data:

@@ -1159,7 +1159,6 @@ def validate_query_transformation_updates(self, widgets: list[dict[str, Any]]) -
         widget_errors: list[dict[str, Any]] = [{} for _ in widgets]
         for widget_index, widget in enumerate(widgets):
             queries = widget.get("queries", [])
-            queries = queries
             query_errors: list[dict[str, Any] | list[Any]] = [{} for _ in queries]
             for query_index, data in enumerate(queries):
                 if "aggregates" not in data and "transformations" not in data:

diff --git a/src/sentry/search/events/datasets/function_aliases.py b/src/sentry/search/events/datasets/function_aliases.py
--- a/src/sentry/search/events/datasets/function_aliases.py
+++ b/src/sentry/search/events/datasets/function_aliases.py
@@ -59,7 +59,6 @@ def resolve_project_threshold_config(
     project_threshold_config_keys = []
     project_threshold_config_values = []
     for project_id, metric in project_threshold_configs:
-        project_id = project_id
         metric = TRANSACTION_METRICS[cast(int, metric)]
         if metric == constants.DEFAULT_PROJECT_THRESHOLD_METRIC:
             # small optimization, if the configuration is equal to the default,

@@ -59,7 +59,6 @@ def resolve_project_threshold_config(
     project_threshold_config_keys = []
     project_threshold_config_values = []
     for project_id, metric in project_threshold_configs:
-        project_id = project_id
         metric = TRANSACTION_METRICS[cast(int, metric)]
         if metric == constants.DEFAULT_PROJECT_THRESHOLD_METRIC:
             # small optimization, if the configuration is equal to the default,

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 12ca8df. Configure here.

Removing cast() from x = cast(T, x) left a no-op x = x in two places.

Co-authored-by: Armen Zambrano G. <armenzg@users.noreply.github.com>
@armenzg
armenzg marked this pull request as ready for review November 20, 2026 19:58
@armenzg
armenzg requested review from a team as code owners November 20, 2026 19:58
@armenzg armenzg self-assigned this Nov 20, 2026
@armenzg
armenzg requested review from a team as code owners November 20, 2026 19:58
@armenzg
armenzg requested review from a team as code owners November 20, 2026 19:58
@armenzg
armenzg requested review from alwx and antonis and removed request for a team November 20, 2026 19:58
@armenzg
armenzg merged commit c592b97 into master Nov 20, 2026
98 checks passed
@armenzg
armenzg deleted the cursor/remove-sentry-casts-e73c branch November 20, 2026 20:05
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.

4 participants