Repository navigation
fix(importers): resolve an unstated threshold and clip as the Python path does (#334, #348) #408
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
base: dev
Are you sure you want to change the base?
Changes from 4 commits
e364b7a
adbda67
3637981
530e77f
ede3216
60703e0
626f6dd
7dfab62
83f1556
6e51532
fe8b830
c6878eb
2934248
dad0ca9
f7a4640
1caa1cb
ff19b01
a7ab802
09dcedd
a17802c
4f0fb4a
f5ad746
3991537
be9b0b0
d78cb3c
526061b
7aa8375
55e4023
1654b58
32febb2
89b7e07
9794614
9d4a8a5
0cad533
4e59553
f7c7782
60cc3a2
d77fb56
e9228d7
44d21f9
96f5567
c4612ba
9b2b5a9
3a18858
cb26400
8ce3b28
5d85b9b
605ea32
a5230d5
d86e94e
43579b1
2b8d1b0
f7149a4
a93086a
726726f
f0800a4
b78859f
e7ccbdc
619b714
006f558
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,7 +11,11 @@ | |||||||||||||
|
|
||||||||||||||
| from stickler.utils.deprecation import warn_once | ||||||||||||||
|
|
||||||||||||||
| from .comparable_field import ComparableField | ||||||||||||||
| from .comparable_field import ( | ||||||||||||||
| ComparableField, | ||||||||||||||
| _mark_unstated, | ||||||||||||||
| _named_comparator_threshold, | ||||||||||||||
| ) | ||||||||||||||
| from .comparator_registry import create_comparator, normalize_comparator_config | ||||||||||||||
| from .type_resolver import resolve_type_string | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -326,15 +330,26 @@ def convert_field_config( | |||||||||||||
| except (KeyError, TypeError) as e: | ||||||||||||||
| raise ValueError(f"Invalid comparator for field '{field_name}': {e}") | ||||||||||||||
|
|
||||||||||||||
| # Extract other field parameters | ||||||||||||||
| threshold = field_config.get( | ||||||||||||||
| "threshold", inferred.threshold if inferred else 0.5 | ||||||||||||||
| # `None` for what the config does not state, so `ComparableField` resolves | ||||||||||||||
| # it exactly as it does on the Python path. Defaulting to 0.5 and True here | ||||||||||||||
| # marked both as stated, which discarded a threshold named in | ||||||||||||||
| # `comparator_config` (#334) and suppressed the object-grade clip default, | ||||||||||||||
| # zeroing a dict field's partial credit (#348). Precedence is field, then | ||||||||||||||
| # comparator, then inference, so a comparator threshold outranks an | ||||||||||||||
| # inferred one: the author wrote it. | ||||||||||||||
| threshold = field_config.get("threshold") | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. High: Measured on head merged into dev (daeb226), compared with the merge base:
In all three cases,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, thanks for the table. This is now the field-threshold validation, and it raises. An adopted A field The Python path gets the same check when the class is defined. A field whose type is defined further down is read as adopting its comparator's threshold, since stickler cannot tell yet whether it is a list of models. So The runtime
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correction to my comment above: asking to "apply the field-threshold validation to the adopted value" asked for a raise on the config path. Under the #443 decision that was wrong for 1.0.2. The fix that meets both this finding and #443: warn, and do not adopt an invalid |
||||||||||||||
| threshold_inferred = ( | ||||||||||||||
| threshold is None | ||||||||||||||
| and inferred is not None | ||||||||||||||
| and _named_comparator_threshold(comparator) is None | ||||||||||||||
| ) | ||||||||||||||
| if threshold_inferred: | ||||||||||||||
| threshold = inferred.threshold | ||||||||||||||
| weight = field_config.get("weight", inferred.weight if inferred else 1.0) | ||||||||||||||
| clip_under_threshold = field_config.get( | ||||||||||||||
| "clip_under_threshold", | ||||||||||||||
| inferred.clip_under_threshold if inferred else True, | ||||||||||||||
| ) | ||||||||||||||
| clip_under_threshold = field_config.get("clip_under_threshold") | ||||||||||||||
| clip_inferred = clip_under_threshold is None and inferred is not None | ||||||||||||||
| if clip_inferred: | ||||||||||||||
| clip_under_threshold = inferred.clip_under_threshold | ||||||||||||||
|
|
||||||||||||||
| # Name the parameters the author took back, so the trail cannot contradict | ||||||||||||||
| # the row it sits under. Inference fills only what the config left unnamed, | ||||||||||||||
|
|
@@ -388,6 +403,9 @@ def convert_field_config( | |||||||||||||
| description=description, | ||||||||||||||
| examples=examples, | ||||||||||||||
| ) | ||||||||||||||
| _mark_unstated( | ||||||||||||||
| comparable_field, threshold=threshold_inferred, clip=clip_inferred | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| # Record what was inferred, so `explain()` reports the reason and a | ||||||||||||||
| # `source` naming what drove the choice (`type`, `name-token`) rather than | ||||||||||||||
|
|
@@ -494,7 +512,8 @@ def _convert_nested_model_field( | |||||||||||||
| # CRITICAL FIX: Create ComparableField for nested models to enable proper comparison | ||||||||||||||
| # Extract threshold and weight from field configuration | ||||||||||||||
| weight = field_config.get("weight", 1.0) # Default weight | ||||||||||||||
| clip_under_threshold = field_config.get("clip_under_threshold", True) | ||||||||||||||
| # `None` when unstated, for the reason given in `convert_field_config`. | ||||||||||||||
| clip_under_threshold = field_config.get("clip_under_threshold") | ||||||||||||||
|
|
||||||||||||||
| # For list_structured_model, don't set threshold (Hungarian matching uses model's match_threshold) | ||||||||||||||
| # For single structured_model, use threshold from config | ||||||||||||||
|
|
@@ -528,6 +547,8 @@ def _convert_nested_model_field( | |||||||||||||
| description=description, | ||||||||||||||
| examples=examples, | ||||||||||||||
| ) | ||||||||||||||
| # The 0.7 above is this path's default, not the author's. | ||||||||||||||
| _mark_unstated(comparable_field, threshold="threshold" not in field_config) | ||||||||||||||
|
|
||||||||||||||
| return field_type, comparable_field | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,11 @@ | |
| from pydantic import AnyUrl, BaseModel, TypeAdapter | ||
| from pydantic.fields import FieldInfo | ||
|
|
||
| from .comparable_field import ComparableField | ||
| from .comparable_field import ( | ||
| ComparableField, | ||
| _mark_unstated, | ||
| _named_comparator_threshold, | ||
| ) | ||
| from .comparator_registry import create_comparator, normalize_comparator_config | ||
| from .field_converter import AUTO_COMPARATOR | ||
| from .model_factory import ModelFactory | ||
|
|
@@ -412,6 +416,24 @@ def _convert_field( | |
| "recursive field-by-field comparison", | ||
| "this object", | ||
| ) | ||
| # Refused rather than dropped. An object with `properties` is scored | ||
| # field by field, so no comparator is ever read here, and accepting | ||
| # one let `explain()` report the placeholder below as the author's | ||
| # explicit choice (#348). The config path refuses `comparator` on a | ||
| # `structured_model` field the same way. | ||
| extra = field_info.json_schema_extra | ||
| for key in ( | ||
| "x-aws-stickler-comparator", | ||
| "x-aws-stickler-comparator-config", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should-fix (a follow-up is fine): misplaced comparator keys are refused unevenly. This raises on
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, and thanks for the Python measurement. I measured the Python path on a single nested model, an optional one and a list, and both importers now match it (626f6dd): a comparator threshold on a single or optional nested model is read and gates, and a named comparator, or any other config key, warns with one wording on both paths, pointing at
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The explain() part vawsgit flagged as still open is fixed in c6878eb. A named comparator on a nested model is now installed, as on the Python path, so explain() reports ExactComparator on all three paths instead of the placeholder Levenshtein. It is still never called, and it still warns. Your array-of-objects point was fixed in 626f6dd, and explain() reports 0.5 there now. The list_structured_model asymmetry (the config path raises on any comparator, the schema path accepts Levenshtein) is left for #468, as you suggested. One correction to my earlier reply: "nothing that loaded before fails" no longer holds. A threshold or
The runtime |
||
| ): | ||
| if isinstance(extra, dict) and key in extra: | ||
| raise ValueError( | ||
| f"{key} is not read on field '{field_path}': it is an " | ||
| "object with properties, scored by recursive " | ||
| "field-by-field comparison, so it has no comparator. " | ||
| "Remove it, and set it on the properties inside the " | ||
| "object instead." | ||
| ) | ||
| comparison_field = self._make_comparison_field( | ||
| field_info, | ||
| comparator_name="LevenshteinComparator", | ||
|
|
@@ -431,8 +453,10 @@ def _convert_field( | |
| # configurable. See #317. | ||
| threshold=extensions.get("threshold", 0.7), | ||
| weight=extensions.get("weight", 1.0), | ||
| clip_under_threshold=extensions.get("clip_under_threshold", True), | ||
| clip_under_threshold=extensions.get("clip_under_threshold"), | ||
| ) | ||
| # The 0.7 is this path's default, not the author's (#334). | ||
| _mark_unstated(comparison_field, threshold="threshold" not in extensions) | ||
| return final_type, comparison_field | ||
|
|
||
| if origin in (list, List, set): | ||
|
|
@@ -738,18 +762,33 @@ def _comparison_from_extensions( | |
| # the same silent drop one branch over. | ||
| comparator = create_comparator(comparator_name, author_config) | ||
|
|
||
| # Precedence is field, then comparator, then this path's default, as on | ||
| # the Python path. The default (inferred, or the type's row) used to be | ||
| # passed whenever the field stated nothing, which discarded a threshold | ||
| # named in `x-aws-stickler-comparator-config` (#334). `None` lets | ||
| # `ComparableField` adopt it. An unstated clip is likewise left `None`, so | ||
| # an object-grade field gets its unclipped default (#348). | ||
| declared_threshold = extensions.get("threshold") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same trail gap on the schema path: with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 60703e0. The schema path recorded no overrides at all, so
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same gap as
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same fix on the schema path (09dcedd, message reworded in f5ad746, 1654b58 and 60cc3a2). An adopted On an array of objects the comparator config is not read, so it warns instead. Pairing there uses |
||
| threshold_defaulted = ( | ||
| declared_threshold is None | ||
| and _named_comparator_threshold(comparator) is None | ||
| ) | ||
| clip = extensions.get("clip_under_threshold") | ||
| clip_inferred = clip is None and inferred is not None | ||
| field = self._make_comparison_field( | ||
| field_info, | ||
| comparator=comparator, | ||
| threshold=extensions.get( | ||
| "threshold", inferred.threshold if inferred else threshold | ||
| threshold=( | ||
| (inferred.threshold if inferred else threshold) | ||
| if threshold_defaulted | ||
| else declared_threshold | ||
| ), | ||
| weight=extensions.get("weight", inferred.weight if inferred else 1.0), | ||
| clip_under_threshold=extensions.get( | ||
| "clip_under_threshold", | ||
| inferred.clip_under_threshold if inferred else True, | ||
| clip_under_threshold=( | ||
| inferred.clip_under_threshold if clip_inferred else clip | ||
| ), | ||
| ) | ||
| _mark_unstated(field, threshold=threshold_defaulted, clip=clip_inferred) | ||
| if inferred is not None and inferred.provenance: | ||
| extra_callable = field.json_schema_extra | ||
| if callable(extra_callable): | ||
|
|
@@ -764,7 +803,7 @@ def _make_comparison_field( | |
| comparator_name: Optional[str] = None, | ||
| threshold: Optional[float], | ||
| weight: float, | ||
| clip_under_threshold: bool, | ||
| clip_under_threshold: Optional[bool], | ||
| ) -> FieldInfo: | ||
| if comparator is None: | ||
| comparator = create_comparator(comparator_name, {}) | ||
|
|
||
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.
Should-fix:
explain()now contradicts its own row. Config{"type":"str","comparator":"auto","comparator_config":{"threshold":0.9}}resolves threshold 0.9, butwhystill says['type:str -> LevenshteinComparator@0.7'], with no override note. A field-levelthresholdgetsconfig: threshold=0.6 (overrides above)(L361-368), but nothing records the newly adopted comparator threshold here. On dev the trail matched, because the comparator threshold was ignored.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.
Good catch. The trail now names an adopted comparator threshold the same way it names a field threshold:
config: comparator_config.threshold=0.9 (overrides above). Fixed in 60703e0, with a test that fails without it. Across a 2,112-cell sweep,whynow never contradicts its row on either path (dev had 168).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.
Fixed in 60703e0, and vawsgit confirmed it in the 2026-10-30 review. It still holds at a93086a: your config gives
why: ['type:str -> LevenshteinComparator@0.7', 'config: comparator_config.threshold=0.9 (overrides above)'].