Skip to content

Fix lambda defaults for callable Protocol types not resolving to class_path and init_args - #1034

Merged
mauvilsa merged 2 commits into
mainfrom
protocol-lambda-default
Nov 6, 2026
Merged

mauvilsa merged 2 commits into
mainfrom
protocol-lambda-default

Conversation

@mauvilsa

@mauvilsa mauvilsa commented Nov 6, 2026 •

Copy link
Copy Markdown
Owner

What does this PR do?

Follow-up to #1033. A default like lambda p: Impl(p, lr=0.5) was resolved to a class_path with init_args only when the parameter's type was Callable[...]. With the equivalent callable Protocol (also Optional[...]), the lambda was kept as is, so dumping the config showed Unable to serialize instance and overriding e.g. --step.init_args.lr failed.

Changes:

  • Lambda defaults are now resolved for any factory type, Callable or callable Protocol, that returns a class.
  • The number of arguments the factory passes positionally is taken from the protocol's __call__ signature, and Optional is stripped first. Before, an Optional[Callable] with more than one parameter was miscounted, so a default like lambda p, lr: Impl(p, lr) was not resolved.
  • Tests for the lambda default now cover Callable, callable Protocol and Optional callable Protocol, plus an Optional[Callable] with two positional arguments.
  • A lambda default is only resolved when its call forwards the lambda's arguments unchanged and in order. Otherwise the lambda is kept, e.g. lambda p: Impl(p[::-1], lr=0.5), which before was resolved and silently lost the [::-1].

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?
  • [n/a] 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 Nov 6, 2026
Comment thread jsonargparse_tests/test_typehints.py Dismissed
@greptile-apps

greptile-apps Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The changes affect the shared path that resolves annotated factory defaults.

Guard callable protocol lambda conversion before merging so it cannot silently change factory results.

Findings

  1. P1 Lambda input changes disappear ▶

Summary

The PR resolves lambda defaults for callable protocols and fixes argument counting for optional callables.

  • Lambda defaults for class-returning factories become editable class settings.

Reviews (1) · Last reviewed commit: "Fix lambda defaults for callable Protoco..." · Reviewed by Greptile

Comment thread jsonargparse/_parameter_resolvers.py Resolved
@codecov

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (6cc6f34) to head (4a25aea).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1034   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines        10091     10095    +4     
=========================================
+ Hits         10091     10095    +4     

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

@mauvilsa
mauvilsa deployed to sonarcloud November 6, 2026 13:19 — with GitHub Actions Active
@sonarqubecloud

sonarqubecloud Bot commented Nov 6, 2026

Copy link
Copy Markdown

@mauvilsa
mauvilsa merged commit 14d6054 into main Nov 6, 2026
35 checks passed
@mauvilsa
mauvilsa deleted the protocol-lambda-default branch November 6, 2026 13:22

This branch was successfully deployed

1 active deployment
sonarcloud — 4a25aead Deployed Nov 6, 2026 by mauvilsa via sonarcloud #1032
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.

2 participants