Skip to content

Support legacy namespace packages #838

@MichaReiser

Description

@MichaReiser

Add support for legacy namespace packages of the form __import__("pkgutil").extend_path(__path__, __name__) as is used by airflow.

See astral-sh/ruff#20016 (comment) for some discussions around how to implement this

Airflow's project structure roughly is:

airflow-core/src/airlfow
  - py.typed (empty except for licence)
  - __init__.py
providers/
  - cncf/kubernetes
    | - pyproject.toml
    | - src/airflow/
      | - __init__.py
      | - providers/
        | - __init__.py
        | - cncf  
          | - __init__.py
          | - kubernetes/
            | - __init__.py
            | - kubernetes_helper_functions.py
  - ... more providers

Each provider is linked into the virtual environment by usinbg an editable install. ty correctly picks them up.

2025-07-17 12:13:06.067791000 DEBUG request{id=2 method="textDocument/semanticTokens/full"}: Adding editable installation to module resolution path /Users/micha/astral/ecosystem/apache:airflow/providers/cncf/kubernetes/src

However, ty can't resolve the airflow.providers.cncf.kubernetes.kubernetes_helper_functions import:

2025-07-17 12:34:06.608986 TRACE Search path '/Users/micha/astral/ecosystem/apache:airflow' contains no stub package named `airflow-stubs.providers.cncf.kubernetes.kubernetes_helper_functions`.
2025-07-17 12:34:06.609018 TRACE Search path '/Users/micha/astral/ecosystem/apache:airflow' contains no package named `airflow.providers.cncf.kubernetes.kubernetes_helper_functions`.
2025-07-17 12:34:06.609036 TRACE Search path 'vendored://stdlib' contains no package named `airflow.providers.cncf.kubernetes.kubernetes_helper_functions`.
2025-07-17 12:34:06.609092 TRACE Search path '/Users/micha/astral/ecosystem/apache:airflow/.venv/lib/python3.12/site-packages' contains no stub package named `airflow-stubs.providers.cncf.kubernetes.kubernetes_helper_functions`.
2025-07-17 12:34:06.609127 TRACE Search path '/Users/micha/astral/ecosystem/apache:airflow/.venv/lib/python3.12/site-packages' contains no package named `airflow.providers.cncf.kubernetes.kubernetes_helper_functions`.
2025-07-17 12:34:06.609161 TRACE Search path '/Users/micha/astral/ecosystem/apache:airflow/airflow-core/src' contains no stub package named `airflow-stubs.providers.cncf.kubernetes.kubernetes_helper_functions`.
2025-07-17 12:34:06.609213 TRACE Package in `/Users/micha/astral/ecosystem/apache:airflow/airflow-core/src doesn't contain module: `airflow.providers.cncf.kubernetes.kubernetes_helper_functions`
2025-07-17 12:34:06.609226 DEBUG Module `airflow.providers.cncf.kubernetes.kubernetes_helper_functions` not found in search paths

The problem is that ty stops searching for kubernetes_helper_functions after it successfully resolved airflow to a regular package in airflow-core. This does seem correct to me. However, it's not clear how airflow would configure ty to get import resolution working because the resolution will always stop after the first import.

airflow's setup works for downstream users because the provider packages get merged into the airflow package during installation. The layout of the package in venv is:

airlfow
  - py.typed (empty except for licence)
  - __init__.py
    providers/
    | - cncf  
      | - __init__.py
      | - kubernetes/
        | - __init__.py
        | - kubernetes_helper_functions.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedContributions especially welcomeimportsModule resolution, site-packages discovery, import-related diagnostics

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions