Skip to content

Handle object as Any, and support subscripted generic protocols and TypedDicts - #958

Merged
mauvilsa merged 3 commits into
mainfrom
object-any-and-subscripted-generics
Aug 19, 2026
Merged

Handle object as Any, and support subscripted generic protocols and TypedDicts#958
mauvilsa merged 3 commits into
mainfrom
object-any-and-subscripted-generics

Conversation

@mauvilsa

Copy link
Copy Markdown
Owner

What does this PR do?

Extends the type hint support for generics and unifies the handling of object
with Any.

Added

  • Support for a subscripted generic TypedDict, e.g. SomeDict[int], and for a TypedDict that inherits from one, e.g. class SubDict(SomeDict[int]), both previously unsupported. Subscripting doesn't change which keys are accepted, only the types of the keys annotated with a TypeVar, composing the substitutions along the inheritance chain. Works for parsing, the --*.help option and shtab completions.
  • A TypeVar default, constraint or bound given as a forward reference is now resolved with the names of the module in which the TypeVar is defined. An unresolvable reference becomes an Unvalidated<...> instead of failing.
  • Keys of a TypedDict whose type can't be validated now accept any value, the same as signature parameters already did.

Fixed

  • A subscripted generic Protocol, e.g. Proto[int], never being implementable. The type arguments are now substituted into the protocol's method signatures and return types, so Proto[int] and Proto[str] accept different implementations, and a TypeVar that remains matches any type, as static type checkers do. This also applies to instance factory protocols, i.e. the return type of a subscripted protocol's __call__.
  • Internal RuntimeError when giving a value for a TypedDict key annotated with a TypeVar.
  • type[Any] rejecting every value, instead of accepting any class as bare type does.
  • The docstring of a subscripted generic class not being used, so the group description and the parameter descriptions were missing in the help.
  • Nested namespaces in init_args, e.g. a subclass spec kept as is for an Any typed parameter, being expanded into keyword arguments on instantiate.

Changed

  • object as a type is now handled exactly like Any, since in standard typing every value is an instance of it. This means it no longer only accepts the import path of a class, it accepts any value, it participates in the validate_subclass_spec_in_any and instantiate_subclass_spec_in_any settings, it is sorted last in unions together with Any and Unvalidated<...>, and dumping it warns about values that lose their type.

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 bug Something isn't working enhancement New feature or request labels Aug 19, 2026
Comment thread jsonargparse_tests/test_typehints.py Dismissed
@mauvilsa
mauvilsa deployed to sonarcloud August 19, 2026 06:24 — with GitHub Actions Active
@codecov

codecov Bot commented Aug 19, 2026

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #958   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           27        27           
  Lines         8600      8671   +71     
=========================================
+ Hits          8600      8671   +71     

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

@sonarqubecloud

sonarqubecloud Bot commented Aug 19, 2026

Copy link
Copy Markdown

@mauvilsa
mauvilsa merged commit 9423976 into main Aug 19, 2026
32 checks passed
@mauvilsa
mauvilsa deleted the object-any-and-subscripted-generics branch August 19, 2026 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants