Given a string like this one:
signature = "func(data: list[str], opt = None) -> int"
How do I get the argument details (and also the function return type) ?
# name type hint default
# ---- --------- -------
# data list[str] -
# opt - None
I have tried to play with ast
:
funcdef = f"def {signature}: pass"
print(ast.dump(ast.pars(funcdef), indent=2))
but this annotation does not look like ‘list[str]’ at all:
arg='name',
annotation=Subscript(
value=Name(id='list', ctx=Load()),
slice=Name(id='str', ctx=Load()),
ctx=Load())),
I think I could use eval
to create a real func object and inspect it, but I’m afraid of possible side effects.