Skip to content

Validate every listed excerpt and every object in a keyed mapping - #154

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

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

Conversation

@caufieldjh

Copy link
Copy Markdown
Contributor

Fixes #139. Fixes #141.

The validation plugin handled only some shapes of data. A list of excerpts crashed it, and evidence held in a keyed mapping was never looked at.

#141: a multivalued excerpt slot crashes validate data

With supporting_text: {multivalued: true}, the isinstance(excerpt_value, str) content check skipped lists, and the if not excerpt_value guard let a non-empty list through. The list then reached SupportingTextValidator.validate and failed with TypeError: expected string or bytes-like object, got 'list' (from re.subn).

Fix:

  • In _validate_instance, an excerpt slot holding a list is split into its items. Each item is reported at its own path (supporting_text[1]), and a single value is treated as a list of one.
  • Each item gets the content check (blank, too short, nothing quoted), now in _excerpt_problem. An item that isn't text is reported as an error rather than crashing.
  • Each substantive item is validated on its own, so a true excerpt next to a fabricated one doesn't carry it.
  • References are resolved once per excerpt field, so an unusable-reference error is reported once, not once per excerpt.

#139: evidence in a keyed mapping is never validated

In the slot traversal, a dict value was always treated as one nested instance. For a multivalued, class-ranged slot with inlined_as_list: false, the dict maps identifier → object. So the evidence objects inside were never visited, and a fabricated quote passed with exit 0.

Fix: a dict in a multivalued, class-ranged slot goes to _validate_keyed_objects:

  • Each object is validated at <slot>[<key>], for example has_evidence[E1].supporting_text.
  • The key is supplied as the identifier when the object omits it, as LinkML loaders do.
  • A null value is an object with only its key.
  • A scalar value (the compact form, which sets one non-key slot) can't hold both a reference and an excerpt, so it's left to schema validation.

Single-valued slots are unchanged.

Docs

docs/troubleshooting.md gets a section next to the existing multiple-references one.

Tests

tests/test_plugin_value_shapes.py uses a real schema, the fixture cache, and the LinkML Validator. It was written first; eight cases failed before the fix (TypeError, or "All validations passed!").

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


Generated by Claude Code

…, #141)

#141: a multivalued excerpt slot reached SupportingTextValidator as a
list and crashed validate data with TypeError. Each listed excerpt now
gets the content check and its own validation, reported at its own [i]
path, against references resolved once for the field; an item that is
not text is an error instead of a crash.

#139: a dict in a multivalued, class-ranged slot is a keyed mapping
(inlined_as_list: false), but was validated as one object, so the
evidence inside was never visited and a fabricated quote passed with
exit 0. Each object in it is now validated at <slot>[<key>], with the
key supplied as the identifier when the object omits 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 1m 18s —— View job


Review of PR #154

  • Gather context
  • Review plugin changes
  • Review tests and docs
  • Run tests: not done. Running uv run pytest needs permission that this job doesn't have, so I couldn't run the suite or my two probe cases. Everything below comes from reading the code.
  • Post review

Both fixes are well scoped. Splitting the excerpt list into (item, path) pairs is a clean approach. Resolving references once per field avoids duplicate unusable-reference errors, and _excerpt_problem turns a non-string value into a reported error instead of a crash. The tests use a real schema, the fixture cache and the real Validator, with no mocks. I have one likely regression and a few smaller points.


1. A single object in a multivalued slot may now pass silently (likely regression)

reference_validation_plugin.py:335: every dict in a multivalued, class-ranged slot now goes to _validate_keyed_objects. That includes a slot whose range class has no identifier. Such a slot can't be a keyed mapping, so the dict is really one object written without the list. YAML authors often write it that way:

# Evidence has no identifier; has_evidence: {multivalued: true, inlined: true}
has_evidence:
  reference: PMID:TEST001
  supporting_text: totally fabricated quote

Before this PR, this dict was checked as one Evidence, and the fabricated quote was caught. Now its keys are read as identifiers: "reference" → "PMID:TEST001" and "supporting_text" → "...". Both values are scalars, so the compact-form branch skips them, and nothing is validated. validate data runs only this plugin (cli/validate.py:297), with no JSON Schema check, so nothing else would reject the shape. The result would be exit 0. I traced this by reading the code; I couldn't run it.

Suggestion: treat the dict as a keyed mapping only when the range class has an identifier or key slot. Otherwise keep the old single-instance path. Add a regression test for this shape.

Fix this →

2. A key: true slot is not filled in from the mapping key

_validate_keyed_objects calls self.schema_view.get_identifier_slot(class_name). In linkml_runtime, that method's use_key parameter defaults to False, so a class that uses key: true instead of identifier: true gets None. Its mapping key is then never copied into the object. This matters when the key slot is a reference or title field, for example evidence keyed by reference. In that case the excerpt looks like it has no reference, and it is silently not checked. Passing use_key=True fixes this. It also makes the guard in point 1 cover both kinds of class.

3. Smaller points

  • Behaviour is unchanged for (lines 220–225):
    • A None inside an excerpt list (supporting_text: [QUOTE, null]): skipped without a message. That matches how an absent excerpt is treated. If you want it reported at supporting_text[1], it's a one-line change. Either way, a test would pin the choice down.
    • An empty list [] is still treated as no excerpt, as before. That's fine.
  • Type hint (line 219): excerpts: list[tuple[str, str]] is filled with item, which is Any. The type is correct only because _excerpt_problem rejects anything that isn't a string. A short comment, or an isinstance(item, str) in the elif, would make that link explicit.
  • Title-only path unchanged (lines 300–313): _extract_reference_id(ref_value) is still called on the whole value. A keyed object now reaches this code too. That's out of scope for this PR, but it's the same kind of shape gap if reference is multivalued.
  • Missing docstring example: _validate_keyed_objects has none. CLAUDE.md prefers doctests. A small example, for instance one showing the key being injected, would document the behaviour.

Performance and security

  • Performance: no concerns. References are resolved once per excerpt field, and each excerpt is checked once, as before.
  • Security: no concerns. The only new thing in message text is the type name, type(excerpt).__name__.

Test coverage

The new tests cover both issues' reproductions, a per-item failure next to a passing item, snippets_checked counts and CLI exit codes. These cases are missing:

  • a single object in a multivalued slot whose class has no identifier (point 1)
  • a class keyed with key: true (point 2)
  • a keyed-mapping entry whose value is null (E1: null, a code path the PR adds)
  • a keyed entry whose value is a scalar (the compact form, which is skipped)
  • a non-string excerpt item, e.g. [5], through the Validator (only covered by the doctest now)

Docs

The new troubleshooting.md section is clear and sits in the right place. If you add the guard from point 1, add one sentence saying a mapping is read as keyed only when the class has an identifier or key.


Summary: the approach is sound and the #141 fix looks correct. Before merging, I'd fix point 1: the #139 change probably makes a previously validated shape pass silently. The use_key=True change in point 2 is small and worth doing in the same edit.

…alues are objects

Review follow-up on #139. Every dict in a multivalued class-ranged slot
was walked as a keyed mapping, so a single object written without the
list had its fields read as keys and was skipped: a fabricated quote in
it passed again, where it had been caught before. A dict is now a keyed
mapping only if the range class has an identifier or key slot and every
value is an object or null; otherwise it is validated as one object.

The key also fills a key: true slot now (use_key=True), so evidence
keyed by its reference is checked against that reference.

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

Copy link
Copy Markdown
Contributor Author

Points 1 and 2 are fixed in e0d5951. Both were regressions I introduced, and both are now covered by tests that failed before the fix.

  1. A single object written without the list: this was a real regression. A dict now counts as a keyed mapping only when the range class has an identifier or key slot and every value is an object or null (_is_keyed_mapping). I added the second condition as well, because a class with an identifier written as one object ({id: E1, reference: …, supporting_text: …}) had the same problem. Anything else is validated as one object, as before. There are tests for both the class without an identifier and the class with one.
  2. key: true: get_identifier_slot(..., use_key=True) is now used in both places. Evidence keyed by its key: true reference is checked against that reference, and a test covers it.
  3. Smaller points:
    • New tests through the Validator: E1: null, a compact scalar entry, [QUOTE, 5] (error at [1]), and [QUOTE, null], which is skipped like an absent excerpt.
    • The elif now checks isinstance(item, str) and has a comment, so the type is explicit.
    • The docs note when a dict counts as keyed.
    • Title-only path: left for Title-only validation silently skips list-valued references and references without an id #146, which covers exactly this.
    • No doctest on _validate_keyed_objects: it needs a SchemaView, and the plugin tests cover it end to end.

Full suite: 2202 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 47s —— View job


Re-review after e0d5951

  • Gather context (diff of the new commit)
  • Check that the earlier points are addressed
  • Look for new edge cases
  • Run tests: not done. This job isn't allowed to run uv run pytest, so I couldn't run the tests. Everything below comes from reading the code.
  • Post review

The new commit fixes both main points from the last review:

  • Point 1 (single object read as a keyed mapping): fixed. _is_keyed_mapping now asks for an identifier or key slot, and for every value to be an object or null. Both shapes of the earlier regression go back to the single-instance path, and two tests pin them down: a class with no identifier, and a class with an identifier whose fields are scalars.
  • Point 2 (key: true slot not filled in): fixed. use_key=True is now passed in both places. test_a_key_slot_is_filled_in_from_the_mapping_key covers evidence keyed by its reference.
  • Smaller points: handled. There's now an isinstance(item, str) guard with a comment, and tests for a [QUOTE, None] list, a [QUOTE, 5] list, E1: null, and the compact form.

I have one remaining concern and a few small points.


1. A mapping that mixes objects and compact entries hides all of its evidence

reference_validation_plugin.py:408: all(obj is None or isinstance(obj, dict) ...) decides for the whole dict. One compact (scalar) entry is enough to make the mapping count as a single object:

has_evidence:
  E1: {reference: PMID:TEST001, supporting_text: FABRICATED}
  E2: some compact value

Here _is_keyed_mapping returns False, so the dict is checked as one Evidence. In _validate_instance, E1 and E2 aren't slots of the class (line 324), so both are skipped. E1's fabricated quote passes and the command exits 0. That is the #139 symptom again, for a narrower shape. The current test checks {E1: None, E3: {...}} and {E2: "..."} on their own, but never together.

Suggestion: decide by the keys, not the values. A dict is one object if any of its keys is a slot of the range class (class_slots(range_class), which includes aliases if you use them). Otherwise it's a keyed mapping, and _validate_keyed_objects already skips scalar entries one at a time.

This also covers an odd shape the value test misses: one object whose fields are all null or nested objects. That's unlikely to matter much, but it would follow the same rule.

Fix this →

2. Smaller points

  • Unreachable branch and outdated docstring (lines 413–427): _is_keyed_mapping now only lets through values that are dicts or null. So in _validate_keyed_objects, if not isinstance(obj, dict): continue can never run, and the docstring's text about the compact form no longer matches what the code does. If you adopt the key-based check from point 1, that branch becomes useful again and the docstring is correct as written.
  • Missing doctest: _is_keyed_mapping is a small pure decision and an easy doctest target, which CLAUDE.md prefers. You'd need a SchemaView built from a short YAML string.
  • Line length: a few new lines are longer than 88 characters: line 336 in the plugin, and lines 300, 316, 323 and 330 in the test file. ruff check with the default rules won't complain, but ruff format --check would rewrap them if it runs in CI.
  • Docs (troubleshooting.md:758): the added sentence makes that line very long compared with the paragraph around it. If point 1 changes the rule, the sentence will need new wording anyway.

Performance and security

  • Performance: no concerns. _is_keyed_mapping makes one linear pass over the values, and get_identifier_slot is cached by SchemaView.
  • Security: no concerns.

Test coverage

The tests now cover every case I listed last time. The only gap left is the mixed mapping from point 1.


Summary: the follow-up commit fixes the regression I flagged and the key: true gap, and it adds tests for both. Before merging, I'd change _is_keyed_mapping to decide by the keys rather than the values, so that a single compact entry can't hide the rest of the mapping. The other points are minor.
· Branch: ccr-65a5480d-pb91sk

…jects

Review follow-up on #139. Requiring every value to be an object meant one
compact (scalar) entry made a whole mapping read as a single object, and
the evidence objects beside it went unchecked again. A dict is now one
object only if one of its keys is a slot of the class (or an alias);
otherwise, for a class with an identifier or key, it is a keyed mapping
and compact entries are skipped one by one.

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

Copy link
Copy Markdown
Contributor Author

Fixed in 677bf65 by switching to your key-based rule.

  1. Mixed mapping: this was a real gap in my value-based check. _is_keyed_mapping now looks at the keys. A dict is one object if any key is a slot of the range class or a slot's alias. Otherwise, for a class with an identifier or key slot, it's a keyed mapping. A new test covers {E1: {…FABRICATED}, E2: "compact value"}, which failed before. Both earlier shapes still go down the single-object path, and their tests still pass.
  2. Unreachable branch and outdated docstring: with the new check, the scalar-entry branch in _validate_keyed_objects is reachable again, and its docstring is accurate as written. The troubleshooting docs now describe the key-based rule.
  • Doctest for _is_keyed_mapping: no doctest. It needs a loaded SchemaView, and the plugin tests cover each branch through the real Validator.

Full suite: 2203 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 46s —— View job


Re-review after 677bf65

  • Gather context (diff of the new commit)
  • Check the key-based _is_keyed_mapping and its tests
  • Run tests: not done. This job isn't allowed to run uv run pytest, so I couldn't run the suite or the new test. Everything below comes from reading the code.
  • Post review

This commit fixes the point left over from the last review. I found nothing that should block merging.


The mixed mapping: fixed

_is_keyed_mapping (reference_validation_plugin.py:395) now decides by the keys instead of the values:

  • If any key is a slot of the class, or a slot's alias, the dict is one object written without the list.
  • Otherwise, if the class has an identifier or key slot, the dict is a keyed mapping.

I checked the cases from earlier reviews against the new rule:

Shape Result
{E1: {...FABRICATED}, E2: "compact"} (the case I raised last time) Keyed. E1 is checked and E2 is skipped. The new test_a_compact_entry_does_not_hide_the_objects_beside_it covers this.
A class with no identifier, written as one object get_identifier_slot returns None, so it's one object, as it was before this PR.
A class with an identifier, written as one object ({id: E1, reference: …}) id is a slot, so it's one object.
A class keyed with key: true on reference (keys are PMIDs) Keyed, and the key is filled in.
E1: null Keyed. The value becomes {id: E1}.

Since a compact entry can now reach _validate_keyed_objects, its if not isinstance(obj, dict): continue branch runs again, and the docstring is accurate again. That resolves the small point about the unreachable branch.

Smaller points (none blocking)

  • A key that happens to be a slot name: an identifier spelled like a slot (for example {reference: {...}, E2: {...}}) would make the whole mapping read as one object. That's very unlikely, and preferring the single-object reading when it's ambiguous is the safer default. No change needed, but a short comment would record that the trade-off was deliberate.
  • Aliases are handled in one place only: _is_keyed_mapping treats aliases as slot names, but _validate_instance (line 321) still matches keys against class_slots only. So a single object written with alias keys is correctly treated as one object, and then its alias-named fields are skipped. This was already the case before this PR, but the new alias check implies support that the rest of the traversal doesn't have. You could either drop the alias part for consistency or follow it up in an issue.
  • Missing doctest: _is_keyed_mapping still has none. It's small enough to cover with a SchemaView built from a short YAML string, which CLAUDE.md prefers. This is optional, since the tests through the Validator cover it.
  • Formatting:
    • The new test adds another line longer than 88 characters (assert [r.instantiates ...] == ["has_evidence[E1].supporting_text"]). It's worth running just format in case CI checks ruff format.
    • In docs/troubleshooting.md:762, "…being checked. Before this, the objects…" is still one very long line. Rewrapping it would match the paragraph around it.

Performance and security

  • Performance: no concerns. _is_keyed_mapping now calls induced_slot for each class slot, for every dict in a multivalued, class-ranged slot. SchemaView caches induced_slot, so this is cheap. If it ever shows up in profiling, you could cache the slot and alias set per class.
  • Security: no concerns.

Test coverage

All the gaps from the two earlier reviews now have tests, including the mixed mapping. The one remaining option is a doctest for _is_keyed_mapping.


Summary: all three rounds of feedback are addressed. #141 and #139 look correct to me from reading the code, and the earlier single-object regressions are covered by tests. I couldn't run the tests in this job, so please make sure CI is green before merging.
· Branch: ccr-65a5480d-pb91sk

Copy link
Copy Markdown
Contributor Author

Thanks. None of these needs a push on its own:

  • A key spelled like a slot name: preferring the single-object reading in that case is deliberate, as you say. The docstring states the rule.
  • Aliases: I'm keeping the alias check. Dropping it would make a single object with alias keys read as a keyed mapping, so its scalar fields would be skipped one by one. That's no better than today. Having the traversal itself honour aliases is a separate gap that predates this PR.
  • ruff format: CI runs ruff check ., which passes. main already fails ruff format --check.
  • Docs line wrap and doctest: I'll do both if this PR gets another code push.

Generated by Claude Code

@caufieldjh
caufieldjh merged commit eb24a1d into main Oct 28, 2026
5 checks passed
@caufieldjh
caufieldjh deleted the ccr-65a5480d-pb91sk branch October 28, 2026 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants