-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[Lang] Support formatted string with args in assert #1806
Conversation
# taichi_lang_core.Expr (defined in C++) | ||
import taichi as ti | ||
taichi_lang_core.create_assert_stmt( | ||
ti.Expr(cond).ptr, msg, [ti.Expr(x).ptr for x in extra_args]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if extra_args
is Vector
? Let's make use of Vector.__ti_repr__
like currently ti_print
does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Unfortunately, assert format specifier doesn't support such a fancy data structure yet, see
taichi/taichi/program/program.cpp
Lines 445 to 454 in c3f1a5d
if (dtype == 'd') { | |
error_message_formatted += fmt::format( | |
"{}", taichi_union_cast_with_different_sizes<int32>(argument)); | |
} else if (dtype == 'f') { | |
error_message_formatted += fmt::format( | |
"{}", | |
taichi_union_cast_with_different_sizes<float32>(argument)); | |
} else { | |
TI_ERROR("Data type identifier %{} is not supported", dtype); | |
} |
It only has %d
or %f
..
I think this comes from the fact that, right now print
and assert
handle args differently. assert
only handles string with format specifiers, which limits what kind of runtime data we can put into the message.
I'd suggest us to translate: assert cond, "foo=%f" % foo into: if cond:
pass
else:
print("foo=%f" % foo)
ti.core.insert_assert_failure_stmt() for API consistence and maintainability. So that we could deprecate |
Codecov Report
@@ Coverage Diff @@
## master #1806 +/- ##
==========================================
+ Coverage 43.51% 43.67% +0.15%
==========================================
Files 44 44
Lines 6023 6045 +22
Branches 1078 1082 +4
==========================================
+ Hits 2621 2640 +19
- Misses 3248 3251 +3
Partials 154 154
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this is a very useful feature. But as yuanming always reminds me, we should always discuss about the design, reach an agreement, before start to writing code. My opinion is to translate assert
into if
+ print
+ ti.assert_failure()
. What do you think?
Thanks for proposing. However, we actually want |
Yeah, this seems cleaner. However, note that this is likely a larger change than expected:
Forget what i said.... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much! LGTM. This can be merged after committing the suggestion in test_assert_minimal
.
Maybe we should also consider supporting f-strings in the future :-) |
Ah yes, that sounds like a much bigger feature, though.. |
This allows us to write
I think we can apply the same strategy to
print
, but let me focus onassert
in this PR.Related issue = #1434
[Click here for the format server]