First check
Description
Hi, I would like to give a try to Typer but I have an issue with my timing callback.
How can I pass the context to a result_callback ?
Additional context
example code :
import time
import typer
app = typer.Typer()
def tear_down(result, ctx: typer.Context):
print(f"Done in {time.time() - ctx.obj['start_time']:.3f}s.")
@app.callback(result_callback=tear_down)
def setup(ctx: typer.Context):
print("before")
ctx.ensure_object(dict)
ctx.obj["start_time"] = time.time()
@app.command()
def my_command():
print("command call")
if __name__ == "__main__":
app()
tb:
Traceback (most recent call last):
File ".\main.py", line 25, in <module>
app()
File "C:\Users\DEVEAUX\Desktop\prog\typer test\venv\lib\site-packages\typer\main.py", line 214, in __call__
return get_command(self)(*args, **kwargs)
File "C:\Users\DEVEAUX\Desktop\prog\typer test\venv\lib\site-packages\click\core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "C:\Users\DEVEAUX\Desktop\prog\typer test\venv\lib\site-packages\click\core.py", line 782, in main
rv = self.invoke(ctx)
File "C:\Users\DEVEAUX\Desktop\prog\typer test\venv\lib\site-packages\click\core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\DEVEAUX\Desktop\prog\typer test\venv\lib\site-packages\click\core.py", line 1224, in _process_result
value = ctx.invoke(self.result_callback, value, **ctx.params)
File "C:\Users\DEVEAUX\Desktop\prog\typer test\venv\lib\site-packages\click\core.py", line 610, in invoke
return callback(*args, **kwargs)
TypeError: tear_down() missing 1 required positional argument: 'ctx'
what actually work with click: https://github.com/Modular-Lab/Modular-API/blob/3665fd8108255ddb67337815fdaf8c9d9c7800ae/modularapi/cli.py#L50
what actually work with Typer:
import time
import typing
import typer
import click
app = typer.Typer()
@click.pass_context
def tear_down(ctx: typer.Context, result: typing.Any):
print(f"Done in {time.time() - ctx.obj['start_time']:.3f}s.")
@app.callback(result_callback=tear_down)
def setup(ctx: typer.Context):
ctx.ensure_object(dict)
ctx.obj["start_time"] = time.time()
@app.command()
def my_command():
time.sleep(5)
if __name__ == "__main__":
app()
First check
and didn't find any information.Description
Hi, I would like to give a try to Typer but I have an issue with my timing callback.
How can I pass the context to a result_callback ?
Additional context
example code :
tb:
what actually work with click: https://github.com/Modular-Lab/Modular-API/blob/3665fd8108255ddb67337815fdaf8c9d9c7800ae/modularapi/cli.py#L50
what actually work with Typer: