Truncating the locals in the error stack trace #923
-
First Check
Commit to Help
Example Codeimport torch
import typer
app = typer.Typer()
@app.command()
def main(size: int = typer.Option(100, help="Size of the tensor")):
"""
Create a large tensor and raise an exception to demonstrate output flooding.
"""
# Create a large tensor
large_tensor = torch.randn(size, size, size, size)
large_tensor_2 = torch.randn(size, size, size, size)
large_tensor_3 = torch.randn(size, size, size, size)
# Function that will raise an exception
def cause_exception(tensor):
print("About to raise an exception. Local variables will include the large tensor.")
raise ValueError("This exception will cause the large tensor to be printed")
# Call the function that raises an exception
cause_exception(large_tensor)
if __name__ == "__main__":
app() Descriptionyou will need to run Try running the example code above using python app.py. You will notice that the large tensors flood the terminal stack trace. Is there a way to shorten the length of these local variable values in the stack trace. I don't want to turn these off completely but these long tensors in my code make the stack trace unreadable! Alternatively, you can allow us to use custom rich Console variables with typer. I did some digging and the code below should do the trick. All we need is a way for us to use these custom Consoles with typer.
Perhaps, we can add this in the
I'd be satisfied with either of these options. The first approach (of including an option to truncate the length) seems easier to implement. Thanks a lot!! Operating SystemmacOS Operating System DetailsNo response Typer Version0.12.3 Python Version3.10.14 Additional ContextSome images of the terminal ^ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Nevermind, i found that the code i wrote using the rich library also does not truncate the pytorch variables in the right way so allowing custom consoles would not help. I'm curious if anyone else here has any solutions! |
Beta Was this translation helpful? Give feedback.
Facing similar issue. The current error output makes it difficult to navigate to the source of an error. Did you find a solution?
EDIT: Passing
pretty_exceptions_enable=False
when initializingtyper
object fixes it for me. Source