Skip to content

Support of TypedDicts #457

@PierreGtch

Description

@PierreGtch

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions