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
{{ message }}
Repository navigation
Improve the error message for assert and AssertionError by adding location information #105724
changed the title [-]Improve the error message for `assert` and `AssertionError`[/-][+]Improve the error message for `assert` and `AssertionError` by adding location information[/+]on Jun 14, 2023
We don't want to increase the cost of execution if the assertion passes, which limits what transformations we can apply.
We can improve a few cases, though.
Boolean operation.
assert cond1 and cond2 is compiled as:
ifnotcond1ornotcond2:
raiseAssertionError# location `cond1 and cond2`
Activity
sunmy2019 commentedon Jun 13, 2023
You need to modify
Lib/traceback.pyand (maybe) make_format_syntax_errormore general.markshannon commentedon Jun 13, 2023
I don't think that is necessary. Changing the location of the
RAISE_VARARGSinstruction in the assertion should be sufficient.heysujal commentedon Jun 14, 2023
Can I work on this issue ?
sobolevn commentedon Jun 14, 2023
@heysujal sorry, I am already working on it :(
Please, feel free to take any other free issue. If you want any help, please feel free to ping me.
sobolevn commentedon Jun 14, 2023
@markshannon @Fidget-Spinner I might be missing something, but looks like
assertalready supports this. There are two possible scenarios:assertis used in a named file, then it worksassertis used in a repl, then it does not work (obviously)Examples, file called
ex.py:Result:
The same happens for CPython tests, if I add some failing assert to
test_typing:But, REPL does not work for obvious reason: it cannot allow to read source lines. Example (the same code as in the first example):
So, I am not sure what the task is.
markshannon commentedon Jun 14, 2023
What version of Python are you testing against?
I'm not seeing that behavior and the code suggests that the location is the whole line, not the expression.
https://github.com/python/cpython/blob/main/Python/compile.c#L3893
sobolevn commentedon Jun 14, 2023
I am using the
mainversion.sobolevn commentedon Jun 14, 2023
@markshannon never mind, I was running my own fork with the changes I made 🤦
markshannon commentedon Jun 14, 2023
Good to know that your changes work 🙂
markshannon commentedon Jun 14, 2023
We've all done it. I hate to think how much time I've wasted debugging the wrong branch in the past.
[-]Improve the error message for `assert` and `AssertionError`[/-][+]Improve the error message for `assert` and `AssertionError` by adding location information[/+]pythongh-105724: Add location information to `assert` errors
pythongh-105724: Add location information to `assert` errors
markshannon commentedon Jun 20, 2023
We don't want to increase the cost of execution if the assertion passes, which limits what transformations we can apply.
We can improve a few cases, though.
Boolean operation.
assert cond1 and cond2is compiled as:We could compile it as:
which is a bit bulkier, but is as fast as it executes the same VM instructions when the assert passes.
Comparison of local variable to constant or local variable
assert var1 == var2Since we can recompute
var1orvar2without side effects, we can embed the values in the error message:This is complicated somewhat if
str(var1)raises.We can add an instrinsic to handle the odd cases.
sobolevn commentedon Jun 20, 2023
I can surely work on this after the initial PR is merged 👍
gh-105724: Add location information to `assert` errors (GH-105935)
hugovk commentedon Nov 10, 2023
@sobolevn Still stuff to do here, or can we close this issue?
sobolevn commentedon Nov 10, 2023
There are still tasks to complete in this issue.
Thanks for your work on closing issues! 👍