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 1 commit
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
… 0.0 to 1.0
- The threshold a field ends up with is checked on all three paths, with
one message from comparable_field._check_threshold. It names the model
(for a schema, x-aws-stickler-model-name or the title, never
"DynamicModel(fields: ...)"), the field path, the source as the author
wrote it, the value with its type when not a number, the range, and the
likely fix ("For 85%, use 0.85", "write 0.9 without quotes"). From a
comparator it also names the field-level key that overrides it.
- Python: StructuredModel.__init_subclass__ now checks
ComparableField(threshold=...) and an adopted comparator threshold such
as LevenshteinComparator(threshold=85). Neither was checked, so at 85 an
identical pair scored 0.0 FD.
- Config and schema: an adopted comparator_config /
x-aws-stickler-comparator-config threshold raises instead of being
ignored with a UserWarning (c6878eb), on a scalar and on a nested model.
The field-level threshold / x-aws-stickler-threshold errors use the same
message, and now refuse a bool, which loaded as 1.
- Not checked: a comparator threshold the field overrides (the evaluator
never reads it; binary_compare() is the only other reader) and one on a
list of models, which pairs on match_threshold and already warns. No
comparator constructor checks its threshold, since a custom comparator
may use it for something else.
- Valid is a real number (numbers.Real, so a numpy scalar still passes),
not bool, from 0.0 to 1.0 inclusive; NaN and infinity fail.
- Removes the warn-and-ignore tests, and updates the three schema tests
that matched the old x-aws-stickler-threshold message.- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,8 +14,8 @@ | |||||||||||||
| from stickler.utils.deprecation import warn_once | ||||||||||||||
|
|
||||||||||||||
| from .comparable_field import ( | ||||||||||||||
| _LEGACY_DEFAULT_THRESHOLD, | ||||||||||||||
| ComparableField, | ||||||||||||||
| _check_threshold, | ||||||||||||||
| _is_valid_threshold, | ||||||||||||||
| _mark_unstated, | ||||||||||||||
| _named_comparator_threshold, | ||||||||||||||
|
|
@@ -236,9 +236,8 @@ def _nested_model_comparator_threshold( | |||||||||||||
| ``comparator`` is the instance built from a named comparator, or ``None`` when | ||||||||||||||
| the field gives only a config. Returns the threshold to adopt or ``None``, | ||||||||||||||
| the names of the config keys that have no effect, and a named threshold that | ||||||||||||||
| is not a number from 0.0 to 1.0 (``None`` if there is none). That one is | ||||||||||||||
| reported by its value rather than raised on: these keys were accepted and | ||||||||||||||
| dropped before, so refusing one now would break a config that loads. | ||||||||||||||
| is not a number from 0.0 to 1.0 (``None`` if there is none). The caller | ||||||||||||||
| raises on that one unless the field states its own threshold, which wins. | ||||||||||||||
| """ | ||||||||||||||
| named = ( | ||||||||||||||
| _named_comparator_threshold(comparator) | ||||||||||||||
|
|
@@ -259,35 +258,41 @@ def _nested_model_comparator_threshold( | |||||||||||||
| return adopted, ignored, rejected | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _rejected_threshold_warning( | ||||||||||||||
| field_path: str, | ||||||||||||||
| def _model_label(model_id: str) -> str: | ||||||||||||||
| """The model's name for an error: ``model_identity`` without its field list. | ||||||||||||||
|
|
||||||||||||||
| ``model_identity`` appends ``(fields: ...)`` to an unnamed model so two of | ||||||||||||||
| them dedupe apart. A reader knows the class as ``DynamicModel``. A | ||||||||||||||
| ``model_name`` is an identifier, so it cannot contain the parenthesis. | ||||||||||||||
| """ | ||||||||||||||
| return model_id.partition("(")[0] | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _check_comparator_threshold( | ||||||||||||||
| value: Any, | ||||||||||||||
| comparator: Any, | ||||||||||||||
| config: Mapping, | ||||||||||||||
| *, | ||||||||||||||
| model_id: str, | ||||||||||||||
| source: str, | ||||||||||||||
| default: Any, | ||||||||||||||
| threshold_key: str, | ||||||||||||||
| ) -> str: | ||||||||||||||
| """The warning for a comparator threshold that is not a number from 0.0 to 1.0. | ||||||||||||||
| field: str, | ||||||||||||||
| model: str, | ||||||||||||||
| config_source: str, | ||||||||||||||
| field_key: str, | ||||||||||||||
| ) -> None: | ||||||||||||||
| """Raise if a comparator threshold the field adopts is not from 0.0 to 1.0. | ||||||||||||||
|
|
||||||||||||||
| A field-level threshold out of range raises on load. A comparator threshold | ||||||||||||||
| was never adopted before, so it loaded whatever its value; it is now read, | ||||||||||||||
| and adopting ``85`` or ``"0.9"`` failed every pair or raised on every | ||||||||||||||
| ``compare_with``. It is ignored instead, which scores the field as before. | ||||||||||||||
| Adopted unchecked, 85 failed every pair, ``"0.9"`` raised on every | ||||||||||||||
| ``compare_with`` and -1 passed every pair. ``config_source`` names the | ||||||||||||||
| config key, as the author wrote it; a threshold the comparator's own | ||||||||||||||
| constructor set is named by the comparator instead. | ||||||||||||||
| """ | ||||||||||||||
| return ( | ||||||||||||||
| f"{source} {value!r} on field '{field_path}'{_of_model(model_id)} is not " | ||||||||||||||
| f"a number from 0.0 to 1.0, so it is ignored and the field keeps its " | ||||||||||||||
| f"default threshold of {default!r}. To gate the field, set " | ||||||||||||||
| f"{threshold_key} to a value from 0.0 to 1.0." | ||||||||||||||
| source = ( | ||||||||||||||
| config_source | ||||||||||||||
| if "threshold" in config | ||||||||||||||
| else f"comparator {type(comparator).__name__!r} (its own threshold)" | ||||||||||||||
| ) | ||||||||||||||
| _check_threshold( | ||||||||||||||
| value, field=field, model=model, source=source, field_key=field_key | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _comparator_threshold_source(comparator: Any, config: Mapping, config_key: str): | ||||||||||||||
| """Where a comparator's named threshold was written, for a warning.""" | ||||||||||||||
| if "threshold" in config: | ||||||||||||||
| return f"'{config_key}' threshold" | ||||||||||||||
| return f"The threshold set by comparator '{type(comparator).__name__}'" | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def _config_keys_phrase(config_key: str, keys: List[str]) -> str: | ||||||||||||||
|
|
@@ -533,39 +538,25 @@ def convert_field_config( | |||||||||||||
| # comparator, then inference, so a comparator threshold outranks an | ||||||||||||||
| # inferred one: the author wrote it. | ||||||||||||||
| # | ||||||||||||||
| # A comparator threshold is adopted only if it could have been written | ||||||||||||||
| # as the field's `threshold`, which is range-checked on load. Otherwise | ||||||||||||||
| # it is ignored with a warning, and the field keeps the default it had | ||||||||||||||
| # before comparator thresholds were read, so nothing that loads raises. | ||||||||||||||
| # A comparator threshold the field adopts is checked as the field's own | ||||||||||||||
| # `threshold` is, and raises on load. A field `threshold` wins, so the | ||||||||||||||
| # comparator's is then not read and not checked. | ||||||||||||||
| 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 |
||||||||||||||
| named = _named_comparator_threshold(comparator) | ||||||||||||||
| adopted = threshold is None and _is_valid_threshold(named) | ||||||||||||||
| threshold_defaulted = ( | ||||||||||||||
| threshold is None | ||||||||||||||
| and not adopted | ||||||||||||||
| and (inferred is not None or named is not None) | ||||||||||||||
| ) | ||||||||||||||
| if threshold_defaulted: | ||||||||||||||
| threshold = ( | ||||||||||||||
| inferred.threshold if inferred is not None else _LEGACY_DEFAULT_THRESHOLD | ||||||||||||||
| if threshold is None and named is not None: | ||||||||||||||
| _check_comparator_threshold( | ||||||||||||||
| named, | ||||||||||||||
| comparator, | ||||||||||||||
| comparator_config, | ||||||||||||||
| field=field_path, | ||||||||||||||
| model=_model_label(model_id), | ||||||||||||||
| config_source="comparator_config.threshold", | ||||||||||||||
| field_key="the field's threshold", | ||||||||||||||
| ) | ||||||||||||||
| if named is not None: | ||||||||||||||
| warn_once( | ||||||||||||||
| "field-config-comparator-threshold-out-of-range", | ||||||||||||||
| f"{model_id}|{field_path}|{named!r}", | ||||||||||||||
| _rejected_threshold_warning( | ||||||||||||||
| field_path, | ||||||||||||||
| named, | ||||||||||||||
| model_id=model_id, | ||||||||||||||
| source=_comparator_threshold_source( | ||||||||||||||
| comparator, comparator_config, "comparator_config" | ||||||||||||||
| ), | ||||||||||||||
| default=threshold, | ||||||||||||||
| threshold_key="'threshold'", | ||||||||||||||
| ), | ||||||||||||||
| category=UserWarning, | ||||||||||||||
| stacklevel=_stacklevel_outside_stickler(through_warn_once=True), | ||||||||||||||
| ) | ||||||||||||||
| adopted = threshold is None and named is not None | ||||||||||||||
| threshold_defaulted = threshold is None and not adopted and inferred is not None | ||||||||||||||
| if threshold_defaulted: | ||||||||||||||
| threshold = inferred.threshold | ||||||||||||||
| weight = field_config.get("weight", inferred.weight if inferred else 1.0) | ||||||||||||||
| clip_under_threshold = field_config.get("clip_under_threshold") | ||||||||||||||
| clip_inferred = clip_under_threshold is None and inferred is not None | ||||||||||||||
|
|
@@ -791,6 +782,16 @@ def _convert_nested_model_field( | |||||||||||||
| if ignored_keys: | ||||||||||||||
| ignored.append(_config_keys_phrase("comparator_config", ignored_keys)) | ||||||||||||||
| stated = "threshold" in field_config | ||||||||||||||
| if rejected is not None and not stated: | ||||||||||||||
| _check_comparator_threshold( | ||||||||||||||
| rejected, | ||||||||||||||
| named_comparator, | ||||||||||||||
| raw_config if isinstance(raw_config, Mapping) else {}, | ||||||||||||||
| field=field_path, | ||||||||||||||
| model=_model_label(model_id), | ||||||||||||||
| config_source="comparator_config.threshold", | ||||||||||||||
| field_key="the field's threshold", | ||||||||||||||
| ) | ||||||||||||||
| if ignored: | ||||||||||||||
| warn_once( | ||||||||||||||
|
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. Medium: the new nested-model warnings do not include the model name, so Python's default warning deduplication hides them for a second model. Loading
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 c6878eb, with docs in 2934248 and 1caa1cb. The nested-model warnings now name the model, so the default filter no longer folds Receipt into Invoice. On the schema path they use the title when there is no |
||||||||||||||
| "nested-model-comparator-keys", | ||||||||||||||
|
|
@@ -814,25 +815,6 @@ def _convert_nested_model_field( | |||||||||||||
| category=UserWarning, | ||||||||||||||
| stacklevel=_stacklevel_outside_stickler(through_warn_once=True), | ||||||||||||||
| ) | ||||||||||||||
| if rejected is not None and not stated: | ||||||||||||||
| warn_once( | ||||||||||||||
| "nested-model-comparator-threshold-out-of-range", | ||||||||||||||
| f"{model_id}|{field_path}|{rejected!r}", | ||||||||||||||
| _rejected_threshold_warning( | ||||||||||||||
| field_path, | ||||||||||||||
| rejected, | ||||||||||||||
| model_id=model_id, | ||||||||||||||
| source=_comparator_threshold_source( | ||||||||||||||
| named_comparator, | ||||||||||||||
| raw_config if isinstance(raw_config, Mapping) else {}, | ||||||||||||||
| "comparator_config", | ||||||||||||||
| ), | ||||||||||||||
| default=0.7, | ||||||||||||||
| threshold_key="'threshold'", | ||||||||||||||
| ), | ||||||||||||||
| category=UserWarning, | ||||||||||||||
| stacklevel=_stacklevel_outside_stickler(through_warn_once=True), | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| # Unused for scoring; ComparableField requires one. Carries an adopted | ||||||||||||||
| # comparator threshold, so `ComparableField` resolves it exactly as for | ||||||||||||||
|
|
@@ -1019,22 +1001,23 @@ def validate_field_config( | |||||||||||||
| raise ValueError(f"Invalid comparator for field '{field_name}': {e}") | ||||||||||||||
|
|
||||||||||||||
| # Validate numeric parameters | ||||||||||||||
| numeric_params = ["threshold", "weight"] | ||||||||||||||
| for param in numeric_params: | ||||||||||||||
| if param in field_config: | ||||||||||||||
| value = field_config[param] | ||||||||||||||
| if not isinstance(value, (int, float)): | ||||||||||||||
| raise ValueError( | ||||||||||||||
| f"Field '{field_name}' parameter '{param}' must be numeric, got {type(value)}" | ||||||||||||||
| ) | ||||||||||||||
| if param == "threshold" and not (0.0 <= value <= 1.0): | ||||||||||||||
| raise ValueError( | ||||||||||||||
| f"Field '{field_name}' threshold must be between 0.0 and 1.0, got {value}" | ||||||||||||||
| ) | ||||||||||||||
| if param == "weight" and value < 0: | ||||||||||||||
| raise ValueError( | ||||||||||||||
| f"Field '{field_name}' weight must be non-negative, got {value}" | ||||||||||||||
| ) | ||||||||||||||
| if "threshold" in field_config: | ||||||||||||||
| _check_threshold( | ||||||||||||||
| field_config["threshold"], | ||||||||||||||
| field=path, | ||||||||||||||
| model=_model_label(model_id), | ||||||||||||||
| source="threshold", | ||||||||||||||
| ) | ||||||||||||||
| if "weight" in field_config: | ||||||||||||||
| value = field_config["weight"] | ||||||||||||||
| if not isinstance(value, (int, float)): | ||||||||||||||
| raise ValueError( | ||||||||||||||
| f"Field '{field_name}' parameter 'weight' must be numeric, got {type(value)}" | ||||||||||||||
| ) | ||||||||||||||
| if value < 0: | ||||||||||||||
| raise ValueError( | ||||||||||||||
| f"Field '{field_name}' weight must be non-negative, got {value}" | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| # Validate boolean parameters | ||||||||||||||
| boolean_params = ["required", "clip_under_threshold"] | ||||||||||||||
|
|
||||||||||||||
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)'].