Replies: 3 comments 1 reply
-
|
A non breaking way to implement this would be to check the return type of the callback, and only call next if it's a generator. |
Beta Was this translation helpful? Give feedback.
-
|
After some deep diving I discovered that the result_callback argument of the Typer.callback decorator is what I was looking for. Why does the annotation say I shouldn't use this? |
Beta Was this translation helpful? Give feedback.
-
|
Did you find a way to have access to the I was hacking my way around by using In my use case I make some setup of dependencies using the callback and hooking the initialized dependencies into the context, that then I need to cleanup in the result_callback. |
Beta Was this translation helpful? Give feedback.
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
It would be nice if a callback could be interpreted as either a regular function or generator.
If a generator is returned,
nextwould be called after the command has finished. This way some cleanup can be done on a global level, like clearing temporary files.This would be similar to FastAPI's lifespan feature.
Operating System
Windows
Operating System Details
No response
Typer Version
0.24.1
Python Version
3.13.12
Additional Context
In my case I would use it to do a version check in the background, because it takes a while. I would then display the result at the end after running the command.
Beta Was this translation helpful? Give feedback.
All reactions