ruff/crates/ty_python_semantic/resources/mdtest/import/basic.md
Alex Waygood e2d96df501
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 / 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
CI / formatter instabilities and black similarity (push) Blocked by required conditions
[ty] Improve diagnostics if the user attempts to import a stdlib module that does not exist on their configured Python version (#18403)
2025-06-02 10:52:26 +00:00

3.2 KiB

Structures

Class import following

from b import C as D

E = D
reveal_type(E)  # revealed: <class 'C'>

b.py:

class C: ...

Module member resolution

import b

D = b.C
reveal_type(D)  # revealed: <class 'C'>

b.py:

class C: ...

Nested

import a.b

reveal_type(a.b.C)  # revealed: <class 'C'>

a/__init__.py:

a/b.py:

class C: ...

Deeply nested

import a.b.c

reveal_type(a.b.c.C)  # revealed: <class 'C'>

a/__init__.py:

a/b/__init__.py:

a/b/c.py:

class C: ...

Nested with rename

import a.b as b

reveal_type(b.C)  # revealed: <class 'C'>

a/__init__.py:

a/b.py:

class C: ...

Deeply nested with rename

import a.b.c as c

reveal_type(c.C)  # revealed: <class 'C'>

a/__init__.py:

a/b/__init__.py:

a/b/c.py:

class C: ...

Unresolvable module import

import zqzqzqzqzqzqzq  # error: [unresolved-import] "Cannot resolve imported module `zqzqzqzqzqzqzq`"

Unresolvable submodule imports

# Topmost component resolvable, submodule not resolvable:
import a.foo  # error: [unresolved-import] "Cannot resolve imported module `a.foo`"

# Topmost component unresolvable:
import b.foo  # error: [unresolved-import] "Cannot resolve imported module `b.foo`"

a/__init__.py:

Long paths

It's unlikely that a single module component is as long as in this example, but Windows treats paths that are longer than 200 and something specially. This test ensures that ty can handle those paths gracefully.

system = "os"

AveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPath/__init__.py:

class Foo: ...
from AveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPathAveryLongPath import (
    Foo,
)

reveal_type(Foo())  # revealed: Foo

Multiple objects imported from an unresolved module

If multiple members are imported from a module that cannot be resolved, only a single diagnostic is 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

[environment]
python-version = "3.10"
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

[environment]
python-version = "3.13"
import aifc  # error: [unresolved-import]
from distutils import sysconfig  # error: [unresolved-import]