[ty] Add note to unresolved-import hinting to users to configure their Python environment (#18207)

Closes https://github.com/astral-sh/ty/issues/453.

## Summary

Add an additional info diagnostic to `unresolved-import` check to hint
to users that they should make sure their Python environment is properly
configured for ty, linking them to the corresponding doc. This
diagnostic is only shown when an import is not relative, e.g., `import
maturin` not `import .maturin`.

## Test Plan

Updated snapshots with new info message and reran tests.
This commit is contained in:
Emily B. Zhang 2025-05-19 17:24:25 -04:00 committed by GitHub
parent b302d89da3
commit a2c87c2bc1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 19 additions and 1 deletions

View file

@ -296,6 +296,7 @@ fn cli_arguments_are_relative_to_the_current_directory() -> anyhow::Result<()> {
3 |
4 | stat = add(10, 15)
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` is enabled by default
Found 1 diagnostic
@ -481,6 +482,7 @@ fn cli_rule_severity() -> anyhow::Result<()> {
3 |
4 | y = 4 / 0
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` is enabled by default
error[division-by-zero]: Cannot divide object of type `Literal[4]` by zero
@ -532,6 +534,7 @@ fn cli_rule_severity() -> anyhow::Result<()> {
3 |
4 | y = 4 / 0
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` was selected on the command line
warning[division-by-zero]: Cannot divide object of type `Literal[4]` by zero
@ -1120,6 +1123,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
3 |
4 | print(z)
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` is enabled by default
error[unresolved-import]: Cannot resolve imported module `does_not_exist`
@ -1128,6 +1132,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
2 | import does_not_exist # error: unresolved-import
| ^^^^^^^^^^^^^^
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` is enabled by default
Found 3 diagnostics
@ -1153,6 +1158,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
3 |
4 | print(z)
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` is enabled by default
error[unresolved-import]: Cannot resolve imported module `does_not_exist`
@ -1161,6 +1167,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
2 | import does_not_exist # error: unresolved-import
| ^^^^^^^^^^^^^^
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` is enabled by default
Found 2 diagnostics

View file

@ -26,6 +26,7 @@ error[unresolved-import]: Cannot resolve imported module `does_not_exist`
2 | from does_not_exist import foo, bar, baz
| ^^^^^^^^^^^^^^
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` is enabled by default
```

View file

@ -24,6 +24,7 @@ error[unresolved-import]: Cannot resolve imported module `zqzqzqzqzqzqzq`
1 | import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve imported module `zqzqzqzqzqzqzq`"
| ^^^^^^^^^^^^^^
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` is enabled by default
```

View file

@ -36,6 +36,7 @@ error[unresolved-import]: Cannot resolve imported module `a.foo`
3 |
4 | # Topmost component unresolvable:
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` is enabled by default
```
@ -48,6 +49,7 @@ error[unresolved-import]: Cannot resolve imported module `b.foo`
5 | import b.foo # error: [unresolved-import] "Cannot resolve imported module `b.foo`"
| ^^^^^
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` is enabled by default
```

View file

@ -28,6 +28,7 @@ error[unresolved-import]: Cannot resolve imported module `does_not_exist`
2 |
3 | x = does_not_exist.foo
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` is enabled by default
```

View file

@ -28,6 +28,7 @@ error[unresolved-import]: Cannot resolve imported module `does_not_exist`
2 |
3 | stat = add(10, 15)
|
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
info: rule `unresolved-import` is enabled by default
```

View file

@ -3812,11 +3812,16 @@ impl<'db> TypeInferenceBuilder<'db> {
let Some(builder) = self.context.report_lint(&UNRESOLVED_IMPORT, range) else {
return;
};
builder.into_diagnostic(format_args!(
let mut diagnostic = builder.into_diagnostic(format_args!(
"Cannot resolve imported module `{}{}`",
".".repeat(level as usize),
module.unwrap_or_default()
));
if level == 0 {
diagnostic.info(
"make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment"
);
}
}
fn infer_import_definition(