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
Python 3.11's new except* syntax has an odd quirk: it throws an exception (from within Python itself) if you use it on a try block that throws an unhashable exception. Normal try ... except blocks don't do this. For example, this works fine:
Produces the following unhandled exception traceback:
+ Exception Group Traceback (most recent call last):
| File "C:\Users\Josep\AppData\Roaming\JetBrains\PyCharmCE2022.2\scratches\scratch_61.py", line 6, in <module>
| raise ExceptionGroup("Foo", [
| ExceptionGroup: Foo (1 sub-exception)
+-+---------------- 1 ----------------
| MyException: Bar
+------------------------------------
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Josep\AppData\Roaming\JetBrains\PyCharmCE2022.2\scratches\scratch_61.py", line -1, in <module>
TypeError: unhashable type: 'MyException'
I'm fairly sure this qualifies as an actual bug in the Python implementation I've got, as I couldn't find this behavior documented anywhere, but that's a ticket for a different repo.
Back to pytest. At the top of this bug report is a block of code reproducing this exception inside a test. Obviously the correct behavior would be for pytest to catch the TypeError, mark the test as failing due to an uncaught exception, and display it with full traceback - or, if that was completely impossible due to the "line -1" thing, then without one.
The most important thing is that the test should fail due to an uncaught exception, rather than pytest itself throwing an internal error and then marking the test as "not run".
The text was updated successfully, but these errors were encountered:
This looks like a straightforward bug in our get_source() function, which should not (anymore?) assume that line_index is in source.lines; we might need some fallback logic for the can't-find-source case. Thanks very much for the report!
Tl;dr: A strange exception thrown by
except*
from "line -1" causes PyTest to crash when trying to retrieve the line it came from. Code to reproduce:And then run that test function (
test_broken
) with pytest.Expected result: test succeeds.
Expected result once you know about the weird behavior of
expect*
(see below): test fails with aTypeError
.Actual result: pytest fails with the following traceback; test is marked as "not run".
I'm running CPython 3.11.0 for Windows (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)], and pytest 7.2.0. My complete
pip list
:I think the cause of the bug is as follows.
Python 3.11's new
except*
syntax has an odd quirk: it throws an exception (from within Python itself) if you use it on atry
block that throws an unhashable exception. Normaltry ... except
blocks don't do this. For example, this works fine:But this:
Produces the following unhandled exception traceback:
I'm fairly sure this qualifies as an actual bug in the Python implementation I've got, as I couldn't find this behavior documented anywhere, but that's a ticket for a different repo.
Back to pytest. At the top of this bug report is a block of code reproducing this exception inside a test. Obviously the correct behavior would be for pytest to catch the
TypeError
, mark the test as failing due to an uncaught exception, and display it with full traceback - or, if that was completely impossible due to the "line -1" thing, then without one.The most important thing is that the test should fail due to an uncaught exception, rather than pytest itself throwing an internal error and then marking the test as "not run".
The text was updated successfully, but these errors were encountered: