[flake8-pyi] Fix incorrect behaviour of custom-typevar-return-type preview-mode autofix if typing was already imported (PYI019)#15853
Merged
AlexWaygood merged 2 commits intomainfrom Jan 31, 2025
Conversation
flake8-pyi] Fix incorrect behaviour of custom-typevar-return-type autofix if typing was already imported (PYI019)flake8-pyi] Fix incorrect behaviour of custom-typevar-return-type preview-mode autofix if typing was already imported (PYI019)
AlexWaygood
commented
Jan 31, 2025
Member
Author
There was a problem hiding this comment.
This is the snapshot for if preview mode is not enabled (the autofix is only availabe with --preview currently)
Member
Author
There was a problem hiding this comment.
this is the snapshot for if --preview mode is enabled
AlexWaygood
commented
Jan 31, 2025
Comment on lines
-273
to
-278
| // See `replace_custom_typevar_with_self`'s doc comment | ||
| if in_stub { | ||
| if let Some(fix) = replace_custom_typevar_with_self(checker, function_def, returns) { | ||
| diagnostic.set_fix(fix); | ||
| } | ||
| } |
Member
Author
There was a problem hiding this comment.
I moved the check for whether or not it's a stub file inside the body of replace_custom_typevar_with_self(), since that function already returns an Option. This means we don't have to have comments in two places :-)
Contributor
|
5dee036 to
ef3e6a2
Compare
ef3e6a2 to
540b085
Compare
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.
Summary
Importer::get_or_import_symbolcan fail, and if it does it returns a usefulResolutionErrorthat tells you why it failed. The message from this error is printed to the terminal if you run Ruff with enough-varguments on the command line. However, we're currently throwing away this useful information in the implementation of this rule by calling.ok()on the value returned byget_or_import_symbol. There's no reason to do this, as it doesn't really simplify the code; we already have aDiagnostic::try_set_optional_fix()method that's designed exactly for this situation.The fix also makes an incorrect edit if
typingis already imported in the scope being edited -- here, it makes a fix that would introduce aNameError(becauseSelfis not imported anywhere in the file):This is because we throw away the second element of the tuple returned by
get_or_import_symbol_here:ruff/crates/ruff_linter/src/rules/flake8_pyi/rules/custom_type_var_return_type.rs
Lines 335 to 337 in f1418be
In this case, the second element would be the string
"typing.Self", which is what we should use in theEdit. But instead we just pass in the string"Self", assuming the at the import edit would have addedfrom typing import Selfat the top of the module (it only does that if bothtypingnorSelfare currently not imported):ruff/crates/ruff_linter/src/rules/flake8_pyi/rules/custom_type_var_return_type.rs
Lines 354 to 356 in f1418be
Test Plan
I added some fixtures that exhibit incorrect behaviour on
main.