-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
Improve Exception Handling #2266
Comments
@awaelchli so what do you propose to have more exact Exceptions like |
Yes exactly, the specific exception we try to catch. try:
x = myobject.attribute
except Exception
x = 0 can be replaced with |
Another code smell we frequently see (in many places in the code): try:
from some_package import some_module
except ImportError:
SOMEPACKAGE_AVAILABLE = False
else:
SOMEPACKAGE_AVAILABLE = True Better: catch Even better (and shorter): import importlib
SOMEPACKAGE_AVAILABLE = importlib.util.find_spec("some_package") is not None |
About the import error, I remember that thare is something twisted about python version and avalanche exceptions, I guess that py3.6 doesn't have the one suggest (need to check) Also is there a simple way how to import jist a function from a package that you are not sure if it is installed? |
I think the simplest is this: if importlib.util.find_spec("some_package") is not None:
from some_package import my_function because when we import the function in Python it always has to import the whole package anyway. |
This issue has been automatically marked as stale because it hasn't had any recent activity. This issue will be closed in 7 days if no further activity occurs. Thank you for your contributions, Pytorch Lightning Team! |
🚀 Code Quality Improvement
I came across this a few times already:
It is the worst possible way to handle Exceptions. It is better to catch the specific exception or at least log a message.
Alternatives
None. Sooner or later someone has to deal with this anyway :)
The text was updated successfully, but these errors were encountered: