-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add Checker::import_from_typing
#17340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
795553c
684ae0d
f4c0317
1a453ea
3a8aadc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,7 @@ use ruff_python_parser::semantic_errors::{ | |
| }; | ||
| use rustc_hash::{FxHashMap, FxHashSet}; | ||
|
|
||
| use ruff_diagnostics::{Diagnostic, IsolationLevel}; | ||
| use ruff_diagnostics::{Diagnostic, Edit, IsolationLevel}; | ||
| use ruff_notebook::{CellOffsets, NotebookIndex}; | ||
| use ruff_python_ast::helpers::{collect_import_from_member, is_docstring_stmt, to_module_path}; | ||
| use ruff_python_ast::identifier::Identifier; | ||
|
|
@@ -62,7 +62,7 @@ use ruff_text_size::{Ranged, TextRange, TextSize}; | |
|
|
||
| use crate::checkers::ast::annotation::AnnotationContext; | ||
| use crate::docstrings::extraction::ExtractionTarget; | ||
| use crate::importer::Importer; | ||
| use crate::importer::{ImportRequest, Importer, ResolutionError}; | ||
| use crate::noqa::NoqaMapping; | ||
| use crate::package::PackageRoot; | ||
| use crate::registry::Rule; | ||
|
|
@@ -530,6 +530,28 @@ impl<'a> Checker<'a> { | |
| f(&mut checker, self); | ||
| self.semantic_checker = checker; | ||
| } | ||
|
|
||
| /// Attempt to create an [`Edit`] that imports `member`. | ||
| /// | ||
| /// On Python <`version_added_to_typing`, `member` is imported from `typing_extensions`, while | ||
| /// on Python >=`version_added_to_typing`, it is imported from `typing`. | ||
| /// | ||
| /// See [`Importer::get_or_import_symbol`] for more details on the returned values. | ||
| pub(crate) fn import_from_typing( | ||
| &self, | ||
| member: &str, | ||
| position: TextSize, | ||
| version_added_to_typing: PythonVersion, | ||
| ) -> Result<(Edit, String), ResolutionError> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not really something to be addressed in this PR, but FWIW I don't love the return type of It also seems like it's too easy to call
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, I thought that return type looked a bit suspicious (especially when I considered adding a third element to the tuple 😆) I'll keep an eye on this and see if I can come up with anything. |
||
| let source_module = if self.target_version() >= version_added_to_typing { | ||
| "typing" | ||
| } else { | ||
| "typing_extensions" | ||
| }; | ||
| let request = ImportRequest::import_from(source_module, member); | ||
| self.importer() | ||
| .get_or_import_symbol(&request, position, self.semantic()) | ||
| } | ||
| } | ||
|
|
||
| impl SemanticSyntaxContext for Checker<'_> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a method on the
Importerinstead? This is, I think, the main interface to generate import edits:ruff/crates/ruff_linter/src/checkers/ast/mod.rs
Lines 434 to 437 in 172af7b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be, but we get the Python version from
Checkerand also theSemanticModelforImporter::get_or_import_symbol, so we'd have to pass a couple more arguments if it were anImportermethod.I also think the
Checkerwill be more likely to contain any additional information we need to fix #9761, such as theLinterSettings, if we add a configuration option.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Checkercreates theImporterso we can just update theImporterto also contain a reference to theSemanticModelbut I trust your judgement about the issue that needs to be fixed so we can leave it as is for now and do a follow-up change if required.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh interesting, I see. I think I'll lean toward leaving it like this for now, but I'll keep this in mind if we don't end up using any more code from the
Checker. It would logically make more sense on theImporter, as you said.