[ty] Improve diagnostics if the user attempts to import a stdlib module that does not exist on their configured Python version (#18403)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / mkdocs (push) Waiting to run
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

This commit is contained in:
Alex Waygood 2025-06-02 11:52:26 +01:00 committed by GitHub
parent 384e80ec80
commit e2d96df501
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 300 additions and 38 deletions

View file

@ -176,3 +176,32 @@ emitted for the `import from` statement:
# error: [unresolved-import]
from does_not_exist import foo, bar, baz
```
## Attempting to import a stdlib module that's not yet been added
<!-- snapshot-diagnostics -->
```toml
[environment]
python-version = "3.10"
```
```py
import tomllib # error: [unresolved-import]
from string.templatelib import Template # error: [unresolved-import]
from importlib.resources import abc # error: [unresolved-import]
```
## Attempting to import a stdlib module that was previously removed
<!-- snapshot-diagnostics -->
```toml
[environment]
python-version = "3.13"
```
```py
import aifc # error: [unresolved-import]
from distutils import sysconfig # error: [unresolved-import]
```

View file

@ -0,0 +1,65 @@
---
source: crates/ty_test/src/lib.rs
expression: snapshot
---
---
mdtest name: basic.md - Structures - Attempting to import a stdlib module that's not yet been added
mdtest path: crates/ty_python_semantic/resources/mdtest/import/basic.md
---
# Python source files
## mdtest_snippet.py
```
1 | import tomllib # error: [unresolved-import]
2 | from string.templatelib import Template # error: [unresolved-import]
3 | from importlib.resources import abc # error: [unresolved-import]
```
# Diagnostics
```
error[unresolved-import]: Cannot resolve imported module `tomllib`
--> src/mdtest_snippet.py:1:8
|
1 | import tomllib # error: [unresolved-import]
| ^^^^^^^
2 | from string.templatelib import Template # error: [unresolved-import]
3 | from importlib.resources import abc # error: [unresolved-import]
|
info: The stdlib module `tomllib` is only available on Python 3.11+
info: Python 3.10 was assumed when resolving modules because it was specified on the command line
info: rule `unresolved-import` is enabled by default
```
```
error[unresolved-import]: Cannot resolve imported module `string.templatelib`
--> src/mdtest_snippet.py:2:6
|
1 | import tomllib # error: [unresolved-import]
2 | from string.templatelib import Template # error: [unresolved-import]
| ^^^^^^^^^^^^^^^^^^
3 | from importlib.resources import abc # error: [unresolved-import]
|
info: The stdlib module `string.templatelib` is only available on Python 3.14+
info: Python 3.10 was assumed when resolving modules because it was specified on the command line
info: rule `unresolved-import` is enabled by default
```
```
error[unresolved-import]: Module `importlib.resources` has no member `abc`
--> src/mdtest_snippet.py:3:33
|
1 | import tomllib # error: [unresolved-import]
2 | from string.templatelib import Template # error: [unresolved-import]
3 | from importlib.resources import abc # error: [unresolved-import]
| ^^^
|
info: The stdlib module `importlib.resources` only has a `abc` submodule on Python 3.11+
info: Python 3.10 was assumed when resolving modules because it was specified on the command line
info: rule `unresolved-import` is enabled by default
```

View file

@ -0,0 +1,47 @@
---
source: crates/ty_test/src/lib.rs
expression: snapshot
---
---
mdtest name: basic.md - Structures - Attempting to import a stdlib module that was previously removed
mdtest path: crates/ty_python_semantic/resources/mdtest/import/basic.md
---
# Python source files
## mdtest_snippet.py
```
1 | import aifc # error: [unresolved-import]
2 | from distutils import sysconfig # error: [unresolved-import]
```
# Diagnostics
```
error[unresolved-import]: Cannot resolve imported module `aifc`
--> src/mdtest_snippet.py:1:8
|
1 | import aifc # error: [unresolved-import]
| ^^^^
2 | from distutils import sysconfig # error: [unresolved-import]
|
info: The stdlib module `aifc` is only available on Python <=3.12
info: Python 3.13 was assumed when resolving modules because it was specified on the command line
info: rule `unresolved-import` is enabled by default
```
```
error[unresolved-import]: Cannot resolve imported module `distutils`
--> src/mdtest_snippet.py:2:6
|
1 | import aifc # error: [unresolved-import]
2 | from distutils import sysconfig # error: [unresolved-import]
| ^^^^^^^^^
|
info: The stdlib module `distutils` is only available on Python <=3.11
info: Python 3.13 was assumed when resolving modules because it was specified on the command line
info: rule `unresolved-import` is enabled by default
```