0

I have a function from an internal library:

def some_external_function(arg1: str, arg2: bool) -> bool:
    pass

How can I get the args that are required for this function as a type hint? I want to be able to do something like:

from external_lib import some_external_function

create_args_for_external_function_args = Type(... args of some_external_function)


def create_args_for_external_function(data: create_args_for_external_function_args):
    ...

I am trying to do the following, create a list of function arguments that I an loop through and add additional ArgumentParser arguments to my CLI with add_argument as I'm implementing an abstract tool.

Essentially this:

main_parser = argparse.ArgumentParser(
                parents=[some_base_parser]
            )

# in real code comes from a higher level
list_of_add_argument_args = [
   (("--recovery-code",), {"type": str, "help": "A recovery code to mark as used"}), 
] <<<< This list is what I need typed

for extra_arg, extra_kwargs in list_of_add_argument_args:
    main_parser.add_argument(*extra_arg, **extra_kwargs) < Unpack args into function somehow

coler-j
  • 1,791
  • 1
  • 27
  • 57
  • Does it answer your question? https://stackoverflow.com/questions/52838204/how-to-get-class-variables-and-type-hints – Idan Aug 25 '22 at 11:47
  • That returns an empty `{}` (I am trying to get args required to mimic an argparser callable `get_type_hints(_ActionsContainer.add_argument)` – coler-j Aug 25 '22 at 11:51
  • 1
    You might get better answers if you explain exactly what you're trying to do instead of guessing halfway to a solution and asking vaguely about that (this is not intended to sound too critical, but your example code doesn't make much sense and it sounds like from your comment you may need something slightly different than what you've asked for) – Anentropic Aug 25 '22 at 11:53
  • @Anentropic add specifics for clarity – coler-j Aug 25 '22 at 11:59
  • For now, I am typing it as `List[Tuple[Tuple, Dict]]` but I would prefer to be able to connect it with the expected type of `_ActionsContainer.add_argument` – coler-j Aug 25 '22 at 12:06
  • 1
    I think it's not possible, the implementation of `add_argument` is quite dynamic and just takes `*args, **kwargs`. You could make your type a little more specific `List[Tuple[Tuple[str, ...], Dict[str, Any]]]` – Anentropic Aug 25 '22 at 13:00

0 Answers0