Repository navigation
[ty] Share call-unwrapping fix generation with Ruff - #28145
Conversation
Typing conformance resultsNo changes detected βCurrent numbersThe percentage of diagnostics emitted that were expected errors held steady at 97.82%. The percentage of expected errors that received a diagnostic held steady at 95.48%. The number of fully passing files held steady at 126/145. |
Memory usage reportMemory usage unchanged β |
|
|
|
I'm not sure the I wonder if we should create a new crate for these kinds of helpers that ruff and ty can both depend on, continuing the precedent that @BurntSushi started when he added the |
|
Good call. What about |
AlexWaygood
left a comment
There was a problem hiding this comment.
I haven't reviewed the code in depth for correctness, but the idea seems fine, though I still feel like in general this is a surprisingly complex method and it's okay to simply bail and not provide a fix in certain weird edge cases. (Do we really need to provide fixes in f-strings?)
It looks like this new method could probably be used to fix #28102 as well.
I'd appreciate sign-off from @ntBre as well!
Thanks for the review! My personal feeling is that this kind of consistency even in edge cases that seem "hard" is delightful as a user when you run into it, and this PR is well within the budget of how much complexity it's worth paying for that. Removing f-string support from this PR would add just as much or more code checking for that case and avoiding suggesting a fix as is required to support it (one |
In #28092 I fixed a bug where ty's redundant-cast removal fix could create broken code if the original
cast()call was multi-line. Doug commented that this could perhaps be a general-purpose utility -- and it turned out there were two existing similar Ruff rules with pre-existing multi-line support that could benefit from such a shared utility.This PR introduces a shared utility in
ruff_python_editsfor removing a function call in favor of one of its arguments. This is the common shape between redundant-cast removal, redundantint()removal, and redundantround()removal.The new utility is more robust than the previous code for any of the three cases. Previously removing redundant
int,round, andtyping.castcalls could discard parentheses needed for line continuation or for token separation. For example,int(1).to_bytes()could become1.to_bytes(), which is invalid.The shared helper preserves optional argument parentheses and the comments inside them, adds grouping where required, and avoids redundant parentheses when the surrounding syntax already allows the expression. Ruff and ty retain their own diagnostic eligibility and fix-safety decisions.
Test plan
ruff_python_editsfor conditional callees, comprehension iterables, and dictionary expressions inside f-strings.