Skip to content

Validate types of values set by links and identify the link in type errors - #982

Merged
mauvilsa merged 2 commits into
mainfrom
link-target-type-validation
Sep 18, 2026
Merged

mauvilsa merged 2 commits into
mainfrom
link-target-type-validation

Conversation

@mauvilsa

@mauvilsa mauvilsa commented Sep 18, 2026

Copy link
Copy Markdown
Owner

What does this PR do?

Values set by links applied on instantiate were not validated against the type of the target, so e.g. a compute_fn returning a wrong type silently passed an invalid value to the class. Now these values are validated. Type errors from any link also name the link (Invalid value for link '<link>': ...), both for parse and instantiate links, which makes failures much easier to trace.

Related fixes found while working on this:

  • link_arguments raised AttributeError when the target was a class group added with instantiate=False.
  • Required init_args parameters that are link targets are no longer included in the parsed namespace with a None value; the link sets them when applied.
  • A value given for a link target in a default (e.g. lazy_instance(Cls, p1=2)) is now removed, since the link provides it.

Before submitting

  • Did you read the contributing guideline?
  • If you used a coding agent, did you fully understand and validate all generated code and ensure it follows the contributing guidelines?
  • Did you update the documentation? (readme and public docstrings)
  • Did you write unit tests such that there is 100% coverage on related code? (required for bug fixes and new features)
  • Did you verify that new and existing tests pass locally?
  • If this is a bug fix, did you verify that the tests fail without the code fix?
  • Did you make sure that all changes preserve backward compatibility?
  • Did you update the CHANGELOG including a pull request link? (not for typos, docs, test updates, or minor internal changes/refactors)

@mauvilsa mauvilsa added the bug Something isn't working label Sep 18, 2026
@codecov

codecov Bot commented Sep 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (2d109be) to head (34ab624).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #982   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines         9114      9162   +48     
=========================================
+ Hits          9114      9162   +48     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@sonarqubecloud

Copy link
Copy Markdown

@greptile-apps

greptile-apps Bot commented Sep 18, 2026

Copy link
Copy Markdown

RetriggerConfidence Score: 4/5

The PR should not merge until instantiate-link validation assigns the normalized value rather than the original value.

Findings

  1. P1 Normalized value is discarded

Summary

This PR improves argument links by identifying links in type errors, validating values produced during instantiation, omitting required linked subclass parameters from parsed namespaces, removing linked values from subclass defaults, and supporting whole-group links when class instantiation is disabled.

  • Adds link-aware validation and error messages for parse and instantiate links.
  • Resolves absent required init_args targets against the selected subclass specification.
  • Removes link-controlled parameters from parsed values and configured defaults.
  • Adds documentation, changelog entries, and regression coverage for the corrected link behavior.
  • The new instantiation validation currently discards normalized values, leaving a type-contract gap.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
    S[Instantiate source objects] --> C{Compute function?}
    C -->|Yes| V[Compute linked value]
    C -->|No| V
    V --> A[adapt_typehints validates and may normalize]
    A --> D[Normalized return value discarded]
    D --> T[Original value assigned to target]
    T --> I[Instantiate target object]
Loading

Reviews (1) · Last reviewed commit: "Validate types of values set by links an..."

Comment thread jsonargparse/_link_arguments.py Outdated
@mauvilsa
mauvilsa merged commit 9273463 into main Sep 18, 2026
30 of 31 checks passed
@mauvilsa
mauvilsa deleted the link-target-type-validation branch September 18, 2026 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant