contract.py¶
clew/guard/contract.py
Machine-readable contract surface for consumers of the gate.
_effective_requirements
dict[str, Any] clew.guard.contract._effective_requirements(dict[str, Any] config)
Describe the requirements catalog as actually resolved from config.
Source · 20 lines
def _effective_requirements(config: dict[str, Any]) -> dict[str, Any]:
req_config = get_requirements(config)
if not req_config:
return {"declared": False}
return {
"declared": True,
"file": req_config.get("file"),
"format": req_config.get("format", REQUIREMENTS_FORMAT_DEFAULT),
"id_column": req_config.get("id_column", REQUIREMENTS_ID_COLUMN_DEFAULT),
"name_column": req_config.get("name_column", REQUIREMENTS_NAME_COLUMN_DEFAULT),
"status_field": req_config.get("status_field"),
"exempt_status": list(req_config.get("exempt_status") or []),
## 2.1 (#72 item 14): field-based exemptions, published beside the status ones.
"exempt_when": {k: list(v) for k, v in (req_config.get("exempt_when") or {}).items()},
"version_field": req_config.get("version_field"),
"external": list(req_config.get("external") or []),
"needs": req_config.get("needs"),
}
Activity · 4 steps
Requirements
_encode_schema
Any clew.guard.contract._encode_schema(Any node)
Render a schema node as JSON-safe data.
Source · 10 lines
def _encode_schema(node: Any) -> Any:
if node is _OPEN_DICT:
return _OPEN_NODE
if isinstance(node, _MapOf):
return {_MAP_KEY: _encode_schema(node.schema)}
if isinstance(node, dict):
return {k: _encode_schema(v) for k, v in node.items()}
return node.__name__ if isinstance(node, type) else str(node)
Activity · 7 steps
Requirements
_strip_private
Any clew.guard.contract._strip_private(Any node)
Strip private keys so the emitted config is valid as input.
Source · 6 lines
def _strip_private(node: Any) -> Any:
if not isinstance(node, dict):
return node
return {k: _strip_private(v) for k, v in node.items() if not str(k).startswith("_")}
Activity · 3 steps
Requirements
build_effective_contract
dict[str, Any] clew.guard.contract.build_effective_contract(dict[str, Any] config)
Build the effective-config contract for the config currently in force.
Called by _run_config_command
Tested by 10 tests
test_adding_a_schema_key_shows_up_without_touching_contract_py, test_effective_envelope, test_emitted_config_loads, test_exposes_exclude_patterns, test_exposes_req_pattern, test_is_json_serializable, test_private_keys_are_stripped, test_reports_declared_requirements, test_reports_undeclared_requirements, test_scalar_validate_keys_all_appear
Source · 17 lines
def build_effective_contract(config: dict[str, Any]) -> dict[str, Any]:
validate = get_validate(config)
resolved: dict[str, Any] = {"output_dir": config.get("output_dir")}
for key, spec in CONFIG_SCHEMA["validate"].items():
if spec is _OPEN_DICT or isinstance(spec, dict | _MapOf):
continue
resolved[key] = validate.get(key)
resolved["req_pattern"] = validate.get("tags", {}).get("req", {}).get("pattern")
resolved["requirements"] = _effective_requirements(config)
guard_only = {k: v for k, v in config.items() if k not in NON_GUARD_KEYS}
return {
"contract_version": CONTRACT_VERSION,
"config": _strip_private(guard_only),
"resolved": resolved,
}
Activity · 10 steps
Requirements
Flows
build_files_contract
dict[str, Any] clew.guard.contract.build_files_contract(list[str] source_dirs, dict[str, Any] config)
Build the exact post-exclude file set the gate walks for the given roots.
Called by _run_files_command
Tested by 9 tests
test_count_matches_file_list, test_excluded_path_is_absent_and_gate_skips_it, test_excluded_paths_are_absent, test_files_contract_reports_and_applies_toolchain_ignores, test_files_envelope, test_is_json_serializable, test_nested_lookalike_path_agrees, test_reports_the_patterns_that_produced_the_set, test_without_excludes_test_files_are_included
Source · 17 lines
def build_files_contract(source_dirs: list[str], config: dict[str, Any]) -> dict[str, Any]:
exclude = get_validate(config).get("exclude", [])
files: list[str] = []
for source_dir in source_dirs:
files.extend(str(p) for p in find_source_files(source_dir, config))
unique = sorted(set(files))
logger.info("Contract file set: %d file(s) across %d root(s)", len(unique), len(source_dirs))
return {
"contract_version": CONTRACT_VERSION,
"source_dirs": list(source_dirs),
"exclude": exclude,
**toolchain_contract(),
"files": unique,
"count": len(unique),
}
Activity · 7 steps
Requirements
Flows
build_schema_contract
dict[str, Any] clew.guard.contract.build_schema_contract()
Build the full config contract: schema, defaults, and catalog constants.
Called by _run_config_command
Tested by 12 tests
test_carries_contract_version, test_catalog_block_envelope, test_catalog_constants_match_the_module, test_id_column_scoped_to_flat_formats, test_is_json_serializable, test_names_the_config_file_and_section, test_open_nodes_are_marked_not_dropped, test_opt_in_languages_are_published_but_not_in_force, test_schema_envelope, test_the_schema_contract_names_the_file_and_section, test_trace_section_absent, test_types_encoded_by_name
Calls _encode_schema, list
Source · 20 lines
def build_schema_contract() -> dict[str, Any]:
return {
"contract_version": CONTRACT_VERSION,
"config_file": CONFIG_FILE_NAME,
"config_section": CONFIG_SECTION,
"config_schema": _encode_schema(CONFIG_SCHEMA),
"config_defaults": CONFIG_DEFAULTS,
"opt_in_language_defaults": OPT_IN_LANGUAGE_DEFAULTS,
"requirements_catalog": {
"root_key": REQUIREMENTS_ROOT_KEY,
"format_default": REQUIREMENTS_FORMAT_DEFAULT,
"id_column_default": REQUIREMENTS_ID_COLUMN_DEFAULT,
"name_column_default": REQUIREMENTS_NAME_COLUMN_DEFAULT,
"required_fields": list(REQUIREMENTS_REQUIRED_FIELDS),
"optional_fields": list(REQUIREMENTS_OPTIONAL_FIELDS),
"formats_using_id_column": list(REQUIREMENTS_FORMATS_USING_ID_COLUMN),
},
}