Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion .importlinter
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[importlinter]
root_package = anonymizer
root_packages =
anonymizer
measurement_tools

# The NDD adapter layer is a leaf boundary: it must never import the engine
# sub-workflows that call it.
Expand Down Expand Up @@ -45,3 +47,16 @@ source_modules =
anonymizer.interface.run_telemetry
forbidden_modules =
anonymizer.interface.anonymizer

[importlinter:contract:wandb-leaves-do-not-import-wandb-models]
name = W&B leaves do not import the compatibility facade
type = forbidden
source_modules =
measurement_tools.wandb_policy
measurement_tools.wandb_settings
measurement_tools.wandb_metadata
measurement_tools.wandb_metrics
measurement_tools.wandb_publication
measurement_tools.wandb_field_policies
forbidden_modules =
measurement_tools.wandb_models
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def loader(
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
for import_path in (*additional_paths, path.parent):
import_paths = list(additional_paths)
if not any(path.is_relative_to(import_path) for import_path in additional_paths):
import_paths.append(path.parent)
for import_path in import_paths:
sys.path.insert(0, str(import_path))
spec.loader.exec_module(module)
return module
Expand Down
578 changes: 578 additions & 0 deletions tests/tools/test_measurement_module_compatibility.py

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions tests/tools/test_measurement_strict_import_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
REPO_ROOT = Path(__file__).resolve().parents[2]


def _wandb_publisher_module() -> ModuleType:
publisher = importlib.import_module("measurement_tools.wandb_publisher")
return sys.modules[publisher.WandbPublisher.__module__]


def _write_simple_suite(tmp_path: Path, *, suite_id: str = "base-suite") -> Path:
return _write_yaml(tmp_path / "suite.yaml", _simple_suite_payload(suite_id=suite_id))

Expand Down Expand Up @@ -649,7 +654,7 @@ def test_wandb_publisher_uses_explicit_handle_and_finishes_once(
case = _write_case_measurements(run_benchmarks_tool, tmp_path, _minimal_benchmark_case(run_benchmarks_tool))
state = _wandb_state()
fake_wandb = _fake_wandb_module(state)
setup_module = sys.modules[run_benchmarks_tool.publish_benchmark_wandb_best_effort.__module__]
setup_module = _wandb_publisher_module()
monkeypatch.setattr(setup_module, "require_wandb", lambda: fake_wandb)

result = setup_module.WandbPublisher().publish(
Expand Down Expand Up @@ -756,7 +761,7 @@ def test_wandb_table_opt_in_changes_only_table_payloads(
run_benchmarks_tool: ModuleType,
) -> None:
case = _write_case_measurements(run_benchmarks_tool, tmp_path, _minimal_benchmark_case(run_benchmarks_tool))
setup_module = sys.modules[run_benchmarks_tool.publish_benchmark_wandb_best_effort.__module__]
setup_module = _wandb_publisher_module()
states: list[SimpleNamespace] = []

for log_tables in (False, True):
Expand Down Expand Up @@ -811,7 +816,7 @@ def failing_init(**kwargs: Any) -> Any:
return run

fake_wandb.init = failing_init
setup_module = sys.modules[run_benchmarks_tool.publish_benchmark_wandb_best_effort.__module__]
setup_module = _wandb_publisher_module()
monkeypatch.setattr(setup_module, "require_wandb", lambda: fake_wandb)

with pytest.raises(RuntimeError, match=f"{failure_stage} failed"):
Expand Down Expand Up @@ -844,7 +849,7 @@ def init_with_two_failures(**kwargs: Any) -> Any:
return run

fake_wandb.init = init_with_two_failures
setup_module = sys.modules[run_benchmarks_tool.publish_benchmark_wandb_best_effort.__module__]
setup_module = _wandb_publisher_module()
monkeypatch.setattr(setup_module, "require_wandb", lambda: fake_wandb)

with pytest.raises(ExceptionGroup) as raised:
Expand Down Expand Up @@ -876,7 +881,7 @@ def init_with_bad_finish(**kwargs: Any) -> Any:
return run

fake_wandb.init = init_with_bad_finish
setup_module = sys.modules[run_benchmarks_tool.publish_benchmark_wandb_best_effort.__module__]
setup_module = _wandb_publisher_module()
monkeypatch.setattr(setup_module, "require_wandb", lambda: fake_wandb)

with pytest.raises(RuntimeError, match="finish failed"):
Expand Down Expand Up @@ -943,7 +948,7 @@ def init_with_changed_id(**kwargs: Any) -> Any:
return run

fake_wandb.init = init_with_changed_id
setup_module = sys.modules[run_benchmarks_tool.publish_benchmark_wandb_best_effort.__module__]
setup_module = _wandb_publisher_module()
monkeypatch.setattr(setup_module, "require_wandb", lambda: fake_wandb)

with pytest.raises(RuntimeError, match="different run identity"):
Expand All @@ -967,7 +972,7 @@ def test_wandb_publisher_rejects_ambient_active_run(
case = _write_case_measurements(run_benchmarks_tool, tmp_path, _minimal_benchmark_case(run_benchmarks_tool))
state = _wandb_state()
fake_wandb = _fake_wandb_module(state, active_run=True)
setup_module = sys.modules[run_benchmarks_tool.publish_benchmark_wandb_best_effort.__module__]
setup_module = _wandb_publisher_module()
monkeypatch.setattr(setup_module, "require_wandb", lambda: fake_wandb)

with pytest.raises(RuntimeError, match="ambient W&B run"):
Expand Down Expand Up @@ -1000,7 +1005,7 @@ def init_with_interrupt(**kwargs: Any) -> Any:
return run

fake_wandb.init = init_with_interrupt
setup_module = sys.modules[run_benchmarks_tool.publish_benchmark_wandb_best_effort.__module__]
setup_module = _wandb_publisher_module()
monkeypatch.setattr(setup_module, "require_wandb", lambda: fake_wandb)

with pytest.raises(KeyboardInterrupt):
Expand Down
4 changes: 2 additions & 2 deletions tests/tools/test_measurement_wandb_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from __future__ import annotations

import importlib
import json
import logging
import os
import sys
from pathlib import Path
from types import ModuleType, SimpleNamespace
from typing import Any
Expand Down Expand Up @@ -608,7 +608,7 @@ def fail_publish(*_args: Any, **_kwargs: Any) -> Any:


def test_wandb_outbound_models_structurally_exclude_sensitive_fields(wandb_setup_tool: ModuleType) -> None:
models = sys.modules[wandb_setup_tool.WandbPublishPayload.__module__]
models = importlib.import_module("measurement_tools.wandb_models")

with pytest.raises(ValidationError, match="Extra inputs"):
models.WandbHistoryPayload(metrics={}, text="Alice")
Expand Down
26 changes: 26 additions & 0 deletions tests/tools/test_measurement_wandb_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from __future__ import annotations

import importlib
import json
import logging
import os
Expand Down Expand Up @@ -514,6 +515,31 @@ def test_wandb_report_markdown_uses_sanitized_fields(wandb_report_tool: ModuleTy
assert "sk-secret-token" not in markdown


def test_wandb_report_save_uses_auth_preflight_fallback(monkeypatch: pytest.MonkeyPatch) -> None:
wandb_reports = importlib.import_module("measurement_tools.wandb_reports")

report = SimpleNamespace(save=lambda **_kwargs: (_ for _ in ()).throw(RuntimeError("relogin required")))
fallback_calls: list[tuple[Any, bool]] = []

def fallback(value: Any, *, draft: bool) -> str:
fallback_calls.append((value, draft))
return "saved"

monkeypatch.setattr(wandb_reports, "_save_report_without_project_preflight", fallback)

assert wandb_reports.save_report(report, draft=True) == "saved"
assert fallback_calls == [(report, True)]


def test_wandb_report_save_preserves_unrelated_sdk_errors() -> None:
wandb_reports = importlib.import_module("measurement_tools.wandb_reports")

report = SimpleNamespace(save=lambda **_kwargs: (_ for _ in ()).throw(RuntimeError("permission denied")))

with pytest.raises(RuntimeError, match="permission denied"):
wandb_reports.save_report(report, draft=False)


def test_wandb_report_views_parse_v1_and_v2_with_explicit_axes(wandb_report_tool: ModuleType) -> None:
summary, v1_config = _wandb_report_fixture()
path = wandb_report_tool.WandbRunPath(entity="entity", project="project", run_id="run-a")
Expand Down
Loading
Loading