|
| 1 | +"""V3 refactor guardrails. |
| 2 | +
|
| 3 | +These tests document the intended package boundaries before the implementation |
| 4 | +stages move code. Known current violations are marked strict xfail so later |
| 5 | +stages must either fix and un-xfail them or keep the debt visible. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import ast |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +REPO_ROOT = Path(__file__).resolve().parents[2] |
| 16 | + |
| 17 | + |
| 18 | +def _module_name_for_path(path: Path) -> str: |
| 19 | + relative = path.relative_to(REPO_ROOT).with_suffix("") |
| 20 | + parts = list(relative.parts) |
| 21 | + if parts[-1] == "__init__": |
| 22 | + parts = parts[:-1] |
| 23 | + return ".".join(parts) |
| 24 | + |
| 25 | + |
| 26 | +def _resolve_import_from(module_name: str, node: ast.ImportFrom) -> str: |
| 27 | + if node.level == 0: |
| 28 | + return node.module or "" |
| 29 | + |
| 30 | + package_parts = module_name.split(".") |
| 31 | + if path_name := package_parts[-1]: |
| 32 | + if path_name != "__init__": |
| 33 | + package_parts = package_parts[:-1] |
| 34 | + base_parts = package_parts[: max(0, len(package_parts) - node.level + 1)] |
| 35 | + if node.module: |
| 36 | + base_parts.extend(node.module.split(".")) |
| 37 | + return ".".join(base_parts) |
| 38 | + |
| 39 | + |
| 40 | +def _forbidden_imports(root: Path, forbidden_prefixes: tuple[str, ...]) -> list[str]: |
| 41 | + violations: list[str] = [] |
| 42 | + for path in sorted(root.rglob("*.py")): |
| 43 | + module_name = _module_name_for_path(path) |
| 44 | + tree = ast.parse(path.read_text(), filename=str(path)) |
| 45 | + for node in ast.walk(tree): |
| 46 | + imported_modules: list[str] = [] |
| 47 | + if isinstance(node, ast.Import): |
| 48 | + imported_modules = [alias.name for alias in node.names] |
| 49 | + elif isinstance(node, ast.ImportFrom): |
| 50 | + imported_modules = [_resolve_import_from(module_name, node)] |
| 51 | + |
| 52 | + for imported_module in imported_modules: |
| 53 | + if imported_module.startswith(forbidden_prefixes): |
| 54 | + rel = path.relative_to(REPO_ROOT) |
| 55 | + violations.append(f"{rel}:{node.lineno}: {imported_module}") |
| 56 | + return violations |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.xfail(strict=True, reason="V3 PR 2/5 removes decoding -> training imports") |
| 60 | +def test_decoding_static_imports_do_not_reference_training(): |
| 61 | + violations = _forbidden_imports( |
| 62 | + REPO_ROOT / "connectomics" / "decoding", |
| 63 | + ("connectomics.training",), |
| 64 | + ) |
| 65 | + assert violations == [] |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.xfail(strict=True, reason="V3 PR 6 moves streamed chunk decoding out of inference") |
| 69 | +def test_inference_static_imports_do_not_reference_decoding(): |
| 70 | + violations = _forbidden_imports( |
| 71 | + REPO_ROOT / "connectomics" / "inference", |
| 72 | + ("connectomics.decoding",), |
| 73 | + ) |
| 74 | + assert violations == [] |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.xfail(strict=True, reason="V3 PR 7 moves data-aware validation out of config") |
| 78 | +def test_config_static_imports_do_not_reference_data_execution(): |
| 79 | + violations = _forbidden_imports( |
| 80 | + REPO_ROOT / "connectomics" / "config", |
| 81 | + ("connectomics.data",), |
| 82 | + ) |
| 83 | + assert violations == [] |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.xfail(strict=True, reason="V3 PR 3 makes unknown top-level keys hard errors") |
| 87 | +def test_config_load_raises_on_unknown_top_level_key(tmp_path): |
| 88 | + from connectomics.config import load_config |
| 89 | + |
| 90 | + config_yaml = tmp_path / "unknown_key.yaml" |
| 91 | + config_yaml.write_text("unknown_section: {}\n") |
| 92 | + |
| 93 | + with pytest.raises(ValueError, match="unknown_section"): |
| 94 | + load_config(config_yaml) |
| 95 | + |
| 96 | + |
| 97 | +def test_connectomics_config_public_api_snapshot(): |
| 98 | + import connectomics.config as config |
| 99 | + |
| 100 | + assert set(config.__all__) == { |
| 101 | + "Config", |
| 102 | + "load_config", |
| 103 | + "save_config", |
| 104 | + "merge_configs", |
| 105 | + "update_from_cli", |
| 106 | + "to_dict", |
| 107 | + "from_dict", |
| 108 | + "print_config", |
| 109 | + "validate_config", |
| 110 | + "get_config_hash", |
| 111 | + "create_experiment_name", |
| 112 | + "resolve_data_paths", |
| 113 | + "resolve_default_profiles", |
| 114 | + "to_plain", |
| 115 | + "as_plain_dict", |
| 116 | + "cfg_get", |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | +def test_connectomics_inference_public_api_snapshot(): |
| 121 | + import connectomics.inference as inference |
| 122 | + |
| 123 | + assert set(inference.__all__) == { |
| 124 | + "InferenceManager", |
| 125 | + "PredictionArtifactMetadata", |
| 126 | + "read_prediction_artifact", |
| 127 | + "write_prediction_artifact", |
| 128 | + "write_prediction_artifact_attrs", |
| 129 | + "run_prediction_inference", |
| 130 | + "is_chunked_inference_enabled", |
| 131 | + "run_chunked_affinity_cc_inference", |
| 132 | + "run_chunked_prediction_inference", |
| 133 | + "apply_prediction_transform", |
| 134 | + "apply_storage_dtype_transform", |
| 135 | + "resolve_output_filenames", |
| 136 | + "write_outputs", |
| 137 | + "build_sliding_inferer", |
| 138 | + "resolve_inferer_roi_size", |
| 139 | + "resolve_inferer_overlap", |
| 140 | + "is_2d_inference_mode", |
| 141 | + "TTAPredictor", |
| 142 | + } |
0 commit comments