-
-
Notifications
You must be signed in to change notification settings - Fork 672
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[QUESTION] How do I disable typer's exception handler #129
Comments
to answer my own question after some digging it looks like you can do this.... if you normally have import typer
app = typer.Typer()
if __name__ == '__main__':
app() you can instead do import typer
app = typer.Typer()
if __name__ == '__main__':
command = typer.main.get_command(app)
try:
result = command(standalone_mode=False)
except:
print(f'exception was thrown')
sys.exit(result) |
I dug into the source code and found that app = typer.Typer()
app.command()(main)
app()` # <-- this app takes the standalone_mode argurment |
I saw this question when I was looking at disabling the typer's exception message in case of issue, as it was too verbose.
or to remove the typer exception completely:
|
Typer runs Click commands in "standalone mode": > Click will then handle exceptions and convert them into error messages > and the function will never return but shut down the interpreter. so any code after the call to Typer will never actually be executed. The `assert False` is required to convince mypy that we know Typer will never return. Related: fastapi/typer#129
A disadvantage of setting |
I actually preferd to add a handler for the throws, then I still benefit from more of try:
app()
except SystemExit as ex:
if ex.args[0] != 0:
raise
# else continue, all is well |
[Maintenance note]: moving this to the discussion forum - we can continue the discussion there! 🙏 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
I want to be able to disable typer/Click's exception handler, and handle the exceptions myself.
I see that in click you can do this by setting standalone_mode = False in the command.main() function.
The text was updated successfully, but these errors were encountered: