-
Notifications
You must be signed in to change notification settings - Fork 0
chore: consolidate check helpers into resources/_check_helpers
#87
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| """Request-side helpers for converting `CheckConfigParam` into the wire format.""" | ||
|
|
||
| from typing import Any, Dict, Iterable, Optional, cast | ||
|
|
||
| from .._models import BaseModel | ||
| from ..types.check import CheckConfigParam | ||
|
|
||
| # Maps a built-in check identifier to its `kind` discriminator. | ||
| IDENTIFIER_TO_KIND: Dict[str, str] = { | ||
| "correctness": "hub_correctness", | ||
| "conformity": "hub_conformity", | ||
| "groundedness": "hub_groundedness", | ||
| "string_match": "string_matching", | ||
| "metadata": "hub_metadata", | ||
| "semantic_similarity": "semantic_similarity", | ||
| } | ||
|
|
||
|
|
||
| def check_param_to_spec(identifier: Optional[str], params: Any) -> Dict[str, Any]: | ||
| """Build a `spec` dict, deriving `kind` from `params["type"]` then `identifier`.""" | ||
| if isinstance(params, BaseModel): | ||
| params_dict: Dict[str, Any] = params.model_dump(exclude_none=True) | ||
| elif isinstance(params, dict): | ||
| params_dict = dict(cast(Dict[str, Any], params)) | ||
| else: | ||
| params_dict = {} | ||
| type_from_params = params_dict.pop("type", None) | ||
| type_str = type_from_params or identifier or "" | ||
| if not type_str: | ||
| raise ValueError( | ||
| "Cannot derive check kind: provide 'identifier' or include 'type' in 'params', " | ||
| "or pass 'spec' directly with an explicit 'kind'." | ||
| ) | ||
| kind = IDENTIFIER_TO_KIND.get(type_str, type_str) | ||
| return {"kind": kind, **params_dict} | ||
|
|
||
|
|
||
| def check_params_to_specs( | ||
| checks: Iterable[CheckConfigParam], | ||
| *, | ||
| flat: bool = False, | ||
| ) -> list[Dict[str, Any]]: | ||
| """Convert checks to the wire format. | ||
|
|
||
| `flat=False` (default) wraps params under a `spec` key: | ||
| `{identifier, enabled, spec: {kind, ...params}}`. | ||
|
|
||
| `flat=True` spreads params alongside `identifier`: | ||
| `{identifier, ...params}` (with the redundant `type` key stripped). | ||
| """ | ||
| result: list[Dict[str, Any]] = [] | ||
| for check in checks: | ||
| identifier = check["identifier"] | ||
| params = check.get("params") or {} | ||
| if flat: | ||
| result.append({"identifier": identifier, **{k: v for k, v in params.items() if k != "type"}}) | ||
| else: | ||
| entry: Dict[str, Any] = {"identifier": identifier, "enabled": check.get("enabled", True)} | ||
| if params: | ||
| entry["spec"] = check_param_to_spec(identifier, params) | ||
| result.append(entry) | ||
| return result | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
flat=Truebranch currently assumesparamsis a dictionary and calls.items(). However,paramscan also be a PydanticBaseModel(as handled incheck_param_to_spec). This will cause anAttributeErrorat runtime if a model is passed. Reusingcheck_param_to_specensures consistent handling of both dictionaries and models while also correctly stripping thetypekey.