Skip to content

[ty] Add quick fix to remove redundant cast#25211

Open
sqqueak wants to merge 2 commits into
astral-sh:mainfrom
sqqueak:quick-fix-redundant-cast
Open

[ty] Add quick fix to remove redundant cast#25211
sqqueak wants to merge 2 commits into
astral-sh:mainfrom
sqqueak:quick-fix-redundant-cast

Conversation

@sqqueak
Copy link
Copy Markdown

@sqqueak sqqueak commented May 17, 2026

Summary

Closes astral-sh/ty#3451

This PR adds the option to remove a redundant cast via quick fix. It is implemented using safe_edit as mentioned in the issue.

Test Plan

Updated and passed tests

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label May 17, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented May 17, 2026

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 89.36%. The percentage of expected errors that received a diagnostic held steady at 85.49%. The number of fully passing files held steady at 88/134.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented May 17, 2026

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented May 17, 2026

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

Comment thread crates/ty_python_semantic/src/types/function.rs Outdated
5 | cast(int, secrets.randbelow(10))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: Remove the redundant `cast`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should add a test where cast is used as part of a larger expression and where inserting the value without parenthesizing changes precedence:

cast(int, x + y) * z

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I just want to double check- as a conservative approach, we could always keep the parens around the contents of the cast, but this would introduce redundant parentheses (cast(int, secrets.randbelow(10)) becomes (secrets.randbelow(10)) with an extra pair of parentheses). Would this approach be acceptable or would it be preferred to do some precedence checking here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be preferred to avoid the parentheses if not necessary. That's also what we do in most places in Ruff.

You should be able to use OperatorPrecedence (python ast) to get the precedence of the value expression and the parent expression. You can then decide on whether to parenthesize the value based on how the two precedences compare

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

redundant-cast quick fix

3 participants