Skip to content

Commit 6178822

Browse files
authored
[ty] Attach subdiagnostics to unresolved-import errors for relative imports as well as absolute imports (#21554)
1 parent 6b7adb0 commit 6178822

4 files changed

+37
-24
lines changed

crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_di…_-_Using_`from`_with_an…_(6cff507dc64a1bff).snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ error[unresolved-import]: Cannot resolve imported module `.does_not_exist.foo.ba
2828
2 |
2929
3 | stat = add(10, 15)
3030
|
31+
info: Searched in the following paths during module resolution:
32+
info: 1. /src (first-party code)
33+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
34+
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
3135
info: rule `unresolved-import` is enabled by default
3236
3337
```

crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_di…_-_Using_`from`_with_an…_(9da56616d6332a83).snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ error[unresolved-import]: Cannot resolve imported module `.does_not_exist`
2828
2 |
2929
3 | stat = add(10, 15)
3030
|
31+
info: Searched in the following paths during module resolution:
32+
info: 1. /src (first-party code)
33+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
34+
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
3135
info: rule `unresolved-import` is enabled by default
3236
3337
```

crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_di…_-_Using_`from`_with_to…_(4b8ba6ee48180cdd).snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ error[unresolved-import]: Cannot resolve imported module `....foo`
4040
2 |
4141
3 | stat = add(10, 15)
4242
|
43+
info: Searched in the following paths during module resolution:
44+
info: 1. /src (first-party code)
45+
info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty)
46+
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
4347
info: rule `unresolved-import` is enabled by default
4448
4549
```

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5803,6 +5803,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
58035803
".".repeat(level as usize),
58045804
module.unwrap_or_default()
58055805
));
5806+
58065807
if level == 0 {
58075808
if let Some(module_name) = module.and_then(ModuleName::new) {
58085809
let program = Program::get(self.db());
@@ -5831,39 +5832,39 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
58315832
}
58325833
}
58335834
}
5835+
}
58345836

5835-
// Add search paths information to the diagnostic
5836-
// Use the same search paths function that is used in actual module resolution
5837-
let verbose = self.db().verbose();
5838-
let search_paths = search_paths(self.db(), ModuleResolveMode::StubsAllowed);
5837+
// Add search paths information to the diagnostic
5838+
// Use the same search paths function that is used in actual module resolution
5839+
let verbose = self.db().verbose();
5840+
let search_paths = search_paths(self.db(), ModuleResolveMode::StubsAllowed);
58395841

5840-
diagnostic.info(format_args!(
5841-
"Searched in the following paths during module resolution:"
5842-
));
5842+
diagnostic.info(format_args!(
5843+
"Searched in the following paths during module resolution:"
5844+
));
58435845

5844-
let mut search_paths = search_paths.enumerate();
5846+
let mut search_paths = search_paths.enumerate();
58455847

5846-
while let Some((index, path)) = search_paths.next() {
5847-
if index > 4 && !verbose {
5848-
let more = search_paths.count() + 1;
5849-
diagnostic.info(format_args!(
5850-
" ... and {more} more paths. Run with `-v` to see all paths."
5851-
));
5852-
break;
5853-
}
5848+
while let Some((index, path)) = search_paths.next() {
5849+
if index > 4 && !verbose {
5850+
let more = search_paths.count() + 1;
58545851
diagnostic.info(format_args!(
5855-
" {}. {} ({})",
5856-
index + 1,
5857-
path,
5858-
path.describe_kind()
5852+
" ... and {more} more paths. Run with `-v` to see all paths."
58595853
));
5854+
break;
58605855
}
5856+
diagnostic.info(format_args!(
5857+
" {}. {} ({})",
5858+
index + 1,
5859+
path,
5860+
path.describe_kind()
5861+
));
5862+
}
58615863

5862-
diagnostic.info(
5863-
"make sure your Python environment is properly configured: \
5864+
diagnostic.info(
5865+
"make sure your Python environment is properly configured: \
58645866
https://docs.astral.sh/ty/modules/#python-environment",
5865-
);
5866-
}
5867+
);
58675868
}
58685869

58695870
fn infer_import_definition(

0 commit comments

Comments
 (0)