Report failed assertion expressions - #698
filiplajszczak wants to merge 1 commit into
Conversation
Use the original assertion expression as the default message when an assertion fails. For example: ```text AssertionError: assert actual == expected ``` Preserve the source expression through redshift and embed it in compiled output, so interpreted, Doppler, and C execution report the same expression without requiring the `.spy` source file at runtime. Explicit messages such as `assert condition, "message"` remain unchanged. Add parser characterization for plain assertions and assertions with explicit messages, then extend the cross-backend assertion tests to cover a simple comparison and preservation of the original expression when redshift can transform it. This differs from CPython's plain `assert`, which raises an `AssertionError` without retaining the failed expression. It establishes source-expression reporting as a basis for pytest-like assertion diagnostics in hypothetical future `spytest`. Later changes could attach values for evaluated expressions and subexpressions while preserving CPython's evaluation semantics. Unlike pytest, SPy can retain the required information in its own compiler pipeline rather than through external AST rewriting hackery.
1a76ab7 to
ae9ea60
Compare
|
I'm not sure this is actually needed. def main(args: list[str]) -> None:
assert len(args) == 2
print(args[0], args[1])In both cases, we can see the source code which triggered the assert. I don't understand what this would buy us, especially considering that we cannot catch exceptions yet :)
I think that when it's time to write So, I'm leaning towards closing this PR, unless there are good reasons to merge which I currently don't see. |
|
Thanks, that makes sense. We may return to this when SPy can catch exceptions. Interestingly, a related approach has been discussed in CPython: python/cpython#105799 |
Use the original assertion expression as the default message when an assertion fails. For example:
Preserve the source expression through redshift and embed it in compiled output, so interpreted, Doppler, and C execution report the same expression without requiring the
.spysource file at runtime. Explicit messages such asassert condition, "message"remain unchanged.Add parser characterization for plain assertions and assertions with explicit messages, then extend the cross-backend assertion tests to cover a simple comparison and preservation of the original expression when redshift can transform it.
This differs from CPython's plain
assert, which raises anAssertionErrorwithout retaining the failed expression. It establishes source-expression reporting as a basis for pytest-like assertion diagnostics in hypothetical futurespytest. Later changes could attach values for evaluated expressions and subexpressions while preserving CPython's evaluation semantics. Unlike pytest, SPy can retain the required information in its own compiler pipeline rather than through external AST rewriting hackery.