[ty] Add quick fix to remove redundant cast#25211
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe 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. |
Memory usage reportMemory usage unchanged ✅ |
|
| 5 | cast(int, secrets.randbelow(10)) | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: Remove the redundant `cast` |
There was a problem hiding this comment.
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) * zThere was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
Summary
Closes astral-sh/ty#3451
This PR adds the option to remove a redundant cast via quick fix. It is implemented using
safe_editas mentioned in the issue.Test Plan
Updated and passed tests