Skip to content

Validate titles over every reference shape, with or without an excerpt - #155

Merged
caufieldjh merged 4 commits into
mainfrom
ccr-65a5480d-pb91sk
Oct 28, 2026
Merged

caufieldjh merged 4 commits into
mainfrom
ccr-65a5480d-pb91sk

Conversation

@caufieldjh

@caufieldjh caufieldjh commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #146.

Problem

When an instance has a title but no excerpt, the plugin validates the title on its own. That branch passed the whole reference value to _extract_reference_id, which returns None for a list and for an object with no id. The title was then skipped without a word, so a wrong title passed with exit 0. This is the title-only counterpart of #140, which fixed the same gap for excerpts.

A related gap: with an excerpt and several references, the title field was ignored entirely. So the same wrong title failed without an excerpt but passed with one.

Changes

plugins/reference_validation_plugin.py:

  • Shared reference resolution. The excerpt path's resolution is now _resolve_references, shared by both paths. It handles lists, objects, and fields tried in turn. It returns ResolvedReferences plus the unusable raw values with their paths, so each caller builds its "no usable identifier" error with the path it leaves unchecked. Behaviour for excerpts is unchanged; the existing A multivalued reference slot makes excerpts silently unchecked (exit 0) #140 tests pin it.
  • _validate_titles_only replaces the old inline branch:
    • A title field and one reference: checked as before. A one-item list now counts as one reference.
    • A title field and several references: a title names one paper, so it passes if it is any listed reference's title. This is the first option in the issue, and it matches how excerpts treat several references.
    • No title field: each reference object that carries its own title, listed or not, is checked against its own reference.
    • A reference with no usable identifier is reported (Reference at '...' has no usable identifier, so 'reference[0].title' cannot be checked). It is reported only when that leaves a title unchecked.
  • _validate_title_any mirrors _validate_excerpt_any:
    • The first match passes, and the remaining references aren't fetched.
    • A skipped prefix vouches for nothing, and it is named in the error.
    • The title counts once.
    • If nothing matches, one error names each reference with its own reason.
  • A title field beside an excerpt and several references is now checked the same way with _validate_title_any, once per instance however many excerpt slots the class has. It replaces the reference objects' own titles in the excerpt check, as it already did for a single reference. So the excerpt may come from one cited paper while the title names another.
  • No double reporting. Title-only validation now runs only when no excerpt was attempted. Otherwise an excerpt with an unusable reference would report it twice.

Docs: docs/how-to/validate-titles.md describes the rules for each reference shape, with and without an excerpt.

Behaviour changes to note

These cases may change results for existing data:

  • A reference object carrying its own title, with no excerpt and no title field beside it, now has that title checked. This applies to a single object as well as a list. (may add errors)
  • A title field beside an excerpt and several references is now checked: it must be one of the cited references' titles. (may add errors)
  • Listed reference objects with their own titles, beside an excerpt and a title field: the title field now replaces the objects' own titles, as it already did for a single reference. An object whose own title was wrong used to fail the excerpt check; now only the title field is checked. (loosens a check that used to fail)

Tests

tests/test_title_only_shapes.py, written first; each group failed before its fix:

  • The issue's reproductions: a one-item list, and an object with no id.
  • Several references: the title matches any of them; matches none, with the error naming each reference and any skipped prefix; all references skipped.
  • Objects with their own titles: listed, a single object, and one with no id (the error names reference[0].title).
  • Beside an excerpt with several references: a wrong title fails; any cited reference's title passes; excerpt and title failures are both reported; two excerpt slots check the title once; only skipped references check nothing.
  • Interactions: a title field wins over an object's own title; an unusable reference beside an excerpt and a title is reported once; with no title anywhere, nothing is reported.
  • The plain-string case is unchanged, and the CLI exits 1.

Full suite: 2226 passed. Doctests, mypy and ruff are clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Qm4PkMsSN3FVpzvBdZYahC

…146)

Title-only validation passed the whole reference value to
_extract_reference_id, so a list, or an object with no identifier, gave
None and the title was skipped without a word: a wrong title passed with
exit 0. The excerpt path already handled these shapes (#140); its
reference resolution is now _resolve_references, shared by both.

With no excerpt to check:
- a title field is checked against the one reference, or, with several,
  passes if it is any one's title (_validate_title_any, mirroring
  _validate_excerpt_any); otherwise one error names each reference;
- without a title field, each reference object carrying its own title,
  listed or not, is checked against its own reference;
- a reference with no usable identifier is reported.

Title-only validation runs only when no excerpt was attempted, so an
unusable reference is not reported twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qm4PkMsSN3FVpzvBdZYahC
@claude

claude Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @caufieldjh's task in 1m 7s —— View job


Review: title validation for every reference shape (#146)

  • Read diff
  • Compare with the existing excerpt helpers
  • Run tests (I couldn't: the uv run pytest command needed approval in this job, so I haven't run them. The PR reports 2215 passed.)
  • Post review

Summary: the PR looks good. Moving the reference resolution into _resolve_references fixes the bug properly instead of patching the title branch on its own. It also keeps the excerpt and title paths from drifting apart again. Switching from validated_with_excerpt to excerpt_attempted is correct, and the reasoning is clear. None of the points below block the merge. Most are about consistency.

Possible issues

  1. Wrong path in the "no usable identifier" message when there is no title field (reference_validation_plugin.py:387). In the no-title branch, checked_path is set to f"{path}.title". That field doesn't exist on the instance; the title that goes unchecked is the reference object's own title. For {"reference": [{"title": "X"}]} the message says ... so 'title' cannot be checked, but it should say 'reference[0].title'. A fix would be to build the error per item using f"{item_path}.title", or to let _resolve_references take a callable for the checked path.
    Fix this →

  2. The unusable-reference filter depends on the shape of the error object (:390-393). It recovers the original value with (problem.instance or {}).get("reference"), which only works because _unusable_reference happens to store {"reference": value}. If _resolve_references returned the unusable raw items alongside their paths, the caller could filter on those before building the errors, and this coupling would go away. Fixing this would also make Bump actions/checkout from 4 to 5 #1 easy.

  3. _validate_title_any leaves out the skipped references (:410-444). _validate_excerpt_any adds ; not checked (skipped prefix): GO:123 to its message, but the title version silently drops skipped IDs. For ["GO:123", "PMID:TEST001"] with a wrong title, the error says "matches none of the 1 cited references". That's accurate, but it's less helpful than the excerpt message. I'd mirror the excerpt version, since the docstring says it does.

  4. Every reference skipped means the title passes without a word. In _validate_title_any, if every reference is skipped then results is empty and it returns, and nothing is counted. That matches the single-reference behaviour, so it's probably fine. There's no test pinning it, though, and the excerpt version does count it (snippets_skipped).

  5. A related gap the PR doesn't cover (it was already there before). If there is an excerpt and several references, the title field is ignored entirely (the else branch at :257). The title-only path now checks a title against any of several references, so {"reference": [A, B], "title": WRONG, "supporting_text": ...} passes, while the same data without the excerpt fails. That may surprise users. It's worth a follow-up issue, or a line in the docs.

Code quality

  • The docstrings are thorough and follow the repo's style. The new private helpers have no doctests, but the existing helpers mostly don't either.
  • _resolve_references returns a 3-tuple that the excerpt path then reduces to 2-tuples (:243). A small NamedTuple (id, title, path) would make resolved[0][0] and the _, _ unpacking easier to read.
  • The behaviour change described in the PR body (reference objects that carry their own title are now checked) is real and worth a changelog or release-note entry.

Tests

The coverage is good: the tests were written first, they reproduce the issue, and the CLI test checks the exit code. Missing:

Performance and security

  • Performance: no concerns. _validate_title_any stops at the first match, and fetches are cached.
  • Security: no concerns. There is no new I/O or input handling beyond what was already there.

…refixes

Review follow-up on #146.

- With no title field, an unusable reference object's error named a
  nonexistent "title" field; it now names the object's own title path,
  e.g. reference[0].title. _resolve_references returns the unusable raw
  values with their paths, so each caller builds its errors with the
  path it leaves unchecked, instead of unpacking them from the result.
- _validate_title_any names references skipped by prefix, as
  _validate_excerpt_any does.
- ResolvedReference names the resolver's fields.
- Tests: all references skipped, an excerpt with an unusable reference
  and a title reported once, a title field winning over an object's own
  title. Docs: how a title field is used beside an excerpt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qm4PkMsSN3FVpzvBdZYahC

Copy link
Copy Markdown
Contributor Author

Addressed in 917c57a.

  1. Wrong title path: a real defect in my code, now fixed. The error now names the object's own title path, e.g. so 'reference[0].title' cannot be checked. A test asserts the message.
  2. Coupling to the error object: fixed the way you suggested. _resolve_references returns the unusable raw values with their paths, and each caller builds its errors with the path it leaves unchecked. ResolvedReference (a NamedTuple) replaces the 3-tuple. The resolver also has doctests now.
  3. Skipped prefixes: _validate_title_any now adds ; not checked (skipped prefix): …, mirroring the excerpt version. There's a test for it.
  4. All references skipped: left as is, with a test pinning it: the title isn't checked and isn't counted. There's no titles-skipped counter to add to, and a single skipped reference already behaves this way.
  5. Excerpt plus several references: not changed. A multivalued reference slot makes excerpts silently unchecked (exit 0) #140 chose to ignore the title field in that case on purpose (A title field names the paper, so it applies only when one paper is cited; listed objects carry their own), so I've documented it in validate-titles.md rather than change it here. I'll raise it as a possible follow-up.

The other tests you suggested are added: an excerpt with an unusable reference plus a title is reported once, and a title field wins over an object's own title.

Full suite: 2220 passed. Doctests, mypy and ruff are clean.


Generated by Claude Code

@claude

claude Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @caufieldjh's task in 58s —— View job


Review of the follow-up commit (917c57a)

  • Read the diff for 917c57a
  • Check each follow-up against the previous review
  • Run tests: I couldn't. uv run pytest needs approval in this job, so the new doctest and tests haven't been run here. Please check that CI is green.
  • Post review

Summary: the commit deals with every point from the previous review, and I found nothing that blocks the merge. The cleanest part is changing _resolve_references to return the raw (value, path) pairs, so each caller builds its own error with its own path. That fixes the wrong path (#1) and removes the dependence on the error object's shape (#2) in one change.

The previous points, one by one

# Point Status
1 Wrong path in the error when there is no title field ✅ The error now uses f"{value_path}.title" (reference_validation_plugin.py:411). A test checks for reference[0].title.
2 Filter depended on the shape of the error object ✅ The filter now checks _extract_title(value) on the raw value (:409-411).
3 Skipped references weren't named in the error ✅ The error now names them, in the same form as _validate_excerpt_any (:452-453, compare :873-874).
4 Title passes without a word when every reference is skipped ✅ A test now covers it (test_a_title_against_only_skipped_references_is_not_checked).
5 Title field is ignored when there is an excerpt and several references ✅ The docs now say so. Fixing the behaviour is still a possible follow-up.
— Using a NamedTuple ✅ ResolvedReference is added, and resolved[0].reference_id reads much better.
— Missing tests ✅ Both added: the excerpt + unusable reference + title case, reported once, and the title field winning over an object's own title.

Minor points (none block the merge)

  1. Error message for a mixed list when there is a title field (:394-395). Take {"reference": ["PMID:1", {"title": "X"}], "title": T}. The {"title": "X"} item is reported with "…so 'title' cannot be checked". But title was checked, against PMID:1. The excerpt path has the same wording (:248-249), so this is consistent and older than this PR. Wording such as "…is not checked against it" would be more accurate for both paths. Up to you.
  2. The count in the title message leaves out skipped references (:458). "matches none of the 1 cited references" counts only the references that were checked. The not checked (skipped prefix) suffix makes this clear, and the excerpt message does the same, so it's fine as is.
  3. Counters. _validate_excerpt_any adds to snippets_skipped when every reference is skipped. The title path has no matching counter, so a title with every reference skipped doesn't show up in the summary. This is consistent with the single-reference _validate_title, so I'd leave it. I mention it only in case summary stats matter to you.
  4. New doctest on a private method (:341-347). This is good: it shows both return shapes. It builds the plugin the same way as the existing _excerpt_problem doctest, so it should run under just doctest. I couldn't confirm that here.

Code quality, performance, security

  • Docstrings match the new return type, and the comments explain the reasoning.
  • Performance: no change. The first match still stops the search, and fetches are cached.
  • Security: no concerns.

Test coverage

With this commit the coverage looks complete for the shapes described in the PR. That includes the regression where the same unusable reference could be reported twice, which now has its own test.

Copy link
Copy Markdown
Contributor Author

Thanks. These stay as they are:

  1. Mixed-list wording: both paths use this wording, and it predates this PR. Rewording "cannot be checked" belongs in its own change.
  2. The count: it matches the excerpt message, and the skipped suffix explains it.
  3. Counters: there's no titles-skipped counter yet. Adding one is a separate change to the summary output.
  4. The doctest: it runs under --doctest-modules. 297 passed locally.

Generated by Claude Code

With an excerpt and several references, the title field was ignored, so
the same wrong title failed without an excerpt but passed with one. It
is now checked once against the cited references and passes if it is
any one's title, as in title-only validation; it replaces any title a
reference object carries, as it already did for a single reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qm4PkMsSN3FVpzvBdZYahC
@claude

claude Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @caufieldjh's task in 49s —— View job


Review of the latest commit (351e714): a title field checked beside an excerpt and several references

  • Read the diff for 351e714 and the code around it
  • Compare with the single-reference path and with _validate_excerpt_any / _validate_title_any
  • Run tests: I couldn't, because uv run pytest needs approval in this job. Please check that CI is green.
  • Post review

Summary: this commit closes the gap that point 5 of the first review raised. Before it, a wrong title failed when there was no excerpt but passed when there was one. The approach is sound. The excerpt is checked against every reference with no title ((reference_id, None)), and the title field is checked once with _validate_title_any. That lets the excerpt come from one paper while the title names another, which matches what "any reference" means elsewhere. The tests cover a failure, a pass with either title (parametrized), and both errors reported together. I found nothing that blocks the merge.

Possible issues (none block the merge)

  1. The title is checked once per excerpt field, not once per instance (reference_validation_plugin.py:279-286). The _validate_title_any call sits inside for excerpt_field in excerpt_fields. If a class has two excerpt slots (say supporting_text and a second oa:exact slot), a wrong title gets reported twice and titles_checked goes up by 2. That's rare, and the single-reference path already checks the title once per excerpt item, so the rule isn't new. Still, here the title is checked "on its own" anyway. Moving the check after the loop would make it truly once per instance. One way: keep the last resolved references and the title field, then check after the loop when there are several references and a title field.
    Fix this →

  2. One reference and several references now report a wrong title differently. With one reference, the title goes into validator.validate(...). A mismatch comes back as one error at the excerpt path, and its message replaces the excerpt's own message (supporting_text_validator.py:295-296). So when the title and the excerpt are both wrong, only the title error shows. With several references (this commit), the title gets its own error at the title path, and an excerpt failure is reported separately, as test_excerpt_and_title_failures_beside_several_references_are_both_reported pins. The multi-reference behaviour is the better one. But a user who adds a second reference will see the error move to a different path. The single-reference behaviour predates this PR, so it's a follow-up, not something to fix here. It may be worth a line in the docs or a separate issue.

  3. A quieter change: listed objects' own titles are now ignored when there is a title field. Take {"reference": [{"id": A, "title": WRONG}, {"id": B}], "title": <A's real title>, "supporting_text": ...}. Before this commit, A's wrong title was checked with the excerpt and failed. Now the title field replaces it and the data passes. That follows from "the title field wins", which is consistent with the single-reference case and with test_a_title_field_wins_over_a_reference_objects_own_title. The docs say so too ("It replaces any title a reference object carries"). It does loosen a check that used to fail, though, so I'd add it to the PR body's "Behaviour change" section next to the one already listed.

  4. The unusable-reference message names only the excerpt (:247-248). Example: an excerpt plus a title field plus [{"title": "X"}] (no id). The error says so 'supporting_text' cannot be checked, but the title can't be checked either. This is the same wording issue you've already set aside for its own change, so I'm only noting that it now affects this path too.

Code quality

  • title_field = next((f for f in title_fields if instance.get(f)), None) is tidier than the old loop with break, and the single-reference branch reads better for it.
  • The comments explain why the excerpt is checked with no title and why the title is checked once. Good.
  • [(reference_id, None) for reference_id, _ in references] and [reference_id for reference_id, _ in references] could build from resolved directly (ref.reference_id). That's a small readability point only.

Tests

The new cases are good and include the titles_checked == 1 assertion. Still missing, if you want them:

  • a wrong title beside an excerpt where every reference is skipped (should pass and not be counted, as in the title-only test)
  • two excerpt slots with several references and a wrong title, which would pin Bump actions/checkout from 4 to 5 #1
  • the error message naming a skipped prefix in this path (it's already covered for the title-only path, and the code is shared, so this one is optional)

Performance and security

  • Performance: about one extra validate_title call per instance with several references. It stops at the first match and fetches are cached, so this is negligible.
  • Security: no concerns. There is no new I/O or input handling.

@caufieldjh caufieldjh changed the title Validate titles over every reference shape when there is no excerpt Validate titles over every reference shape, with or without an excerpt Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

351e714 resolves point 5 from the first review: a title field beside an excerpt and several references is now checked against all the cited references, and passes if it is any one's title. I've updated the PR description and validate-titles.md to match.


Generated by Claude Code

With several references, the title check sat inside the excerpt-field loop,
so a class with two excerpt slots reported a wrong title twice and counted
it twice. The check is now recorded in the loop and run once after it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Qm4PkMsSN3FVpzvBdZYahC
@claude

claude Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @caufieldjh's task in 28s —— View job


Review of the latest commit (9079bde): the title field is now checked once per instance

  • Read the diff for 9079bde and the code around it
  • Check it against point 1 of the previous review
  • Run tests: I couldn't, because uv run pytest needs approval in this job. Please check that CI is green.
  • Post review

Summary: this commit fixes point 1 from the last review. The title check used to sit inside the for excerpt_field loop. Now the loop only records it in pending_title, and _validate_title_any runs once after the loop (reference_validation_plugin.py:286-287). The change is small and correct, and I found nothing that blocks the merge.

Correctness

  • Recording pending_title again on each loop pass is safe. _resolve_references(instance, reference_fields, path) and title_field depend only on the instance, not on the excerpt field. So each pass that reaches line 284 stores the same tuple, and the last one wins without changing anything.
  • An excerpt slot that is empty or invalid is handled. Such a slot hits the continue at :243 and never sets pending_title. The title is still checked if any other slot sets it. If no slot does, then excerpt_attempted is False and _validate_titles_only handles the title. It can't be checked twice, because pending_title is set only after excerpt_attempted = True.
  • Error order: the title error now comes after the errors from every excerpt slot, not between them. That's a harmless change of order. The existing test sorts the results before comparing them, so it isn't affected.

Minor points (none block the merge)

  1. Instance-level work still runs once per excerpt slot (:248-258). _resolve_references and title_field are worked out again for every excerpt field, though they don't change. The cost is small: there is no I/O there. The readability point is that this is now the only per-slot work that doesn't depend on the slot. If you moved both above the loop, pending_title could become a simple flag, or the multi-reference title check could move out of the loop altogether.
  2. The same duplication remains for unusable references (:251-252). With two excerpt slots and a reference that has no usable identifier, the "no usable identifier" error is reported once per slot. The errors aren't identical, because each names a different excerpt path, so you can argue it's correct: each slot really is left unchecked. I'm noting it because it's the same issue this commit fixes for the title. It's older than this PR, so it's fine to leave.
  3. The type annotation Optional[tuple[str, list[str], str]] matches what _validate_title_any takes, so unpacking it with *pending_title is fine. A NamedTuple like ResolvedReference would label the three fields, but the comment at :216-217 already says what they are.

Tests

  • test_a_title_beside_two_excerpt_slots_is_checked_once covers the fix. It asserts one result, snippets_checked == 2 (both slots were still checked) and titles_checked == 1. These are the right three things to check. Using an oa:exact slot as the second excerpt field is a good way to get two excerpt slots.
  • test_a_title_beside_an_excerpt_and_only_skipped_references_is_not_checked covers the "every reference skipped" case on the excerpt path, which the last review listed as missing.
  • The tests now cover each point raised in the three reviews, apart from the follow-ups you've already set aside (the "cannot be checked" wording, a counter for skipped titles, and how one reference reports a wrong title compared with several).

Performance and security

  • Performance: a little better. With several excerpt slots, the title check now runs once per instance, not once per slot.
  • Security: no concerns. There is no new I/O or input handling.

Copy link
Copy Markdown
Contributor Author

Addressed the 351e714 review in 9079bde:

  1. Title checked once per excerpt field: fixed. With several references, the check is recorded inside the excerpt-field loop and runs once after it. New tests: test_a_title_beside_two_excerpt_slots_is_checked_once (one error, titles_checked == 1) and test_a_title_beside_an_excerpt_and_only_skipped_references_is_not_checked.
  2. Different error path for one vs. several references: this stays as is. The single-reference path folds the title into _validate_excerpt, which is the behaviour on main. That also means a single reference with two excerpt slots still checks the title once per slot, and that case predates this PR too. Both belong to the same follow-up: pulling the title out of _validate_excerpt for every shape. That change moves error paths for existing data, so it shouldn't ride on this PR.
  3. The listed-object-title loosening: added to "Behaviour changes to note" in the PR body.
  4. Deferred, as before.
  5. Building the IDs from resolved: done in the several-references branch.

Full suite: 2226 passed. Doctests, mypy and ruff are clean.


Generated by Claude Code

@caufieldjh
caufieldjh merged commit 0eccb0f into main Oct 28, 2026
7 checks passed
@caufieldjh
caufieldjh deleted the ccr-65a5480d-pb91sk branch October 28, 2026 21:19
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.

Title-only validation silently skips list-valued references and references without an id

2 participants