reframx 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- reframx/__init__.py +180 -0
- reframx/api/__init__.py +193 -0
- reframx/core/__init__.py +1 -0
- reframx/core/components/__init__.py +5 -0
- reframx/core/components/handle.py +87 -0
- reframx/core/components/metadata.py +33 -0
- reframx/core/components/protocols.py +47 -0
- reframx/core/components/specification.py +50 -0
- reframx/core/contracts/__init__.py +5 -0
- reframx/core/contracts/capabilities.py +58 -0
- reframx/core/contracts/compatibility.py +574 -0
- reframx/core/contracts/io.py +42 -0
- reframx/core/contracts/requirements.py +115 -0
- reframx/core/exceptions.py +25 -0
- reframx/core/registry/__init__.py +27 -0
- reframx/core/registry/plugins.py +478 -0
- reframx/core/registry/registry.py +186 -0
- reframx/core/types.py +14 -0
- reframx/core/validation/__init__.py +15 -0
- reframx/core/validation/context.py +22 -0
- reframx/core/validation/report.py +80 -0
- reframx/core/validation/runner.py +41 -0
- reframx/datasets/__init__.py +10 -0
- reframx/datasets/adapters/__init__.py +5 -0
- reframx/datasets/adapters/field_mapping.py +179 -0
- reframx/datasets/interface.py +79 -0
- reframx/datasets/specification.py +31 -0
- reframx/evaluators/__init__.py +11 -0
- reframx/evaluators/interface.py +88 -0
- reframx/execution/__init__.py +14 -0
- reframx/execution/backend.py +73 -0
- reframx/execution/context.py +92 -0
- reframx/execution/engine.py +257 -0
- reframx/execution/hooks/__init__.py +6 -0
- reframx/execution/hooks/base.py +46 -0
- reframx/experiments/__init__.py +8 -0
- reframx/experiments/experiment.py +232 -0
- reframx/experiments/run.py +63 -0
- reframx/instrumentation/__init__.py +35 -0
- reframx/instrumentation/events.py +60 -0
- reframx/instrumentation/manager.py +365 -0
- reframx/instrumentation/module_hooks.py +185 -0
- reframx/instrumentation/probes.py +142 -0
- reframx/integrations/__init__.py +1 -0
- reframx/integrations/mmengine/__init__.py +15 -0
- reframx/integrations/mmengine/backend.py +236 -0
- reframx/integrations/mmengine/bridges.py +186 -0
- reframx/integrations/mmengine/strategy.py +101 -0
- reframx/integrations/mmengine/utils.py +42 -0
- reframx/integrations/pytorch/__init__.py +45 -0
- reframx/integrations/pytorch/dataset.py +78 -0
- reframx/integrations/pytorch/evaluators.py +115 -0
- reframx/integrations/pytorch/methods.py +160 -0
- reframx/integrations/pytorch/model.py +95 -0
- reframx/integrations/pytorch/objectives.py +149 -0
- reframx/integrations/pytorch/strategy.py +190 -0
- reframx/integrations/pytorch/utils.py +80 -0
- reframx/methods/__init__.py +13 -0
- reframx/methods/interface.py +138 -0
- reframx/methods/specification.py +24 -0
- reframx/models/__init__.py +8 -0
- reframx/models/interface.py +114 -0
- reframx/models/specification.py +28 -0
- reframx/objectives/__init__.py +6 -0
- reframx/objectives/interface.py +76 -0
- reframx/packaging/__init__.py +38 -0
- reframx/packaging/archive.py +195 -0
- reframx/packaging/exporter.py +206 -0
- reframx/packaging/importer.py +186 -0
- reframx/packaging/manifest.py +166 -0
- reframx/py.typed +0 -0
- reframx/strategies/__init__.py +11 -0
- reframx/strategies/interface.py +186 -0
- reframx/tasks/__init__.py +23 -0
- reframx/tasks/builtin/__init__.py +11 -0
- reframx/tasks/builtin/common.py +137 -0
- reframx/tasks/definitions/__init__.py +9 -0
- reframx/tasks/definitions/evaluation.py +20 -0
- reframx/tasks/definitions/objectives.py +25 -0
- reframx/tasks/definitions/problem.py +10 -0
- reframx/tasks/interface.py +57 -0
- reframx/tasks/specification.py +65 -0
- reframx-0.1.1.dist-info/METADATA +134 -0
- reframx-0.1.1.dist-info/RECORD +87 -0
- reframx-0.1.1.dist-info/WHEEL +5 -0
- reframx-0.1.1.dist-info/licenses/LICENSE +21 -0
- reframx-0.1.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
"""Generic compatibility checks between component requirements and capabilities.
|
|
2
|
+
|
|
3
|
+
A component declares what it requires through ``Requirement`` objects and
|
|
4
|
+
``RequirementGroup`` objects. Other components declare what they provide
|
|
5
|
+
through ``Capability`` objects. This module only matches these contracts; it
|
|
6
|
+
does not encode fixed relationships such as Task -> Method or Method -> Model.
|
|
7
|
+
|
|
8
|
+
Supported requirement forms
|
|
9
|
+
---------------------------
|
|
10
|
+
* ``Requirement``: one capability must be provided.
|
|
11
|
+
* ``RequirementGroup(mode="all_of")``: every child requirement must pass.
|
|
12
|
+
* ``RequirementGroup(mode="any_of")``: at least one child requirement must pass.
|
|
13
|
+
|
|
14
|
+
If a requirement has an ``IOContract`` in its optional ``contract`` attribute,
|
|
15
|
+
the checker compares its declared fields with the provider capability's
|
|
16
|
+
contract. The attribute is accessed dynamically so this module remains usable
|
|
17
|
+
while the IOContract model is being introduced.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
from collections.abc import Iterable, Mapping
|
|
23
|
+
from dataclasses import dataclass
|
|
24
|
+
from typing import Any, Literal
|
|
25
|
+
|
|
26
|
+
from reframx.core.components.specification import ComponentSpec
|
|
27
|
+
from reframx.core.contracts.requirements import Requirement, RequirementGroup
|
|
28
|
+
from reframx.core.validation.report import (
|
|
29
|
+
ValidationIssue,
|
|
30
|
+
ValidationReport,
|
|
31
|
+
ValidationSeverity,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True, slots=True)
|
|
36
|
+
class _ContractComparison:
|
|
37
|
+
"""Result of comparing one required contract with one provided contract."""
|
|
38
|
+
|
|
39
|
+
mismatches: tuple[str, ...] = ()
|
|
40
|
+
unknowns: tuple[str, ...] = ()
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def is_compatible(self) -> bool:
|
|
44
|
+
"""Whether no definite contradiction was found."""
|
|
45
|
+
return not self.mismatches
|
|
46
|
+
|
|
47
|
+
@property
|
|
48
|
+
def is_verified(self) -> bool:
|
|
49
|
+
"""Whether the contracts match and every requested field was declared."""
|
|
50
|
+
return self.is_compatible and not self.unknowns
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@dataclass(frozen=True, slots=True)
|
|
54
|
+
class _RequirementResult:
|
|
55
|
+
"""Internal result used when evaluating requirement groups."""
|
|
56
|
+
|
|
57
|
+
status: Literal["satisfied", "unverified", "unsatisfied"]
|
|
58
|
+
report: ValidationReport
|
|
59
|
+
details: Mapping[str, Any]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _read_contract_value(contract: Any, field_name: str) -> Any:
|
|
63
|
+
"""Read a contract field from either a mapping or an object."""
|
|
64
|
+
if contract is None:
|
|
65
|
+
return None
|
|
66
|
+
if isinstance(contract, Mapping):
|
|
67
|
+
return contract.get(field_name)
|
|
68
|
+
return getattr(contract, field_name, None)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _compare_shape(
|
|
72
|
+
required_shape: Any,
|
|
73
|
+
provided_shape: Any,
|
|
74
|
+
) -> tuple[tuple[str, ...], tuple[str, ...]]:
|
|
75
|
+
"""Compare shapes and return ``(mismatches, unknowns)``.
|
|
76
|
+
|
|
77
|
+
Integer dimensions are concrete and must match. String dimensions are
|
|
78
|
+
symbolic (for example ``"B"`` or ``"T"``) and act as wildcards. A
|
|
79
|
+
provider-side ``None`` or symbolic dimension cannot prove a concrete
|
|
80
|
+
required dimension, so that comparison is marked unknown.
|
|
81
|
+
"""
|
|
82
|
+
try:
|
|
83
|
+
required_dims = tuple(required_shape)
|
|
84
|
+
provided_dims = tuple(provided_shape)
|
|
85
|
+
except TypeError:
|
|
86
|
+
return (("shape must be an iterable of dimensions",), ())
|
|
87
|
+
|
|
88
|
+
if len(required_dims) != len(provided_dims):
|
|
89
|
+
return (
|
|
90
|
+
(
|
|
91
|
+
f"rank mismatch: required rank {len(required_dims)}, "
|
|
92
|
+
f"provided rank {len(provided_dims)}",
|
|
93
|
+
),
|
|
94
|
+
(),
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
mismatches: list[str] = []
|
|
98
|
+
unknowns: list[str] = []
|
|
99
|
+
|
|
100
|
+
for index, (required_dim, provided_dim) in enumerate(
|
|
101
|
+
zip(required_dims, provided_dims, strict=True)
|
|
102
|
+
):
|
|
103
|
+
required_is_concrete = isinstance(required_dim, int) and not isinstance(
|
|
104
|
+
required_dim, bool
|
|
105
|
+
)
|
|
106
|
+
provided_is_concrete = isinstance(provided_dim, int) and not isinstance(
|
|
107
|
+
provided_dim, bool
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
if required_is_concrete:
|
|
111
|
+
if provided_is_concrete:
|
|
112
|
+
if required_dim != provided_dim:
|
|
113
|
+
mismatches.append(
|
|
114
|
+
f"dimension {index} mismatch: required {required_dim}, "
|
|
115
|
+
f"provided {provided_dim}"
|
|
116
|
+
)
|
|
117
|
+
elif provided_dim is None or isinstance(provided_dim, str):
|
|
118
|
+
unknowns.append(
|
|
119
|
+
f"provider dimension {index} is symbolic or unspecified; "
|
|
120
|
+
f"cannot verify required size {required_dim}"
|
|
121
|
+
)
|
|
122
|
+
else:
|
|
123
|
+
mismatches.append(
|
|
124
|
+
f"provider dimension {index} has unsupported value "
|
|
125
|
+
f"{provided_dim!r}"
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
return tuple(mismatches), tuple(unknowns)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _compare_io_contracts(
|
|
132
|
+
requirement_contract: Any,
|
|
133
|
+
capability_contract: Any,
|
|
134
|
+
) -> _ContractComparison:
|
|
135
|
+
"""Compare fields understood by the generic IO contract.
|
|
136
|
+
|
|
137
|
+
Fields omitted by the requirement impose no constraint. If the requirement
|
|
138
|
+
specifies a field but the provider does not declare it, the comparison is
|
|
139
|
+
unknown rather than a definite mismatch. ``strict_contract=True`` turns
|
|
140
|
+
such unknowns into validation errors at the call site.
|
|
141
|
+
"""
|
|
142
|
+
if requirement_contract is None:
|
|
143
|
+
return _ContractComparison()
|
|
144
|
+
|
|
145
|
+
mismatches: list[str] = []
|
|
146
|
+
unknowns: list[str] = []
|
|
147
|
+
|
|
148
|
+
for field_name in ("kind", "value_type", "semantics", "dtype"):
|
|
149
|
+
required_value = _read_contract_value(requirement_contract, field_name)
|
|
150
|
+
if required_value is None:
|
|
151
|
+
continue
|
|
152
|
+
|
|
153
|
+
provided_value = _read_contract_value(capability_contract, field_name)
|
|
154
|
+
if provided_value is None:
|
|
155
|
+
unknowns.append(f"provider did not declare '{field_name}'")
|
|
156
|
+
elif provided_value != required_value:
|
|
157
|
+
mismatches.append(
|
|
158
|
+
f"{field_name} mismatch: required {required_value!r}, "
|
|
159
|
+
f"provided {provided_value!r}"
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
required_shape = _read_contract_value(requirement_contract, "shape")
|
|
163
|
+
if required_shape is not None:
|
|
164
|
+
provided_shape = _read_contract_value(capability_contract, "shape")
|
|
165
|
+
if provided_shape is None:
|
|
166
|
+
unknowns.append("provider did not declare 'shape'")
|
|
167
|
+
else:
|
|
168
|
+
shape_mismatches, shape_unknowns = _compare_shape(
|
|
169
|
+
required_shape,
|
|
170
|
+
provided_shape,
|
|
171
|
+
)
|
|
172
|
+
mismatches.extend(shape_mismatches)
|
|
173
|
+
unknowns.extend(shape_unknowns)
|
|
174
|
+
|
|
175
|
+
required_attributes = _read_contract_value(
|
|
176
|
+
requirement_contract,
|
|
177
|
+
"attributes",
|
|
178
|
+
) or {}
|
|
179
|
+
provided_attributes = _read_contract_value(
|
|
180
|
+
capability_contract,
|
|
181
|
+
"attributes",
|
|
182
|
+
) or {}
|
|
183
|
+
|
|
184
|
+
if not isinstance(required_attributes, Mapping):
|
|
185
|
+
mismatches.append("required contract 'attributes' must be a mapping")
|
|
186
|
+
elif not isinstance(provided_attributes, Mapping):
|
|
187
|
+
unknowns.append("provider contract 'attributes' is not a mapping")
|
|
188
|
+
else:
|
|
189
|
+
for key, required_value in required_attributes.items():
|
|
190
|
+
if key not in provided_attributes:
|
|
191
|
+
unknowns.append(f"provider did not declare attribute '{key}'")
|
|
192
|
+
elif provided_attributes[key] != required_value:
|
|
193
|
+
mismatches.append(
|
|
194
|
+
f"attribute '{key}' mismatch: required {required_value!r}, "
|
|
195
|
+
f"provided {provided_attributes[key]!r}"
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
return _ContractComparison(
|
|
199
|
+
mismatches=tuple(mismatches),
|
|
200
|
+
unknowns=tuple(unknowns),
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _make_issue(
|
|
205
|
+
*,
|
|
206
|
+
consumer: ComponentSpec,
|
|
207
|
+
code: str,
|
|
208
|
+
message: str,
|
|
209
|
+
severity: ValidationSeverity,
|
|
210
|
+
location: str | None = None,
|
|
211
|
+
details: Mapping[str, Any] | None = None,
|
|
212
|
+
) -> ValidationIssue:
|
|
213
|
+
"""Create a standardized issue associated with the consuming component."""
|
|
214
|
+
return ValidationIssue(
|
|
215
|
+
severity=severity,
|
|
216
|
+
code=code,
|
|
217
|
+
message=message,
|
|
218
|
+
component=consumer.metadata.name,
|
|
219
|
+
location=location,
|
|
220
|
+
details=dict(details or {}),
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _evaluate_requirement(
|
|
225
|
+
*,
|
|
226
|
+
consumer: ComponentSpec,
|
|
227
|
+
requirement: Requirement,
|
|
228
|
+
providers: tuple[ComponentSpec, ...],
|
|
229
|
+
strict_contract: bool,
|
|
230
|
+
optional_override: bool = False,
|
|
231
|
+
) -> _RequirementResult:
|
|
232
|
+
"""Evaluate one Requirement and return its report plus an internal status."""
|
|
233
|
+
requirement_name = requirement.name
|
|
234
|
+
expected_provider_type = getattr(requirement, "provider_type", None)
|
|
235
|
+
is_optional = bool(getattr(requirement, "optional", False) or optional_override)
|
|
236
|
+
severity_for_failure = (
|
|
237
|
+
ValidationSeverity.WARNING if is_optional else ValidationSeverity.ERROR
|
|
238
|
+
)
|
|
239
|
+
|
|
240
|
+
named_candidates = [
|
|
241
|
+
(provider, capability)
|
|
242
|
+
for provider in providers
|
|
243
|
+
for capability in provider.capabilities
|
|
244
|
+
if capability.name == requirement_name
|
|
245
|
+
]
|
|
246
|
+
|
|
247
|
+
candidates = [
|
|
248
|
+
(provider, capability)
|
|
249
|
+
for provider, capability in named_candidates
|
|
250
|
+
if expected_provider_type is None
|
|
251
|
+
or provider.metadata.component_type == expected_provider_type
|
|
252
|
+
]
|
|
253
|
+
|
|
254
|
+
if not candidates:
|
|
255
|
+
if named_candidates and expected_provider_type is not None:
|
|
256
|
+
code = "CAPABILITY_PROVIDER_TYPE_MISMATCH"
|
|
257
|
+
reason = (
|
|
258
|
+
f"Capability '{requirement_name}' exists, but not on a provider "
|
|
259
|
+
f"of type '{expected_provider_type}'."
|
|
260
|
+
)
|
|
261
|
+
else:
|
|
262
|
+
code = (
|
|
263
|
+
"OPTIONAL_CAPABILITY_MISSING"
|
|
264
|
+
if is_optional
|
|
265
|
+
else "REQUIRED_CAPABILITY_MISSING"
|
|
266
|
+
)
|
|
267
|
+
if expected_provider_type is None:
|
|
268
|
+
reason = f"No provider offers capability '{requirement_name}'."
|
|
269
|
+
else:
|
|
270
|
+
reason = (
|
|
271
|
+
f"No provider of type '{expected_provider_type}' offers "
|
|
272
|
+
f"capability '{requirement_name}'."
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
if is_optional and code == "CAPABILITY_PROVIDER_TYPE_MISMATCH":
|
|
276
|
+
code = f"OPTIONAL_{code}"
|
|
277
|
+
|
|
278
|
+
report = ValidationReport(
|
|
279
|
+
issues=[
|
|
280
|
+
_make_issue(
|
|
281
|
+
consumer=consumer,
|
|
282
|
+
code=code,
|
|
283
|
+
message=(
|
|
284
|
+
f"Optional requirement '{requirement_name}' is not satisfied. "
|
|
285
|
+
f"{reason}"
|
|
286
|
+
if is_optional
|
|
287
|
+
else f"Required requirement '{requirement_name}' is not satisfied. "
|
|
288
|
+
f"{reason}"
|
|
289
|
+
),
|
|
290
|
+
severity=severity_for_failure,
|
|
291
|
+
location=expected_provider_type,
|
|
292
|
+
details={
|
|
293
|
+
"requirement": requirement_name,
|
|
294
|
+
"provider_type": expected_provider_type,
|
|
295
|
+
"optional": is_optional,
|
|
296
|
+
"constraints": dict(getattr(requirement, "constraints", {})),
|
|
297
|
+
},
|
|
298
|
+
)
|
|
299
|
+
]
|
|
300
|
+
)
|
|
301
|
+
return _RequirementResult(
|
|
302
|
+
status="unsatisfied",
|
|
303
|
+
report=report,
|
|
304
|
+
details={"code": code, "reason": reason},
|
|
305
|
+
)
|
|
306
|
+
|
|
307
|
+
requirement_contract = getattr(requirement, "contract", None)
|
|
308
|
+
comparisons: list[tuple[ComponentSpec, _ContractComparison]] = []
|
|
309
|
+
for provider, capability in candidates:
|
|
310
|
+
comparison = _compare_io_contracts(
|
|
311
|
+
requirement_contract,
|
|
312
|
+
getattr(capability, "contract", None),
|
|
313
|
+
)
|
|
314
|
+
comparisons.append((provider, comparison))
|
|
315
|
+
|
|
316
|
+
verified_candidates = [
|
|
317
|
+
(provider, comparison)
|
|
318
|
+
for provider, comparison in comparisons
|
|
319
|
+
if comparison.is_verified
|
|
320
|
+
]
|
|
321
|
+
unverified_candidates = [
|
|
322
|
+
(provider, comparison)
|
|
323
|
+
for provider, comparison in comparisons
|
|
324
|
+
if comparison.is_compatible and comparison.unknowns
|
|
325
|
+
]
|
|
326
|
+
|
|
327
|
+
if verified_candidates:
|
|
328
|
+
return _RequirementResult(
|
|
329
|
+
status="satisfied",
|
|
330
|
+
report=ValidationReport(),
|
|
331
|
+
details={
|
|
332
|
+
"requirement": requirement_name,
|
|
333
|
+
"providers": [provider.metadata.name for provider, _ in verified_candidates],
|
|
334
|
+
},
|
|
335
|
+
)
|
|
336
|
+
|
|
337
|
+
if unverified_candidates:
|
|
338
|
+
unknown_details = [
|
|
339
|
+
{
|
|
340
|
+
"provider": provider.metadata.name,
|
|
341
|
+
"unknowns": list(comparison.unknowns),
|
|
342
|
+
}
|
|
343
|
+
for provider, comparison in unverified_candidates
|
|
344
|
+
]
|
|
345
|
+
report_severity = (
|
|
346
|
+
ValidationSeverity.WARNING
|
|
347
|
+
if is_optional or not strict_contract
|
|
348
|
+
else ValidationSeverity.ERROR
|
|
349
|
+
)
|
|
350
|
+
report = ValidationReport(
|
|
351
|
+
issues=[
|
|
352
|
+
_make_issue(
|
|
353
|
+
consumer=consumer,
|
|
354
|
+
code="CAPABILITY_CONTRACT_UNVERIFIED",
|
|
355
|
+
message=(
|
|
356
|
+
f"Requirement '{requirement_name}' may be satisfied, but "
|
|
357
|
+
"some contract fields could not be verified."
|
|
358
|
+
),
|
|
359
|
+
severity=report_severity,
|
|
360
|
+
location=expected_provider_type,
|
|
361
|
+
details={
|
|
362
|
+
"requirement": requirement_name,
|
|
363
|
+
"providers": unknown_details,
|
|
364
|
+
"strict_contract": strict_contract,
|
|
365
|
+
},
|
|
366
|
+
)
|
|
367
|
+
]
|
|
368
|
+
)
|
|
369
|
+
return _RequirementResult(
|
|
370
|
+
status="unverified",
|
|
371
|
+
report=report,
|
|
372
|
+
details={
|
|
373
|
+
"requirement": requirement_name,
|
|
374
|
+
"unknowns": unknown_details,
|
|
375
|
+
},
|
|
376
|
+
)
|
|
377
|
+
|
|
378
|
+
mismatch_details = [
|
|
379
|
+
{
|
|
380
|
+
"provider": provider.metadata.name,
|
|
381
|
+
"mismatches": list(comparison.mismatches),
|
|
382
|
+
}
|
|
383
|
+
for provider, comparison in comparisons
|
|
384
|
+
]
|
|
385
|
+
report = ValidationReport(
|
|
386
|
+
issues=[
|
|
387
|
+
_make_issue(
|
|
388
|
+
consumer=consumer,
|
|
389
|
+
code=(
|
|
390
|
+
"OPTIONAL_CAPABILITY_CONTRACT_MISMATCH"
|
|
391
|
+
if is_optional
|
|
392
|
+
else "CAPABILITY_CONTRACT_MISMATCH"
|
|
393
|
+
),
|
|
394
|
+
message=(
|
|
395
|
+
f"Capability '{requirement_name}' was found, but no provider "
|
|
396
|
+
"satisfies its declared contract."
|
|
397
|
+
),
|
|
398
|
+
severity=severity_for_failure,
|
|
399
|
+
location=expected_provider_type,
|
|
400
|
+
details={
|
|
401
|
+
"requirement": requirement_name,
|
|
402
|
+
"candidates": mismatch_details,
|
|
403
|
+
},
|
|
404
|
+
)
|
|
405
|
+
]
|
|
406
|
+
)
|
|
407
|
+
return _RequirementResult(
|
|
408
|
+
status="unsatisfied",
|
|
409
|
+
report=report,
|
|
410
|
+
details={
|
|
411
|
+
"requirement": requirement_name,
|
|
412
|
+
"candidates": mismatch_details,
|
|
413
|
+
},
|
|
414
|
+
)
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
def _validate_requirement_group(
|
|
418
|
+
*,
|
|
419
|
+
consumer: ComponentSpec,
|
|
420
|
+
group: RequirementGroup,
|
|
421
|
+
providers: tuple[ComponentSpec, ...],
|
|
422
|
+
strict_contract: bool,
|
|
423
|
+
) -> ValidationReport:
|
|
424
|
+
"""Validate an all-of or any-of RequirementGroup."""
|
|
425
|
+
group_name = group.name
|
|
426
|
+
mode = group.mode
|
|
427
|
+
group_is_optional = bool(group.optional)
|
|
428
|
+
|
|
429
|
+
results = [
|
|
430
|
+
_evaluate_requirement(
|
|
431
|
+
consumer=consumer,
|
|
432
|
+
requirement=requirement,
|
|
433
|
+
providers=providers,
|
|
434
|
+
strict_contract=strict_contract,
|
|
435
|
+
optional_override=(group_is_optional if mode == "all_of" else False),
|
|
436
|
+
)
|
|
437
|
+
for requirement in group.requirements
|
|
438
|
+
]
|
|
439
|
+
|
|
440
|
+
if mode == "all_of":
|
|
441
|
+
report = ValidationReport()
|
|
442
|
+
for result in results:
|
|
443
|
+
report.extend(result.report)
|
|
444
|
+
return report
|
|
445
|
+
|
|
446
|
+
if mode != "any_of":
|
|
447
|
+
# RequirementGroup should already validate this in __post_init__, but
|
|
448
|
+
# keep the checker defensive against custom or malformed objects.
|
|
449
|
+
raise ValueError(
|
|
450
|
+
f"Unsupported RequirementGroup mode {mode!r}; "
|
|
451
|
+
"expected 'all_of' or 'any_of'."
|
|
452
|
+
)
|
|
453
|
+
|
|
454
|
+
if any(result.status == "satisfied" for result in results):
|
|
455
|
+
# One fully verified alternative is enough. Failed alternatives are not
|
|
456
|
+
# validation errors because they were not selected.
|
|
457
|
+
return ValidationReport()
|
|
458
|
+
|
|
459
|
+
unverified_results = [
|
|
460
|
+
result for result in results if result.status == "unverified"
|
|
461
|
+
]
|
|
462
|
+
if unverified_results:
|
|
463
|
+
severity = (
|
|
464
|
+
ValidationSeverity.WARNING
|
|
465
|
+
if group_is_optional or not strict_contract
|
|
466
|
+
else ValidationSeverity.ERROR
|
|
467
|
+
)
|
|
468
|
+
code = "REQUIREMENT_GROUP_UNVERIFIED"
|
|
469
|
+
message = (
|
|
470
|
+
f"Requirement group '{group_name}' has no fully verified alternative; "
|
|
471
|
+
"at least one alternative may be satisfied but has incomplete contract data."
|
|
472
|
+
)
|
|
473
|
+
details = {
|
|
474
|
+
"group": group_name,
|
|
475
|
+
"mode": mode,
|
|
476
|
+
"alternatives": [dict(result.details) for result in results],
|
|
477
|
+
"strict_contract": strict_contract,
|
|
478
|
+
}
|
|
479
|
+
else:
|
|
480
|
+
severity = (
|
|
481
|
+
ValidationSeverity.WARNING if group_is_optional else ValidationSeverity.ERROR
|
|
482
|
+
)
|
|
483
|
+
code = "OPTIONAL_REQUIREMENT_GROUP_UNSATISFIED" if group_is_optional else (
|
|
484
|
+
"REQUIREMENT_GROUP_UNSATISFIED"
|
|
485
|
+
)
|
|
486
|
+
message = (
|
|
487
|
+
f"No alternative in requirement group '{group_name}' was satisfied."
|
|
488
|
+
)
|
|
489
|
+
details = {
|
|
490
|
+
"group": group_name,
|
|
491
|
+
"mode": mode,
|
|
492
|
+
"alternatives": [dict(result.details) for result in results],
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return ValidationReport(
|
|
496
|
+
issues=[
|
|
497
|
+
_make_issue(
|
|
498
|
+
consumer=consumer,
|
|
499
|
+
code=code,
|
|
500
|
+
message=message,
|
|
501
|
+
severity=severity,
|
|
502
|
+
location=group_name,
|
|
503
|
+
details=details,
|
|
504
|
+
)
|
|
505
|
+
]
|
|
506
|
+
)
|
|
507
|
+
|
|
508
|
+
|
|
509
|
+
def validate_requirements(
|
|
510
|
+
consumer: ComponentSpec,
|
|
511
|
+
providers: Iterable[ComponentSpec],
|
|
512
|
+
*,
|
|
513
|
+
strict_contract: bool = False,
|
|
514
|
+
) -> ValidationReport:
|
|
515
|
+
"""Validate a component's requirements against available providers.
|
|
516
|
+
|
|
517
|
+
A component may declare individual ``Requirement`` objects or grouped
|
|
518
|
+
requirements using ``RequirementGroup``.
|
|
519
|
+
|
|
520
|
+
Matching rules
|
|
521
|
+
--------------
|
|
522
|
+
1. Requirement and Capability names must match.
|
|
523
|
+
2. ``provider_type`` is an optional provider filter when set to ``None``.
|
|
524
|
+
3. If a requirement has an IO contract, its declared fields are compared
|
|
525
|
+
with the matching capability's optional ``contract`` attribute.
|
|
526
|
+
4. Missing provider-side contract details are warnings by default, or
|
|
527
|
+
errors with ``strict_contract=True``.
|
|
528
|
+
5. Optional individual requirements and optional groups produce warnings
|
|
529
|
+
instead of errors when they are not satisfied.
|
|
530
|
+
6. ``all_of`` groups require every member; ``any_of`` groups require at
|
|
531
|
+
least one member. Failed alternatives in a successful ``any_of`` group
|
|
532
|
+
do not create validation issues.
|
|
533
|
+
|
|
534
|
+
The function always returns a ValidationReport, including when the
|
|
535
|
+
consumer declares no requirements.
|
|
536
|
+
"""
|
|
537
|
+
if not isinstance(consumer, ComponentSpec):
|
|
538
|
+
raise TypeError("consumer must be a ComponentSpec.")
|
|
539
|
+
|
|
540
|
+
provider_specs = tuple(providers)
|
|
541
|
+
for provider in provider_specs:
|
|
542
|
+
if not isinstance(provider, ComponentSpec):
|
|
543
|
+
raise TypeError(
|
|
544
|
+
"providers must contain only ComponentSpec objects; "
|
|
545
|
+
f"got {type(provider).__name__}."
|
|
546
|
+
)
|
|
547
|
+
|
|
548
|
+
report = ValidationReport()
|
|
549
|
+
|
|
550
|
+
for item in consumer.requirements:
|
|
551
|
+
if isinstance(item, RequirementGroup):
|
|
552
|
+
report.extend(
|
|
553
|
+
_validate_requirement_group(
|
|
554
|
+
consumer=consumer,
|
|
555
|
+
group=item,
|
|
556
|
+
providers=provider_specs,
|
|
557
|
+
strict_contract=strict_contract,
|
|
558
|
+
)
|
|
559
|
+
)
|
|
560
|
+
elif isinstance(item, Requirement):
|
|
561
|
+
result = _evaluate_requirement(
|
|
562
|
+
consumer=consumer,
|
|
563
|
+
requirement=item,
|
|
564
|
+
providers=provider_specs,
|
|
565
|
+
strict_contract=strict_contract,
|
|
566
|
+
)
|
|
567
|
+
report.extend(result.report)
|
|
568
|
+
else:
|
|
569
|
+
raise TypeError(
|
|
570
|
+
"ComponentSpec.requirements must contain Requirement or "
|
|
571
|
+
f"RequirementGroup objects; got {type(item).__name__}."
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
return report
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from typing import Any, TypeAlias
|
|
6
|
+
|
|
7
|
+
ShapeDim: TypeAlias = int | str | None
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True, slots=True)
|
|
11
|
+
class IOContract:
|
|
12
|
+
"""Describe the structure and semantics of an input or output."""
|
|
13
|
+
|
|
14
|
+
kind: str | None = None
|
|
15
|
+
value_type: str | None = None
|
|
16
|
+
semantics: str | None = None
|
|
17
|
+
|
|
18
|
+
dtype: str | None = None
|
|
19
|
+
shape: tuple[ShapeDim, ...] | None = None
|
|
20
|
+
|
|
21
|
+
attributes: Mapping[str, Any] = field(default_factory=dict)
|
|
22
|
+
|
|
23
|
+
def __post_init__(self) -> None:
|
|
24
|
+
object.__setattr__(self, "attributes", dict(self.attributes))
|
|
25
|
+
if self.shape is not None:
|
|
26
|
+
object.__setattr__(self, "shape", tuple(self.shape))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
IOContractLike: TypeAlias = IOContract | Mapping[str, Any] | None
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def coerce_io_contract(value: IOContractLike) -> IOContract:
|
|
33
|
+
"""Coerce ``None``, ``Mapping``, or ``IOContract`` into an ``IOContract``."""
|
|
34
|
+
if value is None:
|
|
35
|
+
return IOContract()
|
|
36
|
+
if isinstance(value, IOContract):
|
|
37
|
+
return value
|
|
38
|
+
if isinstance(value, Mapping):
|
|
39
|
+
return IOContract(**value)
|
|
40
|
+
raise TypeError(
|
|
41
|
+
f"contract must be IOContract, Mapping, or None; got {type(value).__name__}."
|
|
42
|
+
)
|