-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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 the error message for assert
and AssertionError
by adding location information
#105724
Comments
You need to modify |
I don't think that is necessary. Changing the location of the |
Can I work on this issue ? |
@heysujal sorry, I am already working on it :( |
@markshannon @Fidget-Spinner I might be missing something, but looks like
Examples, file called def some():
b = None
assert b
some() Result: » ./python.exe ex.py
Traceback (most recent call last):
File "/Users/sobolev/Desktop/cpython/ex.py", line 8, in <module>
some()
File "/Users/sobolev/Desktop/cpython/ex.py", line 5, in some
assert b
^
AssertionError The same happens for CPython tests, if I add some failing assert to
But, REPL does not work for obvious reason: it cannot allow to read source lines. Example (the same code as in the first example): >>> def some():
... b = None
... assert b
...
>>> some()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in some
AssertionError So, I am not sure what the task is. |
What version of Python are you testing against? |
I am using the |
@markshannon never mind, I was running my own fork with the changes I made 🤦 |
Good to know that your changes work 🙂 |
We've all done it. I hate to think how much time I've wasted debugging the wrong branch in the past. |
assert
and AssertionError
assert
and AssertionError
by adding location information
We don't want to increase the cost of execution if the assertion passes, which limits what transformations we can apply. Boolean operation.
if not cond1 or not cond2:
raise AssertionError # location `cond1 and cond2` We could compile it as: if not cond1:
raise AssertionError # location `cond1`
if not cond2:
raise AssertionError # location `cond2` 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
if not var1 == var2:
_msg = f"{var1} != {var2}"
raise AssertionError(_msg) # location `var1 == var2` This is complicated somewhat if if not var1 == var2:
instrinsic_assert_eq(var1, var2) |
I can surely work on this after the initial PR is merged 👍 |
@sobolevn Still stuff to do here, or can we close this issue? |
There are still tasks to complete in this issue. |
Feature or enhancement
Right now
assert
generate errors that are hard to read, mostly because they lack context.Example:
Pitch
Let's make them better:
This is a good starting point.
Later this can be enhanced to use more context.
Linked PRs
assert
errors #105935The text was updated successfully, but these errors were encountered: