Skip to content

ty fails to lookup relative imports if the relative path from the nearest import root includes an invalid module name #839

@MichaReiser

Description

@MichaReiser

Langchain has an __init__.py with the following content

# ruff: noqa: E402
import pytest

# Rewrite assert statements for test suite so that implementations can
# see the full error message from failed asserts.
# https://docs.pytest.org/en/7.1.x/how-to/writing_plugins.html#assertion-rewriting
modules = [
    "chat_models",
    "embeddings",
    "tools",
]

for module in modules:
    pytest.register_assert_rewrite(f"langchain_tests.unit_tests.{module}")

from .chat_models import ChatModelUnitTests
from .embeddings import EmbeddingsUnitTests
from .tools import ToolsUnitTests

__all__ = ["ChatModelUnitTests", "EmbeddingsUnitTests", "ToolsUnitTests"]

ty fails to resolve the relative imports chat_models, embeddings and .tools even though those files clearly exist. Pyright seems fine with the import.

The issue seems to be that file_to_module fails because the relative path to the search root (which is the project root) libs/standard-tests/langchain_tests/unit_tests/teswt.py can't be turned into a valid module name because of the standard-tests.

A solution here is to add standard-tests to the search path but having to do this is at least confusing in an LSP setup (where ty should just work out of the box)

Playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    importsModule resolution, site-packages discovery, import-related diagnostics

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions