How to skip callback for specific functions using a decorator #614
Unanswered
SamEdwardes
asked this question in
Questions
Replies: 1 comment
-
For example, here is my best current effort. But it does not work because the callback gets executed before the code in the decorator does. import functools
import typer
app = typer.Typer()
STATE = {"skip_callback": False}
def skip_callback(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
print("Skipping the callback")
STATE["skip_callback"] = True
return func(*args, **kwargs)
return wrapper
@app.callback()
def callback(ctx: typer.Context):
if STATE["skip_callback"]:
return None
print("Running the callback function")
@skip_callback
@app.command()
def hello():
print("I don't want the callback to run on this command.")
if __name__ == "__main__":
app() |
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
-
First Check
Commit to Help
Example Code
Description
I am looking for an approach to run the callback only on certain functions. The provided code example is the most graceful way I have found to do this so far. But I am looking for something a bit easier to maintain.
I think the ideal solution would use a decorator that I can put on a function that will tell it to skip the callback. For example.
I have been trying to come up with a way to do this, but so far nothing has worked. Does anyone have suggestions for this pattern? Or another pattern they use to skip the callback?
Operating System
macOS
Operating System Details
No response
Typer Version
0.7.0
Python Version
3.11.3
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions