Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support of TypedDicts #457

Closed
PierreGtch opened this issue Feb 22, 2024 · 1 comment · Fixed by #494
Closed

Support of TypedDicts #457

PierreGtch opened this issue Feb 22, 2024 · 1 comment · Fixed by #494
Labels
enhancement New feature or request

Comments

@PierreGtch
Copy link
Contributor

PierreGtch commented Feb 22, 2024

Hi,
Thanks for this great library! Extremely useful!!

🚀 Feature request

The idea here would be to support arguments with type typing.TypedDict.

Motivation

This feature would be great when we want to get a dictionary instead of a namespace but still want to check the keys and types of its elements.

This could also allow checking the elements in **kwargs: https://peps.python.org/pep-0692/

Pitch

For example, if a function calls another function, we could then pass arguments to the second one very easily:

# script.py
from typing import TypedDict
from jsonargparse import CLI

def aux(a: int = 1):
    print(a)

AuxKwargs = TypedDict("AuxKwargs", aux.__annotation__)

def main(aux_kwargs: AuxKwargs):
    aux(**aux_kwargs)

CLI(main)

Then we would be able to call:

python script.py --aux_kwargs.a=2

Alternatives

Currently, the only alternative for such cases is to use the dict type, which prevents from checking the key names and types individually.
In the above example, the current alternative is:

def main(aux_kwargs: dict):
    aux(**aux_kwargs)
@PierreGtch PierreGtch added the enhancement New feature or request label Feb 22, 2024
@mauvilsa
Copy link
Member

Thank you for the proposal! Indeed the support for TypedDict is missing and can be added.

This could also allow checking the elements in **kwargs: https://peps.python.org/pep-0692/

A small note about this. jsonargparse already supports **kwargs without the need of type hinting. It does this via source code introspection. See classes-methods-and-functions and ast-resolver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants