Skip to content

Commit a2c87c2

Browse files
authored
[ty] Add note to unresolved-import hinting to users to configure their Python environment (#18207)
Closes astral-sh/ty#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.
1 parent b302d89 commit a2c87c2

7 files changed

+19
-1
lines changed

crates/ty/tests/cli.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ fn cli_arguments_are_relative_to_the_current_directory() -> anyhow::Result<()> {
296296
3 |
297297
4 | stat = add(10, 15)
298298
|
299+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
299300
info: rule `unresolved-import` is enabled by default
300301
301302
Found 1 diagnostic
@@ -481,6 +482,7 @@ fn cli_rule_severity() -> anyhow::Result<()> {
481482
3 |
482483
4 | y = 4 / 0
483484
|
485+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
484486
info: rule `unresolved-import` is enabled by default
485487
486488
error[division-by-zero]: Cannot divide object of type `Literal[4]` by zero
@@ -532,6 +534,7 @@ fn cli_rule_severity() -> anyhow::Result<()> {
532534
3 |
533535
4 | y = 4 / 0
534536
|
537+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
535538
info: rule `unresolved-import` was selected on the command line
536539
537540
warning[division-by-zero]: Cannot divide object of type `Literal[4]` by zero
@@ -1120,6 +1123,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
11201123
3 |
11211124
4 | print(z)
11221125
|
1126+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
11231127
info: rule `unresolved-import` is enabled by default
11241128
11251129
error[unresolved-import]: Cannot resolve imported module `does_not_exist`
@@ -1128,6 +1132,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
11281132
2 | import does_not_exist # error: unresolved-import
11291133
| ^^^^^^^^^^^^^^
11301134
|
1135+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
11311136
info: rule `unresolved-import` is enabled by default
11321137
11331138
Found 3 diagnostics
@@ -1153,6 +1158,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
11531158
3 |
11541159
4 | print(z)
11551160
|
1161+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
11561162
info: rule `unresolved-import` is enabled by default
11571163
11581164
error[unresolved-import]: Cannot resolve imported module `does_not_exist`
@@ -1161,6 +1167,7 @@ fn check_specific_paths() -> anyhow::Result<()> {
11611167
2 | import does_not_exist # error: unresolved-import
11621168
| ^^^^^^^^^^^^^^
11631169
|
1170+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
11641171
info: rule `unresolved-import` is enabled by default
11651172
11661173
Found 2 diagnostics

crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Multiple_objects_imp…_(cbfbf5ff94e6e104).snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ error[unresolved-import]: Cannot resolve imported module `does_not_exist`
2626
2 | from does_not_exist import foo, bar, baz
2727
| ^^^^^^^^^^^^^^
2828
|
29+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
2930
info: rule `unresolved-import` is enabled by default
3031
3132
```

crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Unresolvable_module_…_(846453deaca1071c).snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ error[unresolved-import]: Cannot resolve imported module `zqzqzqzqzqzqzq`
2424
1 | import zqzqzqzqzqzqzq # error: [unresolved-import] "Cannot resolve imported module `zqzqzqzqzqzqzq`"
2525
| ^^^^^^^^^^^^^^
2626
|
27+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
2728
info: rule `unresolved-import` is enabled by default
2829
2930
```

crates/ty_python_semantic/resources/mdtest/snapshots/basic.md_-_Structures_-_Unresolvable_submodu…_(4fad4be9778578b7).snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ error[unresolved-import]: Cannot resolve imported module `a.foo`
3636
3 |
3737
4 | # Topmost component unresolvable:
3838
|
39+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
3940
info: rule `unresolved-import` is enabled by default
4041
4142
```
@@ -48,6 +49,7 @@ error[unresolved-import]: Cannot resolve imported module `b.foo`
4849
5 | import b.foo # error: [unresolved-import] "Cannot resolve imported module `b.foo`"
4950
| ^^^^^
5051
|
52+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
5153
info: rule `unresolved-import` is enabled by default
5254
5355
```

crates/ty_python_semantic/resources/mdtest/snapshots/unresolved_import.md_-_Unresolved_import_di…_-_An_unresolvable_impo…_(72d090df51ea97b8).snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ error[unresolved-import]: Cannot resolve imported module `does_not_exist`
2828
2 |
2929
3 | x = does_not_exist.foo
3030
|
31+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
3132
info: rule `unresolved-import` is enabled by default
3233
3334
```

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ error[unresolved-import]: Cannot resolve imported module `does_not_exist`
2828
2 |
2929
3 | stat = add(10, 15)
3030
|
31+
info: make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment
3132
info: rule `unresolved-import` is enabled by default
3233
3334
```

crates/ty_python_semantic/src/types/infer.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3812,11 +3812,16 @@ impl<'db> TypeInferenceBuilder<'db> {
38123812
let Some(builder) = self.context.report_lint(&UNRESOLVED_IMPORT, range) else {
38133813
return;
38143814
};
3815-
builder.into_diagnostic(format_args!(
3815+
let mut diagnostic = builder.into_diagnostic(format_args!(
38163816
"Cannot resolve imported module `{}{}`",
38173817
".".repeat(level as usize),
38183818
module.unwrap_or_default()
38193819
));
3820+
if level == 0 {
3821+
diagnostic.info(
3822+
"make sure your Python environment is properly configured: https://github.com/astral-sh/ty/blob/main/docs/README.md#python-environment"
3823+
);
3824+
}
38203825
}
38213826

38223827
fn infer_import_definition(

0 commit comments

Comments
 (0)