Skip to content

Fix instance factories rejecting implementers of a returned Protocol and accepting abstract classes - #1033

Merged
mauvilsa merged 1 commit into
mainfrom
fix-factory-return-protocol
Nov 3, 2026
Merged

mauvilsa merged 1 commit into
mainfrom
fix-factory-return-protocol

Conversation

@mauvilsa

@mauvilsa mauvilsa commented Nov 3, 2026

Copy link
Copy Markdown
Owner

What does this PR do?

Fixes instance factories whose return type is a Protocol, e.g. Callable[[list[float]], SomeProtocol] or a callable protocol returning one. Unless the protocol was runtime_checkable, these failed for every class and function, even ones inheriting from the protocol or annotated to return it, because the return type was checked with issubclass, which raises an error for other protocols. They now use the same signature-based check as plain Protocol type hints, including for subscripted generic protocols, where Proto[int] and Proto[str] accept different implementations. This check also applies when resolving defaults in signatures, so lambda p: Impl(p, lr=0.5), and Impl(lr=0.5) for a plain Protocol, now become a class_path with init_args.

Also, Callable and callable Protocol types now reject abstract classes and protocols when parsing, with the same error as other class types. Previously, an abstract class only failed on instantiation or when calling the factory.

A class with the wrong signature is now rejected even if the protocol is runtime_checkable, which previously passed by matching method names only.

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 Nov 3, 2026
Comment thread jsonargparse_tests/test_typehints.py Dismissed
Comment thread jsonargparse_tests/test_typehints.py Dismissed
Comment thread jsonargparse_tests/test_typehints.py Dismissed
Comment thread jsonargparse_tests/test_typehints.py Dismissed
Comment thread jsonargparse_tests/test_typehints.py Dismissed
Comment thread jsonargparse_tests/test_typehints.py Dismissed
Comment thread jsonargparse_tests/test_typehints.py Dismissed
@greptile-apps

greptile-apps Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The changes affect factory type-hint workflows across parsing and defaults.

The PR appears safe to merge, with a remaining gap in callable protocol lambda defaults.

Findings

  1. P2 Protocol lambda defaults stay unresolved ▶

Summary

Lets instance factories accept classes and functions that implement their returned Protocol.

  • Factories can now return classes and functions that match a Protocol.
  • Factory settings reject abstract classes and protocols before they are used.

Reviews (1) · Last reviewed commit: "Fix instance factories rejecting impleme..." · Reviewed by Greptile

Comment thread jsonargparse/_parameter_resolvers.py Resolved
@codecov

codecov Bot commented Nov 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3a3656c) to head (edc25e5).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #1033   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines        10081     10091   +10     
=========================================
+ Hits         10081     10091   +10     

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

@sonarqubecloud

sonarqubecloud Bot commented Nov 3, 2026

Copy link
Copy Markdown

@mauvilsa
mauvilsa merged commit 6cc6f34 into main Nov 3, 2026
36 checks passed
@mauvilsa
mauvilsa deleted the fix-factory-return-protocol branch November 3, 2026 23:41

This branch was previously deployed

1 inactive deployment
sonarcloud — edc25e5c Deployed Nov 3, 2026 by mauvilsa via sonarcloud #1029
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