You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In my understanding, the description pages for rules B904 and TRY200 say (paraphrased): "Re-raising an exception (i.e., catching an exception and then raising a different exception) without specifying a cause with the from keyword will make the raised exception not be chained, which means we won't have the full stack trace information."
This is incorrect. As per the official documentation, if an exception is re-raised without explicit chaining, it will be implicitly chained with the caught exception. Therefore, a re-raised exception without explicit chaining (which the linter rule says is bad) has similar behavior as explicit chaining (which the linter rule recommends). The main difference seems to be a slight difference in the wording of the stack trace. This behavior was not as well-documented in previous python versions, but it has been like this since at least version 3.5.9, if not older.
Side note: the fact that these two lint rules are duplicates is a known issue as per #2186
The text was updated successfully, but these errors were encountered:
Traceback (most recent call last):
File "example.py", line 2, in <module>
raise ValueError(1)
ValueError: 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "example.py", line 4, in <module>
raise ValueError(2)
ValueError: 2
Traceback (most recent call last):
File "example.py", line 19, in <module>
raise ValueError(1)
ValueError: 1
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "example.py", line 21, in <module>
raise ValueError(2) from exc
ValueError: 2
Traceback (most recent call last):
File "example.py", line 39, in <module>
raise ValueError(2) from None
ValueError: 2
I agree the rule documentation is confusing as written. However "During handling of the above exception, another exception occurred" and "The above exception was the direct cause of the following exception" signal different things to a user. Intentionally raising an exception in an except block should always use from for clarity. It'd be great to update the documentation for these rules and add some examples
In my understanding, the description pages for rules B904 and TRY200 say (paraphrased): "Re-raising an exception (i.e., catching an exception and then raising a different exception) without specifying a cause with the
from
keyword will make the raised exception not be chained, which means we won't have the full stack trace information."This is incorrect. As per the official documentation, if an exception is re-raised without explicit chaining, it will be implicitly chained with the caught exception. Therefore, a re-raised exception without explicit chaining (which the linter rule says is bad) has similar behavior as explicit chaining (which the linter rule recommends). The main difference seems to be a slight difference in the wording of the stack trace. This behavior was not as well-documented in previous python versions, but it has been like this since at least version 3.5.9, if not older.
Side note: the fact that these two lint rules are duplicates is a known issue as per #2186
The text was updated successfully, but these errors were encountered: