Skip to content

Validate inlined objects as the class their designates_type names - #177

Merged
caufieldjh merged 2 commits into
mainfrom
fix-designates-type-170
Nov 17, 2026
Merged

caufieldjh merged 2 commits into
mainfrom
fix-designates-type-170

Conversation

@caufieldjh

Copy link
Copy Markdown
Contributor

Closes #170.

Problem

When a slot's range is an abstract class and the inlined object names its concrete subclass through a designates_type slot, the plugin validated the object as the abstract range. Slots defined only on the subclass (e.g. references) were filtered out, so their titles and excerpts were never checked and validation reported success ("Titles checked: 0").

Fix

_validate_instance now calls a new _resolve_concrete_class before anything else. Resolving in that one place covers every route into it: nested single objects, each list item independently, each value of a keyed mapping (inlined_as_list: false), and the top-level --target-class.

The helper:

  • finds the range's type designator slot, including one inherited from an ancestor;
  • reads its value under the slot name or alias;
  • matches it as a class name, or as a CURIE or full URI against each class's URI;
  • uses the named class only if it is the range or a descendant (any depth). Otherwise, for an unknown name, an unrelated class, or a non-text value, it falls back to the declared range with a debug log.

Tests

tests/test_designates_type.py (14 tests) uses the schema from #170 and covers nested single objects, mixed-type lists, keyed mappings, a grandchild type, CURIE and URI values, fallback for unknown or unrelated values, the top-level target class, the helper directly, and the CLI reproduction (Titles checked: 1, exit 1). 10 of the 14 failed before the fix; the 4 fallback cases already passed. The helper also has doctests.

just test: 2594 passed, 6 failed, all network tests in test_pmid_network.py / test_html_fulltext_acceptance.py. Rerunning them on a clean main also fails, with a different set each run and NCBI timeouts in the log. mypy and ruff pass.

🤖 Generated with Claude Code

An inlined object in a slot whose range is an abstract class was validated
as that range, so slots defined only on the subclass it names (e.g.
references) were skipped and validation passed having checked nothing.

_validate_instance now resolves the concrete class through the range's
type designator slot (own or inherited) before anything else, accepting a
class name, CURIE or URI, and only a descendant of the declared range.
Resolving there covers single objects, list items, keyed mappings and the
top-level target class alike.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@claude

claude Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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


Code review: designates_type resolution (#170)

  • Read the diff and the surrounding _validate_instance code
  • Check every call site of the new helper
  • Review the tests
  • Post the review

I couldn't run the test suite because uv isn't installed on this runner. Everything below comes from reading the code.

Overall: looks good to merge. The fix is small and placed well. The tests cover the realistic cases. I found no correctness bugs and only have a few minor suggestions.

What works well

  • One resolution point. Resolving in _validate_instance (reference_validation_plugin.py:241-244) covers every path into it: the top-level --target-class (L214), nested objects (L380), list items (L394) and keyed-mapping values (L712). Because class_name is reassigned before the for slot_name, value in instance.items() loop, induced_slot(slot_name, class_name) also starts finding the subclass's slots. So nested objects that sit on subclass-only slots are now visited too, not just the reference and excerpt fields.
  • The data can only narrow the class, never widen it. The named class is used only if it is the declared range or a descendant of it. That is the right safety rule. Without it, a type: value in the data could pull in any class's reference or excerpt slots.
  • Inherited designators are found through class_induced_slots, and aliases are handled.
  • Non-text and empty values are guarded against (42 is tested).
  • Every caller only passes dicts (L366, L391, L708), so instance.get is safe.

Suggestions (all minor)

  1. A silent fallback can bring back the Inlined objects are validated as the slot's abstract range, ignoring designates_type, so subclass references go unchecked #170 symptom. If the value is unknown or unrelated, for example a typo like type: Organiod, the helper logs at debug and validates against the abstract range. That leads straight back to "Titles checked: 0" and a passing run, which is the bug this PR fixes. In normal use, nothing tells the user the type was ignored. I'd suggest one of these:

    • raise the log to logger.warning; or
    • yield a LinkMLValidationResult with severity=WARN from _validate_instance when a designator value is set but doesn't resolve.

    The helper would then need to tell _validate_instance it fell back, for example by returning Optional[str]. The existing test test_unknown_or_unrelated_type_falls_back_to_range already gives you the place to assert this.
    Fix this →

  2. Value forms are not tied to the slot's range. LinkML defines the expected form by the designator's range: string means a class name, uriorcurie means a CURIE, and uri means a full URI. The helper accepts all three forms whatever the range is. For a validator that is reasonable leniency, since the core LinkML validator is the place to enforce the exact form. It's worth one sentence in the docstring so nobody later "fixes" it into strict matching.

  3. Performance is fine. class_induced_slots and class_descendants run once per instance. Recent linkml_runtime caches both on SchemaView with lru_cache, so large lists should not slow down noticeably. The URI loop only runs when the value isn't a plain class name. If a profile ever flags it, the designator lookup could be memoized per class_name on the plugin, but there's no need to do that now.

  4. Possible extra test. Add a case where the subclass carries a nested inlined object on a slot that exists only on the subclass, for example an Organoid.protocol whose range has excerpts. This would pin the second-order effect from the first point under "What works well" (subclass-only slots are now recursed into). It works today because of where the resolution happens, but no test asserts it directly.

Security

No concerns. The data can't widen its declared range, and no new I/O is added.

Test coverage

Coverage is good. There are 14 tests built on the schema from the issue, covering single, list and keyed shapes, a grandchild class, CURIE and URI values, fallbacks, the top-level class and the CLI. The helper's doctest also documents the behaviour. You note that 10 of the 14 failed before the fix.

Falling back silently to the declared range on an unknown or unrelated
designates_type value (e.g. a typo) brought back the #170 symptom: a
passing run that checked none of the subclass's slots. The fallback now
yields a WARN result naming the value and the range, so the run fails.

Also document that class names, CURIEs and URIs are all accepted
whatever the designator's range, deliberately.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@claude

claude Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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


Re-review after 4b96105 (warn when a type designator names no usable class)

  • Read the new commit and the full diff against origin/main
  • Check how the CLI handles WARN results
  • Check the updated tests against my earlier suggestions
  • Post the review

I couldn't run the tests. uv isn't installed on this runner and linkml_runtime isn't importable, so everything below comes from reading the code.

Overall: ready to merge. The new commit fixes the main problem from my last review, a fallback that nobody saw, and does it properly.

How the earlier suggestions were handled

  1. The silent fallback can bring back Inlined objects are validated as the slot's abstract range, ignoring designates_type, so subclass references go unchecked #170. Fixed. _resolve_concrete_class now returns None when the designator is set but doesn't resolve. _validate_instance then yields a WARN result and still validates against the declared range (reference_validation_plugin.py:245-251). The CLI puts any file with results into failing_files (cli/validate.py:355-367), so a WARN also exits with code 1. The commit message says the run fails, and that matches. test_cli_reports_a_misspelled_type checks this end to end.
  2. Lenient value forms. Documented in the docstring with the reason for it. Good.
  3. Performance. Nothing new to raise.
  4. A test for nested objects on subclass-only slots. Already covered, and I missed that last time: Organoid.references is an inlined Reference that exists only on the subclass, so every positive test checks that recursion.

What works well in the new commit

  • The edge cases in the tests are well chosen. An absent value gives no warning, type: Model gives no warning, and "", 42 and the typo Organiod all warn. An empty string counting as "set but invalid" is the right call.
  • The warning tells the user what to do. It names the slot, the value as repr (so '' and 42 are clear) and the range, and it says that subclass-only slots were not checked.
  • The warning's instantiates/context use path, so the CLI's Location: line points at the right object.

Minor points (none block merging)

  • assert in library code (_unresolved_type_warning, around L697). Python's -O flag removes asserts, and then a None designator would fail with an AttributeError on .name. The only caller makes that impossible today. A cleaner option is to pass designator and value in from _validate_instance. That also saves the second walk through class_induced_slots that _type_designator does on the warning path. You could have _resolve_concrete_class return them, or have _validate_instance look up the designator once and pass it to both helpers. This is a tidy-up only.
  • Overlap with the core validator. If the designator's range is an enum, or the user also runs the JSON Schema validator, a typo may be reported twice: once by the core validator and once as this warning. The two messages say different things (an invalid value versus "your references weren't checked"), so I'd keep both. It may be worth one line in the docs.
  • type on the warning. It uses "reference_validation", the same as title and excerpt failures. Anyone who filters results by type can't tell "the quote didn't match" apart from "the class didn't resolve". A separate value such as "reference_validation_type_designator" would fix that if it ever matters. Only a suggestion.

Security

No concerns. Data still can't widen its declared range, and the commit adds no new I/O.

Test coverage

There are now 17 tests, plus the helper's doctest. They cover the warning (severity, location, message), the cases that should not warn, the helper returning None for unrelated and non-text values, and the CLI exit code for a typo.

@caufieldjh
caufieldjh merged commit 95a8645 into main Nov 17, 2026
5 checks passed
@caufieldjh
caufieldjh deleted the fix-designates-type-170 branch November 17, 2026 22:09
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.

Inlined objects are validated as the slot's abstract range, ignoring designates_type, so subclass references go unchecked

1 participant