facebookresearch / hydra

Hydra is a framework for elegantly configuring complex applications
https://hydra.cc
MIT License
8.66k stars 623 forks source link

PrettyErrors doesn't work with Hydra #1431

Open ashleve opened 3 years ago

ashleve commented 3 years ago

🐛 Bug

Description

PrettyErrors is a package by @onelivesleft, which makes stack trace more readable in terminal. Unfortunetely it seems it simply doesn't do anything when Hydra is used.

Is there any way to make Hydra and PrettyErrors compatible?

Checklist

To reproduce

Isntall PrettyErrors with: python -m pip install pretty_errors python -m pretty_errors

Run any file with Hydra:

import hydra

@hydra.main(config_path="", config_name="config.yaml")
def main(config):
    raise Exception("Some exception")

if __name__ == "__main__":
    main()

(if we just comment out the hydra.main decorator then stack trace is printed correctly)

Additional context

Checked both on ubuntu bash and windows powershell.

omry commented 3 years ago

Hydra is changing the working directory by default, maybe it's confusing pretty_errors? It seems like this bug should be reported against pretty_errors.

omry commented 3 years ago
import hydra
import os

@hydra.main()
def main(_):
    print("cwd", os.getcwd())
    raise Exception("Some exception")

if __name__ == "__main__":
    main()

Does not seem related to the working directory. Running in . still have the issue:

$ python test.py hydra.run.dir=.
cwd /home/omry/dev/hydra/temp/repro/1431
Traceback (most recent call last):
  File "test.py", line 7, in main
    raise Exception("Some exception")
Exception: Some exception

Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.

Hydra is modifying the exception by default to hide some uninteresting stack frames. This could be incompatible with PrettyErrors. You can enable the full stack trace with HYDRA_FULL_ERROR and it seems that PrettyError likes it better:

$ HYDRA_FULL_ERROR=1 python test.py 

image

ashleve commented 3 years ago

@omry actually I just tried to use Rich exception handling (https://rich.readthedocs.io/en/latest/logging.html) along with Hydra and it's the same as with PrettyErrors. Same happened with python-coloredlogs.

omry commented 3 years ago

By default, Hydra is printing the exception directly (after tweaking the backtrace) instead of raising it. This seems incompatible with the concept of having a generic exception handler like PrettyErrors or the others.

You can see the exception handling logic here.

For now, the solution is to use HYDRA_FULL_ERROR to prevent Hydra from intercepting the exception. I am open to pull requests to improve this area, but everything is behaving as designed right now.

Closing as this is not an actual bug. But feel free to continue the discussion here.

Thanks.

omry commented 3 years ago

I am going to try to install an exception hook as a way to be compatible with other exception hooks.

lminer commented 1 year ago

Just wanted to add that this would be a nice feature.