Skip to content

Fix union of class type and instance factory, and bash completion without tput - #968

Merged
mauvilsa merged 3 commits into
mainfrom
union-class-and-factory
Sep 1, 2026
Merged

Fix union of class type and instance factory, and bash completion without tput#968
mauvilsa merged 3 commits into
mainfrom
union-class-and-factory

Conversation

@mauvilsa

@mauvilsa mauvilsa commented Sep 1, 2026

Copy link
Copy Markdown
Owner

What does this PR do?

Two independent fixes, both with tests and changelog entries.

Union of a class type and an instance factory for it

A Union of a class and a factory that returns it, e.g. Union[Optimizer, Callable[[Iterable], Optimizer]], accepted no value at all. When the value was adapted against the class subtype, sub-defaults added a placeholder for the parameter that the factory receives when called (e.g. params). That placeholder made the value invalid for the class subtype itself, and since adapting modifies the value in place, it also invalidated the subtypes that would have accepted the original value.

Changes in _typehints.py:

  • Each union subtype is now adapted against a pristine copy of the value, so a failed or lenient adaptation can no longer leak into the remaining subtypes.
  • New sub_defaults_invalidate_value check: when a subclass spec value comes back from a subtype but only validates because of leniently added sub-defaults, the union keeps trying the other subtypes instead of stopping at it.

Both directions now work: --optimizer=Adam --optimizer.lr=0.01 resolves to the factory subtype and is callable with the runtime argument, while a value that provides params explicitly resolves to the class subtype and instantiates.

Bash completion with tput unavailable

The shtab-bash completion script ran tput inside the printf format string, so every completion printed tput: command not found in environments without tput (some containers) or with TERM unset.

The colors are now resolved once when the script is sourced, into _jsonargparse_{prog}_color_message / _jsonargparse_{prog}_color_reset, with tput errors ignored. Completion messages are colored when tput is available and plain, without any error output, when it is not.

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 Sep 1, 2026
@codecov

codecov Bot commented Sep 1, 2026

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #968   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           28        28           
  Lines         9374      9388   +14     
=========================================
+ Hits          9374      9388   +14     

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

@sonarqubecloud

sonarqubecloud Bot commented Sep 1, 2026

Copy link
Copy Markdown

@mauvilsa
mauvilsa merged commit eacf963 into main Sep 1, 2026
32 checks passed
@mauvilsa
mauvilsa deleted the union-class-and-factory branch September 1, 2026 05:39
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