crates/ty_python_semantic/resources/mdtest/import/cyclic.md
See: https://github.com/astral-sh/ty/issues/261
main.py:
from foo import bar
reveal_type(bar) # revealed: <module 'foo.bar'>
foo/__init__.py:
from foo import bar
__all__ = ["bar"]
foo/bar/__init__.py:
# empty
See: https://github.com/astral-sh/ty/issues/113
main.py:
from pkg.sub import A
# TODO: This should be `<class 'A'>`
reveal_type(A) # revealed: Divergent
pkg/outer.py:
class A: ...
pkg/sub/__init__.py:
from ..outer import *
from .inner import *
pkg/sub/inner.py:
from pkg.sub import A
The following example fails at runtime. Ideally, we would emit a diagnostic here. For now, we only make sure that this does not lead to a module resolution cycle.
main.py:
from module import x
reveal_type(x) # revealed: Unknown
module.py:
# error: [unresolved-import]
from module import x
from import in a nested scopeA from <self> import <name> inside a function body should resolve the name from the module's
global scope without triggering a cycle.
See: https://github.com/astral-sh/ty/issues/2596
main.py:
def foo() -> int:
return 0
def bar() -> int:
from main import foo
return foo()
Some modules like sys in typeshed import themselves. Here, we make sure that this does not lead to
cycles or unresolved imports.
module/__init__.py:
import module # self-referential import
from module.sub import x
module/sub.py:
x: int = 1
main.py:
from module import x
reveal_type(x) # revealed: int