6.6 KiB
Structures
Imported modules can be reassigned
Importing a module binds its current module-literal type, but does not declare that later assignments must preserve that exact type.
import os
from types import ModuleType
def normalize(module: ModuleType) -> ModuleType:
return module
os = normalize(os)
reveal_type(os) # revealed: ModuleType
Imported names can be rebound by match patterns in loops
Checking whether an imported name is Final must not recursively infer synthetic loop or
comprehension bindings when a match pattern rebinds that name.
import os as x
class C:
x = object()
items = [object()]
for _ in items:
match (x for _ in items):
case C.x as x:
pass
case C():
x = 0
Imported names can be rebound by context managers
An imported name is an ordinary binding, so a context-manager target may replace it with a value of another type.
values.py:
value: str = "before"
from values import value
class Manager:
def __enter__(self) -> int:
return 1
def __exit__(self, exc_type, exc_value, traceback): ...
with Manager() as value:
reveal_type(value) # revealed: int
Imported names can participate in cyclic loop and match bindings
Names introduced by from imports can create the same cyclic loop and match bindings as imported
modules, even when the imported value is not final.
source.py:
value = object()
main.py:
from source import value
class Matcher:
value = object()
items = [object()]
for _ in items:
match (value for _ in items):
case Matcher.value as value:
pass
case Matcher():
value = 0
Existing annotations constrain imported bindings
An import must respect an existing declaration without creating a conflicting declaration of its own.
values.py:
value: str = "available"
value: str | None
try:
from values import value
except ImportError:
value = None
reveal_type(value) # revealed: str | None
Imported values must satisfy existing annotations
Although an import does not declare a type, its value must still be assignable to an existing local declaration.
values.py:
value: str = "incompatible"
value: int
from values import value # error: [invalid-assignment]
reveal_type(value) # revealed: int
Star-imported names can be reassigned
Wildcard imports bind the names they introduce without declaring their imported types.
values.py:
value: str = "before"
from values import *
value = 1
reveal_type(value) # revealed: Literal[1]
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: [import-not-found] "Cannot resolve imported module `zqzqzqzqzqzqzq`"
Unresolvable submodule imports
# Topmost component resolvable, submodule not resolvable:
import a.foo # error: [import-not-found] "Cannot resolve imported module `a.foo`"
# Topmost component unresolvable:
import b.foo # error: [import-not-found] "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: [import-not-found]
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: [import-not-found]
from string.templatelib import Template # error: [import-not-found]
from importlib.resources import abc # error: [missing-module-member]
Attempting to import a stdlib submodule when both parts haven't yet been added
compression and compression.zstd were both added in 3.14 so there is a typeshed VERSIONS entry
for compression but not compression.zstd. We can't be confident compression.zstd exists but we
do know compression does and can still give good diagnostics about it.
[environment]
python-version = "3.10"
import compression.zstd # error: [import-not-found]
from compression import zstd # error: [import-not-found]
import compression.fakebutwhocansay # error: [import-not-found]
from compression import fakebutwhocansay # error: [import-not-found]
Attempting to import a stdlib module that was previously removed
[environment]
python-version = "3.13"
import aifc # error: [import-not-found]
from distutils import sysconfig # error: [import-not-found]
Cannot shadow core standard library modules
types.py:
x: int
# error: [missing-module-member]
from types import x
from types import FunctionType