diff --git a/packages/tangle-cli/src/tangle_cli/__init__.py b/packages/tangle-cli/src/tangle_cli/__init__.py index dd8ab10..c3fc380 100644 --- a/packages/tangle-cli/src/tangle_cli/__init__.py +++ b/packages/tangle-cli/src/tangle_cli/__init__.py @@ -14,6 +14,6 @@ try: __version__ = metadata_version("tangle-cli") except PackageNotFoundError: - __version__ = "0.1.37" + __version__ = "0.1.38" __all__ = ["TangleDynamicDiscoveryClient", "__version__"] diff --git a/packages/tangle-cli/src/tangle_cli/pipeline_compiler.py b/packages/tangle-cli/src/tangle_cli/pipeline_compiler.py index 093784c..847a338 100644 --- a/packages/tangle-cli/src/tangle_cli/pipeline_compiler.py +++ b/packages/tangle-cli/src/tangle_cli/pipeline_compiler.py @@ -1144,7 +1144,9 @@ def _process_subpipeline_children( ambient_context=ambient_passthrough or None, config_identity=child_identity, task_annotations=child_task_annotations, - own_task_annotations=_own_task_annotations_for_key(child_fn, child_doc, effective), + own_task_annotations=_own_task_annotations_for_key( + child_fn, child_doc, effective, base_dir=child_base_dir, ctx=ctx + ), ) # Cycle detection (covers self-reference). active_stack holds the @@ -1526,20 +1528,34 @@ def _own_task_annotations_for_key( pipeline_fn: PipelineFn, doc: CfgDocument | None, overrides: Mapping[str, Any], + *, + base_dir: Path, + ctx: CompileContext, ) -> dict[str, Any]: """A child's own ``@pipeline(task_annotations=...)`` as its trace applies them, for its compile key. A function is resolved against the cfg :func:`_resolve_cfg_for_pipeline` - builds for the child (its config plus ``overrides``). Without a config - file it contributes nothing: the child's own compile then fails on the - missing file. + builds for the child (its config plus ``overrides``), under the import + setup :func:`_compile_pipeline_fn` traces the child with (``base_dir`` + recorded for the after-compile purge, shadowing modules evicted, and + ``base_dir`` on ``sys.path``), so a function importing a sibling of the + child resolves it as it does in the trace. Without a config file it + contributes nothing: the child's own compile then fails on the missing + file. """ if not callable(pipeline_fn.task_annotations): return _own_task_annotations(pipeline_fn, None) if doc is None: return {} - return _own_task_annotations(pipeline_fn, build_cfg(doc.data, overrides=dict(overrides), coerce=False)) + cfg = build_cfg(doc.data, overrides=dict(overrides), coerce=False) + try: + ctx.source_dirs.add(base_dir.resolve()) + except OSError: # pragma: no cover — defensive + pass + _evict_shadowed_bundle_modules(base_dir) + with _temp_sys_path(base_dir): + return _own_task_annotations(pipeline_fn, cfg) def _validate_artifact(artifact: SubgraphArtifact, ctx: CompileContext) -> None: diff --git a/pyproject.toml b/pyproject.toml index f198f7b..8195739 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "tangle-cli" -version = "0.1.37" +version = "0.1.38" description = "CLI for Tangle, the open-source ML pipeline orchestration platform" readme = "README.md" authors = [ diff --git a/tests/test_packaging.py b/tests/test_packaging.py index 622eb5e..ad45a08 100644 --- a/tests/test_packaging.py +++ b/tests/test_packaging.py @@ -183,7 +183,7 @@ def test_tangle_cli_wheel_supports_expert_no_deps_import_path_without_tangle_api requires_dist = [line for line in metadata.splitlines() if line.startswith("Requires-Dist: ")] assert not any(name.startswith("tangle_api/") for name in names) assert "tangle_cli/openapi/openapi.json" not in names - assert "Version: 0.1.37" in metadata + assert "Version: 0.1.38" in metadata assert "Requires-Dist: tangle-api==0.1.1" in requires_dist assert not any("extra == 'native'" in line for line in requires_dist) assert "Provides-Extra: native" in metadata diff --git a/tests/test_task_annotation_defaults.py b/tests/test_task_annotation_defaults.py index 01535e0..e9db926 100644 --- a/tests/test_task_annotation_defaults.py +++ b/tests/test_task_annotation_defaults.py @@ -223,6 +223,53 @@ def root(): assert _annotations(_child(out, "First"), "Child Task") == {"team": "left"} +def test_a_child_defaults_function_imports_siblings_of_the_child(tmp_path): + src = tmp_path / "src" + child_dir = src / "child" + child_dir.mkdir(parents=True) + (child_dir / "child_annotation_helper.py").write_text( + "def annotations_for(cfg):\n return {'team': 'helper-' + cfg.team}\n", encoding="utf-8" + ) + (child_dir / "config.yaml").write_text("team: child\n", encoding="utf-8") + (child_dir / "child_pipeline.py").write_text( + _HEADER + + textwrap.dedent( + """ + def defaults(cfg): + from child_annotation_helper import annotations_for + + return annotations_for(cfg) + + @pipeline("Child", task_annotations=defaults) + def child(): + op.named("Child Task")() + """ + ), + encoding="utf-8", + ) + (src / "pipeline.py").write_text( + _HEADER + + textwrap.dedent( + """ + from child.child_pipeline import child + + @pipeline("Root") + def root(): + subpipeline(child).named("Run Child")() + """ + ), + encoding="utf-8", + ) + nested = tmp_path / "nested" / "compiled.yaml" + direct = tmp_path / "direct" / "compiled.yaml" + + compile_pipeline(src / "pipeline.py", nested, None, pipeline_name="Root") + compile_pipeline(child_dir / "child_pipeline.py", direct, None, pipeline_name="Child") + + assert _annotations(_child(nested, "Run Child"), "Child Task") == {"team": "helper-child"} + assert _annotations(direct, "Child Task") == {"team": "helper-child"} + + @pytest.mark.parametrize("declared", ['"davies"', 'lambda cfg: "davies"']) def test_pipeline_defaults_that_are_not_a_mapping_fail_the_compile(tmp_path, declared): with pytest.raises(CompileError, match=r"'Root'.*must be a mapping or a function of cfg returning one, got str"): diff --git a/uv.lock b/uv.lock index 8af0558..6b6ac5c 100644 --- a/uv.lock +++ b/uv.lock @@ -2083,7 +2083,7 @@ requires-dist = [{ name = "pydantic", specifier = ">=2.0" }] [[package]] name = "tangle-cli" -version = "0.1.37" +version = "0.1.38" source = { editable = "." } dependencies = [ { name = "cloud-pipelines" },