codex-python / tests /test_ast_imports.py
tailot's picture
Upload folder using huggingface_hub
f4952b6 verified
Raw History Blame Contribute Delete
8.74 kB
"""Import resolution over a package that uses the awkward import forms."""
from __future__ import annotations
from pathlib import Path
from typing import List, Tuple
from lingologos_python.ast_graph import (
build_reverse_index,
discover_python_files,
extract_python_graph,
importers_from_index,
importers_of,
reexported_names,
)
from lingologos_python.test_selector import select_python_tests
def _write(root: Path, rel_posix: str, text: str) -> None:
path = root / Path(*rel_posix.split("/"))
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, encoding="utf-8", newline="\n")
def _hard_package(root: Path) -> None:
"""One package with the forms that broke selection before: dots, stars, aliases, no __init__."""
_write(root, "app/__init__.py", "")
_write(root, "app/core/__init__.py", "")
_write(root, "app/core/engine.py", "def run() -> int:\n return 1\n")
_write(root, "app/core/legacy.py", "def old() -> int:\n return 0\n")
_write(root, "app/core/sub/__init__.py", "")
_write(
root,
"app/core/sub/calc.py",
"from ..engine import run\n\n\ndef twice() -> int:\n return run() + run()\n",
)
# app/plugins carries no __init__.py: a namespace package, named by its folder.
_write(root, "app/plugins/extra.py", "def assist() -> str:\n return 'assist'\n")
_write(
root,
"tests/test_plain.py",
"from app.core.engine import run\n\n\ndef test_run():\n assert run() == 1\n",
)
_write(
root,
"tests/test_alias.py",
"from app.core import engine as eng\n\n\ndef test_eng():\n assert eng.run() == 1\n",
)
_write(
root,
"tests/test_star.py",
"from app.core.legacy import *\n\n\ndef test_old():\n assert old() == 0\n",
)
_write(
root,
"tests/test_ns.py",
"from app.plugins.extra import assist\n\n\ndef test_assist():\n assert assist() == 'assist'\n",
)
def _file_edges(root: Path) -> List[Tuple[str, str]]:
_, edges = extract_python_graph(root, discover_python_files(root))
return [(s.split("::", 1)[0], d.split("::", 1)[0]) for s, d in edges]
def test_relative_dots_namespace_and_star_imports_become_file_edges(tmp_path: Path) -> None:
_hard_package(tmp_path)
edges = set(_file_edges(tmp_path))
assert ("app/core/sub/calc.py", "app/core/engine.py") in edges
assert ("tests/test_alias.py", "app/core/engine.py") in edges
assert ("tests/test_star.py", "app/core/legacy.py") in edges
assert ("tests/test_ns.py", "app/plugins/extra.py") in edges
def test_an_aliased_module_import_selects_the_module_it_names(tmp_path: Path) -> None:
_hard_package(tmp_path)
selected = select_python_tests(tmp_path, ["app/core/engine.py"])
assert "tests/test_alias.py" in selected
assert "tests/test_plain.py" in selected
assert "tests/test_star.py" not in selected
assert "tests/test_ns.py" not in selected
def test_a_star_import_selects_the_test_that_stars_the_changed_module(tmp_path: Path) -> None:
_hard_package(tmp_path)
selected = select_python_tests(tmp_path, ["app/core/legacy.py"])
assert "tests/test_star.py" in selected
assert "tests/test_plain.py" not in selected
def test_a_namespace_package_change_selects_its_only_importer(tmp_path: Path) -> None:
_hard_package(tmp_path)
selected = select_python_tests(tmp_path, ["app/plugins/extra.py"])
assert selected == ["tests/test_ns.py"]
def _reexport_package(root: Path) -> None:
"""One package that answers with names its __init__ imports from inside itself."""
_write(root, "app/__init__.py", "from .impl import helper\nfrom . import tools\n")
_write(root, "app/impl.py", "def helper() -> int:\n return 2\n")
_write(root, "app/tools.py", "X = 1\n")
_write(
root,
"tests/test_app.py",
"from app import helper\n\n\ndef test_helper():\n assert helper() == 2\n",
)
_write(
root,
"tests/test_tools.py",
"from app.tools import X\n\n\ndef test_x():\n assert X == 1\n",
)
def test_a_reexport_table_names_every_file_a_package_answer_for(tmp_path: Path) -> None:
_reexport_package(tmp_path)
table = reexported_names(tmp_path, "app/__init__.py")
assert "app.impl" in table["helper"]
assert "app.tools" in table["tools"]
def test_a_change_behind_a_reexport_reaches_the_test_that_never_named_the_file(
tmp_path: Path,
) -> None:
_reexport_package(tmp_path)
files = discover_python_files(tmp_path)
assert "tests/test_app.py" in importers_of(tmp_path, files, "app/impl.py")
edges = set(_file_edges(tmp_path))
assert ("tests/test_app.py", "app/impl.py") in edges
selected = select_python_tests(tmp_path, ["app/impl.py"])
assert "tests/test_app.py" in selected
def _star_reexport_package(root: Path) -> None:
"""A package whose __init__ stars two modules: one lists __all__, one does not."""
_write(root, "app/__init__.py", "from .impl import *\nfrom .tools import *\n")
_write(
root,
"app/impl.py",
"__all__ = ['shown']\n\nhidden = 3\n\n\ndef shown() -> int:\n return 5\n",
)
_write(
root,
"app/tools.py",
"visible = 1\n_hidden = 2\n\n\ndef usable() -> int:\n return visible\n",
)
_write(
root,
"tests/test_star_app.py",
"from app import *\n\n\ndef test_shown():\n assert shown() == 5\n",
)
def test_a_star_reexport_table_expands_to_the_names_the_star_hands_over(
tmp_path: Path,
) -> None:
_star_reexport_package(tmp_path)
table = reexported_names(tmp_path, "app/__init__.py")
assert table.get("shown") == ["app.impl.shown"]
assert "hidden" not in table # __all__ said shown, and __all__ is the contract
assert table.get("visible") == ["app.tools.visible"]
assert table.get("usable") == ["app.tools.usable"]
assert "_hidden" not in table # a star never carries an underscored name
def test_a_change_behind_a_star_reexport_selects_the_test_that_stars_the_package(
tmp_path: Path,
) -> None:
_star_reexport_package(tmp_path)
edges = set(_file_edges(tmp_path))
assert ("tests/test_star_app.py", "app/impl.py") in edges
selected = select_python_tests(tmp_path, ["app/impl.py"])
assert "tests/test_star_app.py" in selected
def test_a_literal_dynamic_import_counts_as_an_import(tmp_path: Path) -> None:
"""import_module and __import__ with a plain string name bind a file like any import."""
root = tmp_path
_write(root, "app/__init__.py", "")
_write(root, "app/impl.py", "def helper() -> int:\n return 2\n")
_write(
root,
"tests/test_module_lib.py",
"import importlib\n\n\ndef test_dyn():\n"
" mod = importlib.import_module('app.impl')\n"
" assert mod.helper() == 2\n",
)
_write(
root,
"tests/test_dunder_import.py",
"impl = __import__('app.impl')\n\n\ndef test_dyn():\n"
" assert impl.helper() == 2\n",
)
_write(
root,
"tests/test_opaque.py",
"import importlib\n\n\ndef test_opaque():\n"
" mod = importlib.import_module(NAME_FROM_CONFIG)\n"
" assert mod is not None\n",
)
files = discover_python_files(tmp_path)
importers = importers_of(tmp_path, files, "app/impl.py")
assert "tests/test_module_lib.py" in importers
assert "tests/test_dunder_import.py" in importers
assert "tests/test_opaque.py" not in importers
edges = set(_file_edges(tmp_path))
assert ("tests/test_module_lib.py", "app/impl.py") in edges
assert ("tests/test_dunder_import.py", "app/impl.py") in edges
selected = select_python_tests(tmp_path, ["app/impl.py"])
assert "tests/test_module_lib.py" in selected
assert "tests/test_dunder_import.py" in selected
assert "tests/test_opaque.py" not in selected
def test_an_index_built_once_answers_every_target_as_a_per_query_walk_does(
tmp_path: Path,
) -> None:
"""A workspace parsed once gives the same importers as parsing it once per question."""
hard = tmp_path / "hard"
_hard_package(hard)
reexported = tmp_path / "reexported"
_reexport_package(reexported)
for workspace in (hard, reexported):
files = discover_python_files(workspace)
index = build_reverse_index(workspace, files)
for target in files:
remaining = [p for p in files if p != target]
assert importers_from_index(index, target) == sorted(
importers_of(workspace, remaining, target)
), "the index and the walk disagree about %s" % target