Skip to content

test: harden README defaults and example checks - #558

Open
mangeshraut712 wants to merge 7 commits into
awslabs:devfrom
mangeshraut712:test/436-readme-defaults
Open

mangeshraut712 wants to merge 7 commits into
awslabs:devfrom
mangeshraut712:test/436-readme-defaults

Conversation

@mangeshraut712

@mangeshraut712 mangeshraut712 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #436.

The README regression tests now execute annotated print calls and associate each numeric claim with its source call, including loops, repeated labels and multiline annotations. Unannotated output and prose comments do not become claims. Field verdicts use field thresholds; model-list pairing separately uses the unwrapped element threshold, including Optional/Annotated types.

The default-pattern checks cover scalar, enum, const and mapping fields. Fixtures defer document/schema loading until test execution, helper regressions share model setup, and the README tree is aligned. No production or dependency changes relative to dev.

Validation against dev 7034ec07ddf6084a0541e1c48c3fe8418c737922, Python 3.12.16:

  • 11 new regression failures reproduced before the review corrections; all 55 focused tests pass after them.
  • Full non-integration suite with optional LLM/BERT dependencies: 3,553 passed, 2 skipped, 1 xfailed.
  • Repository-wide Ruff and diff whitespace checks pass.
  • Tests explicitly import this checkout's src/stickler. Independent review found no blocking issues.

AI-assisted implementation and independent review. GitHub workflows remain subject to maintainer authorization; this does not claim hosted CI completion.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@vawsgit vawsgit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs changes

Two of the new checks do not catch the drift that #436 asks them to pin. Everything else in the PR works. Measured at head facaa4b with stickler imported from that checkout's src.

1. The threshold Default check never reads the enum / const / format clause.

(
r"`([0-9.]+)` for a free-form",
[

The `([0-9.]+)` for a free-form pattern takes the number before "free-form" and applies it to the enum, const and format fields as well. Editing README.md:351 to say 0.5 for "any property or array items with enum, const, or a string format" (the code still returns 1.0) gives 40 passed. That is the #352 drift from #436 item 1. Fix: give that clause its own pattern and field list.

2. The element-threshold tests cannot tell the element's threshold from the parent's.

if isinstance(item, type) and issubclass(item, StructuredModel):
return item.match_threshold
return model._get_comparison_info(field).threshold

value: str = ComparableField()
match_threshold = 0.7

value: str = ComparableField()
match_threshold = 0.7

Both tests set Item.match_threshold = 0.7, which is also the parent model's default. Changing line 133 to return model.match_threshold gives 40 passed. With Item.match_threshold = 0.8, the same change fails (assert 0.7 == 0.8). Fix: give the element class and the parent model different thresholds (for example 0.8 and 0.6).

Verified and cleared

  • The module passes 40/40 on Python 3.12 and 3.10, and with -n 4. The full suite on a local merge with current dev passes (3498 passed, 12 skipped, 1 xfailed, integrations excluded).
  • Each mutation the PR claims it catches does fail: removing the element-class branch, an import-time README read, README weight Default 2.0, comparator-table date threshold 0.5, model-list threshold 0.8, an untested comparator-table row, an extra Position row.
  • Ruff check and format pass. No conflict with dev.

Non-blocking (low)

  • An annotation on any line of a multi-line print other than the last is ignored without failing (L63).
  • An import-time read written with open() passes the import-time check (L511).
  • The x-aws-stickler-infer-unspecified and x-aws-stickler-comparator-config rows are still unchecked, so "complete documentation-table coverage" in the CHANGELOG line overstates it.
  • Some doc edits fail with a bare AttributeError/KeyError/ValueError rather than the "Docs table changed" message (L244-250, L289, L415).

The threshold Default check read one number for the free-form object
clause and applied it to the enum, const and format fields too, so the
README could give that clause a different value without a failure. The
clause now has its own pattern, which requires it to follow the free-form
number with no new value in between.

The element-threshold tests used match_threshold 0.7 for the element
class, which is also the parent model's default, so returning the parent's
threshold passed. They now use 0.8 for the element and 0.6 for the parent.

The CHANGELOG line no longer claims complete documentation-table
coverage; two extension rows are still unchecked.
@mangeshraut712

Copy link
Copy Markdown
Contributor Author

@vawsgit Both fixed in 0dde58e.

  1. The enum/const/format clause has its own pattern and field list. It requires the clause to follow the free-form number with no new value in between. With README:351 edited to give that clause 0.5, the module now fails.
  2. The element-threshold tests use 0.8 for Item and 0.6 for the parent, and the straddle values moved to 0.798/0.802. Changing line 133 to return model.match_threshold now fails 4 tests.

The module gives 40 passed, and Ruff check and format pass. I also changed the CHANGELOG line so it no longer claims complete documentation-table coverage, since the two extension rows you noted are still unchecked. Could you re-review?

@mangeshraut712

Copy link
Copy Markdown
Contributor Author

Merged current dev into this branch in 76d403b to clear the CHANGELOG.md conflict; both ### Changed entries are kept. No code changes beyond the merge. The full suite gives 4145 passed, 13 skipped, 1 xfailed, and ruff is clean on tests/test_readme_examples.py.

@sromoam sromoam self-assigned this Nov 11, 2026

@sromoam sromoam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @mangeshraut712! Pinning the README's own defaults and example numbers is a great idea, and the tests pass on this branch. A few of the new checks would fail when nothing is wrong, and one checks the wrong threshold, so this needs some changes before it can merge.

Needs changes

  1. List-of-models fields use the wrong threshold (_matching_threshold, around tests/test_readme_examples.py:129). For a list of models, the check compares the list's field_scores value against the element class's match_threshold (0.7). That threshold only gates individual Hungarian pairs. The list score itself is judged against the field's own threshold (0.5), which is also what explain() reports for it. As written, a README Line Items row measuring 0.701 against an annotated 0.69 fails although no verdict changes. A 0.49 vs 0.51 change that does cross 0.5 isn't caught.
  2. Unwrap Annotated in the same helper. get_origin(...) is list returns Annotated for Annotated[List[LineItem], ...], so the helper silently falls back. The library already has helpers for this (unwrap_annotated, _annotation_is_list), and reusing them would avoid a second copy of that logic.
  3. Labels with leading spaces never match (around :79). The label is stripped, but output lines are matched unstripped. Reproduced: print(f" Customer: {s:.3f}") # 0.562 fails with README output missing labels: {'Customer:'}.
  4. Prose comments are treated as numbers (around :64). re.match(r"[+\-.\d]", ...) matches any comment starting with -, + or ., so print(...) # ... or # -> see below now fails with README numeric annotation is invalid. The old regex skipped these.
  5. Unannotated prints sharing a label fail (around :92). Every output line starting with an annotated label has to parse as a number, so a later print(f"Score: {detail}") printing n/a fails the test. Please match only the annotated print's own output line.
  6. Keep the four fields the threshold-pattern check dropped (around :343): int_enum, num_const, typeless_enum and dated_levenshtein. The prose it checks says "any property ... with enum, const", so they still apply. The third pattern is also an exact prefix of the fourth, so one of them can go.

Smaller things

  • The Item/Document model pair is defined twice with small differences (around :535). One module-level helper would keep them in sync.
  • In test_readme_comparator_table_matches_schema (around :425), dispatching on position alone would be simpler and safer than mixing field-name and position checks.
  • test_module_import_defers_document_reads_and_schema_building reruns the module and relies on pytest's fixture __wrapped__, which is an internal detail and may break on a pytest upgrade. A simpler check, or none, would be more robust.
  • This change touches only tests, so its CHANGELOG entry shouldn't sit under the user-facing ### Changed. Either drop it or note it as internal. The new tests/README.md tree line is also slightly misaligned.

Thanks again for taking this on!

@mangeshraut712

Copy link
Copy Markdown
Contributor Author

Thank you @sromoam for the careful review! All items have been addressed in d1c5fee:

  1. List-of-models threshold: Evaluated list score against the field's own threshold (0.5), keeping Hungarian pairs gated by the element class's match_threshold (0.7).
  2. Unwrap Annotated: Reused existing library helpers unwrap_annotated and _annotation_is_list instead of duplicated unwrapping logic.
  3. Leading whitespace: Handled leading indentation on labels when matching output lines.
  4. Prose comment regex: Tightened numeric comment parsing so non-numeric prose comments are skipped rather than failing validation.
  5. Output line matching: Restricted numeric assertions strictly to the annotated print's own line.
  6. Threshold-pattern fields: Retained int_enum, num_const, typeless_enum, and dated_levenshtein, removing the redundant prefix pattern.
  7. Model deduplication: Consolidated the Item/Document model definitions into a shared module-level helper.
  8. Position dispatch: Simplified comparator table verification to dispatch on position alone.
  9. Removed __wrapped__ reliance: Replaced reliance on pytest fixture internals with a standard isolated invocation.
  10. CHANGELOG & README alignment: Moved test-only note out of the user-facing ### Changed section in CHANGELOG.md and aligned the tree hierarchy in tests/README.md.

Whenever you have a chance, please let me know if anything further is needed.

@vawsgit vawsgit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs changes

Thanks for the round, @mangeshraut712. Nearly everything from both earlier reviews is fixed. One change in d1c5fee undoes item 2 of #436, and this PR says Fixes #436, so merging it would close the issue with item 2 still open.

Blocking: the example verdict check uses a threshold the library does not apply to lists of models

d1c5fee changed _matching_threshold (tests/test_readme_examples.py#L156-L160) back to _get_comparison_info(field).threshold for every field. That is what dev already does. Commit 0dde58e used the element class's match_threshold, which is what #436 item 2 asks for.

The library decides a list-of-models field's TP/FD with the element's match_threshold, not with the field threshold. I measured this on this PR's head, with stickler imported from the PR worktree's src, using the PR's own _threshold_model() (Item.match_threshold = 0.8, field threshold 0.5) and one-item lists:

score=0.800 tp=1 fd=0  PR check says pass (>= 0.5): True
score=0.700 tp=0 fd=1  PR check says pass (>= 0.5): True
score=0.502 tp=0 fd=1  PR check says pass (>= 0.5): True

Two more facts point the same way:

  • ComparableField(threshold=0.9) on a List[Item] field raises ValueError: Field 'items' is a List[StructuredModel] and cannot have a 'threshold' parameter in ComparableField. Hungarian matching uses each StructuredModel's 'match_threshold' class attribute instead.
  • On a model whose list field reports a threshold of 0.9, a list score of 0.800 is still tp=1. The reported field threshold is not consulted.

So a README Line Items annotation that reads 0.802 (a TP) while the code measures 0.798 (an FD) passes this check. The review comment that prompted the change (item 1 of the 2026-11-11 review, "the list score itself is judged against the field's own threshold (0.5)") does not match this measured behaviour.

What to change:

  1. In _matching_threshold, return _element_matching_threshold(model, field) when the field is a list of models. Keep the unwrap_annotated / _annotation_is_list handling you added.
  2. Invert test_example_verdict_check_uses_the_field_threshold and test_an_element_threshold_crossing_does_not_change_the_fields_verdict. A crossing of the element threshold (0.798 vs 0.802 with match_threshold = 0.8) must fail the check, and a crossing of 0.5 only must pass it.
  3. If you would rather not do 1 and 2 here, change Fixes #436 to Refs #436 and say that item 2 is left.

Earlier findings: all fixed except two LOW items

  • The enum/const/format threshold clause is now checked. A mutated 0.5 in that README clause fails test_readme_extension_defaults_match_schema.
  • The element and parent thresholds are now distinct (0.8 and 0.6), and the Annotated unwrap is pinned. Removing unwrap_annotated(item) fails the [True] case.
  • Multi-line print annotations, leading-space labels, prose comments, unannotated prints that share a label, and the four restored threshold-pattern fields all behave correctly, and each has a test.
  • runpy.run_path replaces the __wrapped__ dependency. An import-time Path.read_text or from_json_schema call fails that test.
  • The CHANGELOG entry is gone, and the tests/README.md tree line is aligned.

What was verified

  • The diff against current dev touches only tests/test_readme_examples.py and tests/README.md. The PR adds no production or dependency changes.
  • The branch merges cleanly into current dev. After the merge, the 55 focused tests pass, and the non-integration suite gives 3584 passed, 2 skipped, 1 xfailed.
  • At the head, the suite gives 3553 passed, 2 skipped, 1 xfailed, as the PR body says. The focused module passes on Python 3.10, 3.12 and 3.14. Ruff passes.
  • I ran 22 mutations against the README text, the docs tables, the production default and the test helpers. The tests caught 20 of them. The two misses are the LOW items below.

Non-blocking (LOW)

  • An annotation that starts with a word or symbol (# about 0.9, # ≈0.9) is silently dropped. assert rows catches only a block that loses every claim.
  • The format list in the threshold Default prose is not checked. Deleting uuid from it still passes.
  • An import-time read written with open() still passes the import-deferral test.
  • A Position cell without backticks fails with AttributeError at line 324, and a renamed extension row fails with KeyError at lines 279-283, instead of a "docs changed" message.
  • The PR body says the tests "explicitly import this checkout's src/stickler". There is no conftest or sys.path change, so the import depends on the environment.

Comment thread tests/test_readme_examples.py Resolved
Comment thread tests/test_readme_examples.py Outdated
@pytest.mark.parametrize(
"measured, claimed", [(0.498, 0.502), (0.502, 0.498), (0.5, 0.499)]
)
def test_example_verdict_check_uses_the_field_threshold(monkeypatch, measured, claimed):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking. This test pins the field threshold (0.5) as the verdict line for items. The library does not use 0.5 there: 0.502 is fd=1 when Item.match_threshold = 0.8. After the _matching_threshold fix, this case should pass the check instead of raising.

Comment thread tests/test_readme_examples.py Outdated
test_readme_example_does_not_annotate_a_miss_as_a_pass("", "unused")


def test_an_element_threshold_crossing_does_not_change_the_fields_verdict(monkeypatch):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking. 0.798 vs 0.802 crosses Item.match_threshold = 0.8, so the library reports FD for one and TP for the other. That is the miss-annotated-as-a-pass case #436 item 2 targets. This test should expect an AssertionError.

The library decides a list-of-models field's TP/FD with the element
class's match_threshold, so the README verdict check now uses it for
those fields. A crossing of the element threshold fails the check; a
crossing of the field threshold alone passes.
@mangeshraut712

Copy link
Copy Markdown
Contributor Author

@vawsgit Fixed in d3761c8. For a list-of-models field, _matching_threshold now returns the element class's match_threshold, using the same unwrap_annotated / _annotation_is_list handling. The two tests are inverted: crossing 0.8 (0.798/0.802, 0.802/0.798, 0.8/0.799) now fails the check, and crossing only 0.5 passes it. The distinct-thresholds test now checks the field threshold (0.5) separately from the verdict threshold (0.8). If the field-threshold branch is restored, 8 tests fail.

tests/test_readme_examples.py 57 passed; non-integration suite 4179 passed, 13 skipped, 1 xfailed; Ruff clean. I left the LOW items as they are.

@vawsgit vawsgit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM!

Thanks, @mangeshraut712. d3761c8 fixes the blocking item from my last review, so Fixes #436 is now accurate. I measured at head d3761c8 with stickler imported from that checkout's src, on Python 3.12.

The blocking item is fixed. For a list-of-models field, _matching_threshold now returns the element class's match_threshold. Non-list fields still use their field threshold. The new _model_list_element helper keeps the _unwrap_optional / unwrap_annotated / _annotation_is_list handling.

Before and after, using _threshold_model() (Item.match_threshold = 0.8, field threshold 0.5) and one-item lists:

List score Library result Check at d1c5fee (0.5) Check at d3761c8 (0.8)
0.800 tp=1 fd=0 pass pass
0.799 tp=0 fd=1 pass (wrong) fail
0.700 tp=0 fd=1 pass (wrong) fail
0.502 tp=0 fd=1 pass (wrong) fail

The check gives the same result for the plain annotation and for the Annotated / Optional one. For non-list fields with threshold=0.9, the check still matches the library at 0.9, 0.899 and 0.6.

The two tests are inverted, and they catch the regression.

Restoring the field-threshold branch fails 8 tests, as you said. Other mutations of the changed code:

Mutation Tests that fail (of 57)
The list branch returns the parent's match_threshold 5
Drop either unwrap_annotated call 1
Drop the outer _unwrap_optional 9
Change >= to > in the verdict check 1
Turn off the verdict check 3

What was verified

  • The diff against current dev changes only tests/test_readme_examples.py and tests/README.md. It has no production or dependency changes.
  • At the head, the focused module gives 57 passed. The suite without tests/integrations gives 3555 passed, 2 skipped, 1 xfailed. The full suite gives 4196 passed, 3 skipped, 1 xfailed. Ruff passes.
  • The branch merges into current dev (b59c7ce) with no conflicts. After the merge, the focused module gives 57 passed and the full suite gives 4252 passed, 3 skipped, 1 xfailed.
  • The README rows use the correct threshold. The Quick Start and schema-example Line Items rows use the element's match_threshold (0.7). The scalar rows use their field thresholds.

Non-blocking (LOW)

  • _model_list_element raises IndexError: tuple index out of range at line 162 for a field annotated with a bare list. Before this commit, such a field returned its field threshold. No README model has such a field, and the error is a crash, not a silent pass.
  • The PR body is out of date. It still says "Field verdicts use field thresholds" and quotes "55 focused / 3,553 passed".
  • Your "non-integration suite 4179 passed, 13 skipped" matches neither of my runs. It looks like the full suite run with fewer optional extras installed. Nothing fails either way.
  • The LOW items from my last review still apply, as you said.

@mangeshraut712

Copy link
Copy Markdown
Contributor Author

Merged current dev, including the 1.0.2 cut, in 69801df. This PR has no changelog bullets, so CHANGELOG.md matches dev: Unreleased is empty and the 1.0.2 notes stay under ## [1.0.2]. Locally, tests/test_readme_examples.py passed (57 tests) and pytest tests/ --ignore=tests/integrations passed (4972 passed, 12 skipped, 1 xfailed). Ruff is clean on the PR files.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harden tests/test_readme_examples.py and pin the README's own defaults

3 participants