ruff/crates/ty_python_semantic/resources/mdtest/import/namespace.md

1.4 KiB

Namespace package

Basic namespace package

[environment]
python = "/.venv"

parent/child/one.py:

one = 1

/.venv/<path-to-site-packages>/parent/child/two.py:

two = 2

main.py:

import parent.child.one
import parent.child.two

from.py

# TODO: This should not be an error
from parent.child import one, two  # error: [unresolved-import]

Regular package in namespace package

[environment]
python = "/.venv"

An adapted test case from the PEP420 examples. The src/parent/child package is a regular package. Therefore, site_packages/parent/child/two.py should not be resolved.

src
  parent
    child
      __init__.py
      one.py
.venv/site-packages
  parent
    child
      two.py

parent/child/__init__.py:

parent/child/one.py:

one = 1

/.venv/<path-to-site-packages>/parent/child/two.py:

two = 2

main.py:

import parent.child.one

import parent.child.two  # error: [unresolved-import]

Priority between file and identically named namespace package

If there's a namespace package with the same name as a module, the module takes precedence.

foo.py:

x = "module"

foo/bar.py:

x = "namespace"
from foo import x

reveal_type(x)  # revealed: Unknown | Literal["module"]

import foo.bar  # error: [unresolved-import]