[Python] Typehints for dicts in Python including the default values [duplicate]

Discussão em 'Python' iniciado por Stack, Abril 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have a function that calls a bunch of other functions like this:

    import inspect
    def get_default_args(func):
    # obtained from: https://stackoverflow.com/questions/12627118/get-a-function-arguments-default-value
    signature = inspect.signature(func)
    return {
    k: v.default
    for k, v in signature.parameters.items()
    if v.default is not inspect.Parameter.empty
    }

    def func_a(self, var_a:int=10, var_b:str = 'a') -> float:
    pass

    def func_b(self, var_c:int=10, var_d:str = 'b') -> float:
    pass

    def main_func(args_a, args_b):
    value_a = func_a(**args_a)
    value_b = func_b(**args_b)

    # Get the options and default values to call the functions
    args_a = get_default_args(func_a)
    args_b = get_default_args(func_a)

    # Edit the default values
    args_a = {"var_b":'d'}
    args_b = {"var_c":5}

    # Make the call to the main function
    main_func(args_a, args_b)


    This works, however, when I edit what is the input to func_a I would like to get all options that this functions have in the IDE similar to when I call the function func_a:[​IMG]

    Since get_default_args is executed at runtime the IDE (like VSCode) does not recognize what options and what default values there is in args_a. For examle:

    [​IMG]

    I've tried with dataclasses and TypeDicts but I have not managed to get the behavior that I'm looking for.

    Continue reading...

Compartilhe esta Página