ruff/crates/ty_python_semantic/resources/mdtest/import/basic.md
2025-05-03 19:49:15 +02:00

2.2 KiB

Structures

Class import following

from b import C as D

E = D
reveal_type(E)  # revealed: Literal[C]

b.py:

class C: ...

Module member resolution

import b

D = b.C
reveal_type(D)  # revealed: Literal[C]

b.py:

class C: ...

Nested

import a.b

reveal_type(a.b.C)  # revealed: Literal[C]

a/__init__.py:

a/b.py:

class C: ...

Deeply nested

import a.b.c

reveal_type(a.b.c.C)  # revealed: Literal[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: Literal[C]

a/__init__.py:

a/b.py:

class C: ...

Deeply nested with rename

import a.b.c as c

reveal_type(c.C)  # revealed: Literal[C]

a/__init__.py:

a/b/__init__.py:

a/b/c.py:

class C: ...

Unresolvable module import

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

Unresolvable submodule imports

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

# Topmost component unresolvable:
import b.foo  # error: [unresolved-import] "Cannot resolve import `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