-
Notifications
You must be signed in to change notification settings - Fork 9
Make public mathematical contracts canonical and composable #2185
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 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d0c4b32
refactor(polynomials): add canonical ideal and rational-function values
morluto 9ceb72a
fix(galois): enforce backend domain and complete certificates
morluto 7d2f411
fix(finite-fields): authenticate derived values and parent bindings
morluto 2279a57
fix(universal-algebra): close term and quotient contracts
morluto 2c649c9
fix(symbolic): replace expression strings with canonical values
morluto c7ad4f0
fix(exact-values): canonicalize rational contracts and results
morluto ef73877
fix(domain-contracts): enforce structural invariants at validation
morluto 1009d98
fix(ci): align finite-field import contract with validation
morluto c4f4c74
fix(math): preflight exact result growth
morluto a08ec92
fix(math): bound additional exact result growth
morluto 0c41ded
fix(contracts): preserve nested schema closure and versions
morluto fa982b4
style(graphical-models): format contract regression
morluto 57437ac
fix(contracts): preflight exact ratio and determinant growth
morluto 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
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 |
|---|---|---|
| @@ -1,106 +1,15 @@ | ||
| """Narrow strict-model primitive shared by unrelated wire owners.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import fields as dataclass_fields | ||
| from dataclasses import is_dataclass | ||
| from types import UnionType | ||
| from typing import Annotated, Any, Union, get_args, get_origin | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, model_validator | ||
|
|
||
|
|
||
| def _unwrap_annotation(annotation: Any) -> Any: | ||
| origin = get_origin(annotation) | ||
| if origin is Annotated: | ||
| args = get_args(annotation) | ||
| return _unwrap_annotation(args[0]) if args else annotation | ||
| return annotation | ||
|
|
||
|
|
||
| def _tuple_annotation(annotation: Any) -> Any | None: | ||
| annotation = _unwrap_annotation(annotation) | ||
| origin = get_origin(annotation) | ||
| if origin is tuple: | ||
| return annotation | ||
| if origin in {Union, UnionType}: | ||
| tuple_args = [ | ||
| arg | ||
| for arg in get_args(annotation) | ||
| if arg is not type(None) and get_origin(_unwrap_annotation(arg)) is tuple | ||
| ] | ||
| if len(tuple_args) == 1: | ||
| return tuple_args[0] | ||
| return None | ||
|
|
||
|
|
||
| def _lists_to_tuples(value: Any) -> Any: | ||
| if isinstance(value, list): | ||
| return tuple(_lists_to_tuples(item) for item in value) | ||
| return value | ||
|
|
||
|
|
||
| def _dataclass_annotation(annotation: Any) -> type[Any] | None: | ||
| annotation = _unwrap_annotation(annotation) | ||
| origin = get_origin(annotation) | ||
| if origin in {Union, UnionType}: | ||
| dataclass_args = [ | ||
| arg | ||
| for arg in get_args(annotation) | ||
| if arg is not type(None) and _dataclass_annotation(arg) is not None | ||
| ] | ||
| if len(dataclass_args) == 1: | ||
| return _dataclass_annotation(dataclass_args[0]) | ||
| return None | ||
| if ( | ||
| isinstance(annotation, type) | ||
| and is_dataclass(annotation) | ||
| and not issubclass(annotation, BaseModel) | ||
| ): | ||
| return annotation | ||
| return None | ||
|
|
||
|
|
||
| def _dataclass_from_json(cls: type[Any], value: Any) -> Any: | ||
| if isinstance(value, cls): | ||
| return value | ||
| if not isinstance(value, dict): | ||
| return value | ||
| allowed = {field.name for field in dataclass_fields(cls)} | ||
| unexpected = sorted(set(value) - allowed) | ||
| if unexpected: | ||
| raise ValueError(f"unexpected dataclass fields: {', '.join(unexpected)}") | ||
| prepared = { | ||
| name: _lists_to_tuples(item) if isinstance(item, list) else item | ||
| for name, item in value.items() | ||
| } | ||
| return cls(**prepared) | ||
| from pydantic import BaseModel, ConfigDict | ||
|
|
||
|
|
||
| class StrictModel(BaseModel): | ||
| """Closed, immutable model with JSON tuple/dataclass decoding.""" | ||
| """Closed immutable model; Pydantic owns nested and JSON validation.""" | ||
|
|
||
| model_config = ConfigDict(extra="forbid", frozen=True) | ||
| model_config = ConfigDict( | ||
| extra="forbid", | ||
| frozen=True, | ||
| ) | ||
|
|
||
| @model_validator(mode="before") | ||
| @classmethod | ||
| def accept_json_wire_shapes(cls, data: Any) -> Any: | ||
| """Decode JSON arrays and objects into declared tuple and dataclass fields.""" | ||
|
|
||
| if isinstance(data, cls) or not isinstance(data, dict): | ||
| return data | ||
| coerced = dict(data) | ||
| for name, field in cls.model_fields.items(): | ||
| if name not in coerced: | ||
| continue | ||
| value = coerced[name] | ||
| dataclass_type = _dataclass_annotation(field.annotation) | ||
| if dataclass_type is not None: | ||
| coerced[name] = _dataclass_from_json(dataclass_type, value) | ||
| continue | ||
| if _tuple_annotation(field.annotation) is None or not isinstance( | ||
| value, list | ||
| ): | ||
| continue | ||
| coerced[name] = _lists_to_tuples(value) | ||
| return coerced | ||
| __all__ = ["StrictModel"] | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.