Replies: 1 comment
-
Having the support of typer.run(
functools.partial(simple, argument1)
) But it's not supported.. You can try the workaround with Look in the detailsimport functools
import inspect
import json
from typing import Any
import typer
from makefun import create_function # pip install makefun
def my_partial(func, *pargs, **pkwargs):
"""
Like functools.partial, but returns a real function with
substituted parameters removed from the signature.
"""
p = functools.partial(func, *pargs, **pkwargs)
sig = inspect.signature(func)
bound = sig.bind_partial(*pargs, **pkwargs)
# Keep only the parameters that were not fixed
remaining_params = [
param for name, param in sig.parameters.items()
if name not in bound.arguments
]
new_sig = "(" + ", ".join(str(param) for param in remaining_params) + ")"
def wrapper(*args, **kwargs):
return p(*args, **kwargs)
return create_function(
new_sig,
wrapper,
func_name=func.__name__,
doc=func.__doc__,
annotations=func.__annotations__,
)
def simple(
argument1: Any,
argument2: str,
):
print(argument1, argument2)
if __name__ == "__main__":
with open("file.json") as f:
argument1 = json.load(f)
typer.run(my_partial(simple, argument1)) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
I want to pass cli arguments and normal arguments to a function using typer.
Wanted Solution
A way to pass arguments from command lines and normal arguments different than cli arguments to same function.
Wanted Code
Alternatives
No response
Operating System
Linux
Operating System Details
No response
Typer Version
0.4.0
Python Version
Python 3.8.10
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions