Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 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
7 changes: 7 additions & 0 deletions .github/workflows/code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ jobs:
- name: Ruff Format
run: uv run ruff format --check .

- name: Guard against defensive getattr on typed config/backend
run: |
if grep -rnE 'getattr\((config|cfg|self\.config|self\.cfg|backend|self\.backend|self\._backend)\s*,\s*"[a-zA-Z_][a-zA-Z0-9_]*"\s*,' src/raitap --include='*.py' | grep -v '/tests/'; then
echo "::error::Defensive getattr on typed config/backend. AppConfig fields and ModelBackend attrs are declared — read them directly (config.x / backend.x) so a missing field fails loud. See docs/contributor/adr.md."
exit 1
fi

- name: Pyright
run: uv run pyright

Expand Down
10 changes: 10 additions & 0 deletions docs/contributor/adr.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ The *why* behind the layout in {doc}`architecture`. Each record has the same fou

**Consequences:** Zero-config for plugin authors. Each plugin loads under its own try/except so one failure logs and continues. The version check reuses the plugin's pip `Requires-Dist` raitap specifier against `raitap.__about__`; a mismatch warns and skips. See {doc}`writing-a-plugin`.

## Config access is typed, not defensive

**Context:** `AppConfig` is a Hydra structured config; every field has a declared type and default, and `ModelBackend` declares `provides` / `task_kind` / `device` on the ABC. Yet prod code read them through `getattr(config, "x", default)` at ~56 sites, driven by tests injecting `SimpleNamespace` fakes that lacked fields. A real missing-field bug returned a silent default instead of failing loud.

**Options:** Keep defensive `getattr`; commit to direct typed access.

**Decision:** Direct attribute access (`config.transparency`, `backend.provides`). Test configs build via `make_app_config` (a real struct-mode `DictConfig`, the same shape Hydra hands prod), never `SimpleNamespace`; backend fakes build via `make_fake_backend`. Where a value is *genuinely* optional (an assessor may run with no backend), handle the `None` explicitly at the call site — `backend.provides if backend is not None else frozenset()` — not with a `getattr` default that also masks a truly missing attribute.

**Consequences:** A missing field fails loud instead of returning a silent default. A CI guard (`Quality Gate`) bans defensive `getattr` on config/backend in `src/**`. Genuinely dynamic reflection (`getattr(obj, variable)`) and explicit `None`-handling stay in scope of normal code — the ban targets only the string-literal-plus-default shape. The type system becomes load-bearing instead of decoration.

## Cross-cutting principles

The threads that recur across the records above:
Expand Down
7 changes: 5 additions & 2 deletions src/raitap/_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
from collections.abc import Iterator, Sequence
from types import ModuleType

from raitap.models.base_backend import ModelBackend
from raitap.types import Capability
Comment thread
stanlrt marked this conversation as resolved.
Resolved

CtxT = TypeVar("CtxT", contravariant=True)
Expand Down Expand Up @@ -195,15 +196,17 @@ def required_capabilities(self) -> frozenset[Capability]:
hints = registry.get(getattr(self, "algorithm", ""))
return getattr(hints, "requires", frozenset())

def check_backend_compat(self, backend: object) -> None:
def check_backend_compat(self, backend: ModelBackend | None) -> None:
"""Default backend gate: every required capability must be provided.

An absent backend provides nothing (empty capability set).

Override only for a non-capability contract (e.g. ``MarabouAssessor`` uses
the hook for per-call setup; ``AutoLiRPAAssessor`` adds an XPU warning).
"""
from raitap.utils.errors import BackendIncompatibilityError

provided = getattr(backend, "provides", frozenset())
provided = backend.provides if backend is not None else frozenset()
missing = self.required_capabilities() - provided
if missing:
raise BackendIncompatibilityError(
Expand Down
4 changes: 2 additions & 2 deletions src/raitap/configs/adapter_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ def resolve_per_image_transform(
if resolved_preprocessing is not None:
return module_as_per_image_callable(resolved_preprocessing.data_module)

model_cfg = getattr(config, "model", None)
data_cfg = getattr(config, "data", None)
model_cfg = config.model
data_cfg = config.data
if model_cfg is None or data_cfg is None:
return None
resolved = resolve_preprocessing(model_cfg, data_cfg)
Expand Down
13 changes: 5 additions & 8 deletions src/raitap/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def _load_data(
)

inputs_cfg = _get_optional_config_value(cfg.data, "inputs")
model_cfg_for_text = getattr(cfg, "model", None)
model_cfg_for_text = cfg.model
tokenizer_id = _get_optional_config_value(model_cfg_for_text, "tokenizer")
if inputs_cfg is not None and tokenizer_id is not None:
try:
Expand Down Expand Up @@ -153,12 +153,9 @@ def _load_data(
if resolved_preprocessing is not None:
data_module = resolved_preprocessing.data_module
else:
model_cfg = getattr(cfg, "model", None)
if model_cfg is None:
data_module = None
else:
resolved = resolve_preprocessing(model_cfg, cfg.data)
data_module = resolved.data_module
model_cfg = cfg.model
resolved = resolve_preprocessing(model_cfg, cfg.data)
data_module = resolved.data_module
per_image_transform = module_as_per_image_callable(data_module)

is_detection = self.task_kind is TaskKind.detection
Expand Down Expand Up @@ -327,7 +324,7 @@ def _resolve_and_parse_labels(
)

data_source = _get_optional_config_value(cfg.data, "source")
model = getattr(cfg, "model", None)
model = cfg.model
class_names = _get_optional_config_value(model, "class_names")

return parser.parse(
Expand Down
18 changes: 11 additions & 7 deletions src/raitap/data/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

from dataclasses import dataclass
from pathlib import Path
from typing import Any
from typing import TYPE_CHECKING, Any

from raitap.configs import cfg_to_dict
from raitap.data.types import MODALITY_EXTENSIONS, InputModality

from .samples import SAMPLE_SOURCES

if TYPE_CHECKING:
from raitap.configs.schema import AppConfig

Comment thread
stanlrt marked this conversation as resolved.
Resolved
_IMAGE_EXTENSIONS = MODALITY_EXTENSIONS[InputModality.image]
_TABULAR_EXTENSIONS = MODALITY_EXTENSIONS[InputModality.tabular]

Expand All @@ -22,10 +25,10 @@ class DataInputMetadata:
metadata: dict[str, Any] | None = None


def infer_data_input_metadata(config: object, data: object) -> DataInputMetadata:
def infer_data_input_metadata(config: AppConfig, data: object) -> DataInputMetadata:
explicit = getattr(data, "input_metadata", None)
if explicit is None:
explicit = getattr(getattr(config, "data", None), "input_metadata", None)
explicit = config.data.input_metadata
if explicit is not None:
explicit_mapping = cfg_to_dict(explicit)
kind = explicit_mapping.get("kind")
Expand All @@ -51,10 +54,11 @@ def infer_data_input_metadata(config: object, data: object) -> DataInputMetadata
return DataInputMetadata(kind="text", shape=shape, layout="TOKENS")

# Fallback for callers with no recorded modality (e.g. direct/test use):
# sniff the source path as before.
source = str(
getattr(data, "source", None) or getattr(getattr(config, "data", None), "source", "")
)
# sniff the source path as before. ``data`` is a loosely-typed runtime
# object here (tests may pass fakes without a ``source`` attribute), so
# that half stays defensive; ``config.data.source`` is a real schema
# field and always resolves.
source = str(getattr(data, "source", None) or config.data.source)
if is_image_source(source):
return DataInputMetadata(kind="image", shape=shape, layout="NCHW")
if is_tabular_source(source):
Expand Down
144 changes: 20 additions & 124 deletions src/raitap/data/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def _write_csv(path: Path, rows: int = 4, cols: int = 5) -> None:
path.write_text(f"{header}\n{data_rows}")


def _simple_cfg(source: str, name: str = "isic2018") -> AppConfig:
"""A minimal real ``AppConfig`` (``model`` uses its schema defaults)."""
from raitap.configs.schema import AppConfig, DataConfig

return AppConfig(data=DataConfig(source=source, name=name))


# ---------------------------------------------------------------------------
# get_source_path
# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -489,100 +496,51 @@ class TestLoadData:
def test_local_image_file(self, tmp_path: Path) -> None:
p = tmp_path / "img.png"
_write_image(p)
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{"data": type("DataConfig", (), {"source": str(p), "name": "isic2018"})},
)(),
)
cfg = _simple_cfg(str(p))
data = Data(cfg)
assert isinstance(data.tensor, torch.Tensor)
assert data.tensor.shape == (1, 3, 32, 32)

def test_local_image_directory(self, tmp_path: Path) -> None:
for i in range(2):
_write_image(tmp_path / f"img{i}.jpg")
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{"data": type("DataConfig", (), {"source": str(tmp_path), "name": "isic2018"})},
)(),
)
cfg = _simple_cfg(str(tmp_path))
data = Data(cfg)
assert isinstance(data.tensor, torch.Tensor)
assert data.tensor.shape[0] == 2

def test_local_csv_file(self, tmp_path: Path) -> None:
p = tmp_path / "data.csv"
_write_csv(p, rows=6, cols=3)
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{"data": type("DataConfig", (), {"source": str(p), "name": "isic2018"})},
)(),
)
cfg = _simple_cfg(str(p))
data = Data(cfg)
assert isinstance(data.tensor, torch.Tensor)
assert data.tensor.shape == (6, 3)

def test_local_tabular_directory(self, tmp_path: Path) -> None:
_write_csv(tmp_path / "a.csv", rows=2, cols=3)
_write_csv(tmp_path / "b.csv", rows=4, cols=3)
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{"data": type("DataConfig", (), {"source": str(tmp_path), "name": "isic2018"})},
)(),
)
cfg = _simple_cfg(str(tmp_path))
data = Data(cfg)
assert isinstance(data.tensor, torch.Tensor)
assert data.tensor.shape == (6, 3)

def test_mixed_directory_raises(self, tmp_path: Path) -> None:
_write_image(tmp_path / "img.jpg")
_write_csv(tmp_path / "data.csv")
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{"data": type("DataConfig", (), {"source": str(tmp_path), "name": "isic2018"})},
)(),
)
cfg = _simple_cfg(str(tmp_path))
with pytest.raises(ValueError, match="both image and tabular"):
Data(cfg)

def test_empty_directory_raises(self, tmp_path: Path) -> None:
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{"data": type("DataConfig", (), {"source": str(tmp_path), "name": "isic2018"})},
)(),
)
cfg = _simple_cfg(str(tmp_path))
with pytest.raises(FileNotFoundError, match="No supported files"):
Data(cfg)

def test_unknown_extension_raises(self, tmp_path: Path) -> None:
p = tmp_path / "data.xyz"
p.write_text("something")
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{"data": type("DataConfig", (), {"source": str(p), "name": "isic2018"})},
)(),
)
cfg = _simple_cfg(str(p))
with pytest.raises(ValueError, match="Cannot infer data type"):
Data(cfg)

Expand Down Expand Up @@ -629,50 +587,19 @@ def forward(self, batch: torch.Tensor) -> torch.Tensor:
def test_tabular_no_data_module_passes_through(self, tmp_path: Path) -> None:
p = tmp_path / "rows.csv"
_write_csv(p, rows=2, cols=3)
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{"data": type("DataConfig", (), {"source": str(p), "name": "tab"})},
)(),
)
cfg = _simple_cfg(str(p), name="tab")
data = Data(cfg)
torch.testing.assert_close(data.tensor, _load_tabular(p))

def test_invalid_source_raises(self) -> None:
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{
"data": type(
"DataConfig", (), {"source": "/no/such/path/file.csv", "name": "isic2018"}
)
},
)(),
)
cfg = _simple_cfg("/no/such/path/file.csv")
with pytest.raises(ValueError, match="could not be resolved"):
Data(cfg)

def test_url_source_loads_csv_via_get_source_path(self, tmp_path: Path) -> None:
p = tmp_path / "remote.csv"
_write_csv(p, rows=2, cols=3)
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{
"data": type(
"DataConfig",
(),
{"source": "https://example.com/remote.csv", "name": "isic2018"},
)
},
)(),
)
cfg = _simple_cfg("https://example.com/remote.csv")
with patch("raitap.data.data.get_source_path", return_value=p):
data = Data(cfg)
assert isinstance(data.tensor, torch.Tensor)
Expand All @@ -681,20 +608,7 @@ def test_url_source_loads_csv_via_get_source_path(self, tmp_path: Path) -> None:
def test_url_source_loads_image_via_get_source_path(self, tmp_path: Path) -> None:
p = tmp_path / "remote.png"
_write_image(p)
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{
"data": type(
"DataConfig",
(),
{"source": "https://example.com/remote.png", "name": "isic2018"},
)
},
)(),
)
cfg = _simple_cfg("https://example.com/remote.png")
with patch("raitap.data.data.get_source_path", return_value=p):
data = Data(cfg)
assert isinstance(data.tensor, torch.Tensor)
Expand Down Expand Up @@ -742,18 +656,7 @@ class TestDescribeData:
def test_describe_data_includes_shape_dtype_and_sample_shape(self, tmp_path: Path) -> None:
img_file = tmp_path / "img.jpg"
_write_image(img_file, 32, 32)
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{
"data": type(
"DataConfig", (), {"source": str(img_file), "name": "imagenet_samples"}
)
},
)(),
)
cfg = _simple_cfg(str(img_file), name="imagenet_samples")
data = Data(cfg)
data.source = "/tmp/imagenet"

Expand All @@ -769,14 +672,7 @@ def test_describe_data_includes_shape_dtype_and_sample_shape(self, tmp_path: Pat
def test_describe_data_without_sample_shape_for_1d_input(self, tmp_path: Path) -> None:
csv_file = tmp_path / "data.csv"
csv_file.write_text("a\n1\n2\n3\n4\n5\n6\n7\n8")
cfg = cast(
"AppConfig",
type(
"AppConfig",
(),
{"data": type("DataConfig", (), {"source": str(csv_file), "name": "vector_data"})},
)(),
)
cfg = _simple_cfg(str(csv_file), name="vector_data")
data = Data(cfg)

info = data.describe()
Expand Down
Loading
Loading