Skip to content

[red-knot] Add support for relative imports#12910

Merged
AlexWaygood merged 5 commits intomainfrom
alex/relative-imports
Aug 16, 2024
Merged

[red-knot] Add support for relative imports#12910
AlexWaygood merged 5 commits intomainfrom
alex/relative-imports

Conversation

@AlexWaygood
Copy link
Copy Markdown
Member

Summary

This PR adds support for relative import statements, e.g. from .foo import bar, from . import bar, or from ...foo import bar.

Test Plan

Several tests added to infer.rs.

Co-authored-by: Carl Meyer carl@astral.sh

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Aug 15, 2024
@AlexWaygood
Copy link
Copy Markdown
Member Author

(I'm expecting this to have a significant impact on the tomllib benchmark, since lots of relative imports in tomllib that we previously couldn't understand at all will now be understood)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Aug 15, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@carljm
Copy link
Copy Markdown
Contributor

carljm commented Aug 16, 2024

I'll let @MichaReiser review this, since I helped write it :)

You'll have to update the expected number of errors in the benchmark before we'll get to see benchmark results.

And it looks like Clippy has a complaint.

@carljm carljm removed their request for review August 16, 2024 05:00
Copy link
Copy Markdown
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

This looks great. Again, much simpler than I expected and nice that the file_to_module provides everything that we need.

@AlexWaygood AlexWaygood force-pushed the alex/relative-imports branch from 6a1a69a to 54ab1f0 Compare August 16, 2024 09:30
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Aug 16, 2024

CodSpeed Performance Report

Merging #12910 will degrade performances by 29.01%

Comparing alex/relative-imports (79c1853) with main (9b73532)

Summary

⚡ 1 improvements
❌ 2 (👁 2) regressions
✅ 29 untouched benchmarks

Benchmarks breakdown

Benchmark main alex/relative-imports Change
linter/all-rules[numpy/globals.py] 779.1 µs 727.8 µs +7.05%
👁 red_knot_check_file[cold] 43.4 ms 51 ms -14.9%
👁 red_knot_check_file[incremental] 1.9 ms 2.7 ms -29.01%

@AlexWaygood AlexWaygood force-pushed the alex/relative-imports branch from fc9b5eb to 5138cc3 Compare August 16, 2024 09:45
let result = db.check_file(*parser).unwrap();

assert_eq!(result.len(), 111);
assert_eq!(result.len(), 29);
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.

Woohoo nice

Comment on lines +875 to +897
fn relative_module_name(&self, tail: Option<&str>, level: NonZeroU32) -> Option<ModuleName> {
let Some(module) = file_to_module(self.db, self.file) else {
tracing::debug!("Failed to resolve file {:?} to a module", self.file);
return None;
};
let mut level = level.get();
if module.kind().is_package() {
level -= 1;
}
let mut module_name = module.name().to_owned();
for _ in 0..level {
module_name = module_name.parent()?;
}
if let Some(tail) = tail {
if let Some(valid_tail) = ModuleName::new(tail) {
module_name.extend(&valid_tail);
} else {
tracing::debug!("Failed to resolve relative import due to invalid syntax");
return None;
}
}
Some(module_name)
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Are these tracing calls the kind of thing you were thinking of @MichaReiser? I also added a tracing::trace!() call to infer_import_from_definition.

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.

LGTM. Thanks

@AlexWaygood AlexWaygood merged commit a87b27c into main Aug 16, 2024
@AlexWaygood AlexWaygood deleted the alex/relative-imports branch August 16, 2024 11:35
/// module_name.extend(&ModuleName::new_static("baz.eggs.ham").unwrap());
/// assert_eq!(&module_name, "foo.bar.baz.eggs.ham");
/// ```
pub fn extend(&mut self, other: &ModuleName) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice, I also had a thought late last night that this is the name we should have used for this method, so glad you all settled on the same thing 😆

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.

3 participants