Skip to content
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

Add an "assert" function #252

Open
carlosgalvezp opened this issue May 16, 2023 · 3 comments
Open

Add an "assert" function #252

carlosgalvezp opened this issue May 16, 2023 · 3 comments

Comments

@carlosgalvezp
Copy link

Hi,

There is already a fail function, but it's not ergonomic to use, because it takes 2 lines and 1 added indentation level. It would be good if we could have a assert function. Consider:

if (not condition):
   fail("Error message")

vs

assert(condition, "Error message")

The same code with assert is much cleaner. It's also consistent with most programming languages.

@laurentlb
Copy link
Contributor

In Python 3, this line is a no op:

>>> assert(False, "abc")
<stdin>:1: SyntaxWarning: assertion is always true, perhaps remove parentheses?

The syntax is actually without parentheses:

assert someExpression
assert someExpression, "error message"

So the request would require a change in the syntax (and all Starlark-aware tooling).

We would also need a way to prevent users from writing the no-op line assert(False, "abc"), as it's very error-prone.

@fenollp
Copy link

fenollp commented May 16, 2023

As someone that ported pytruth to Starlark (see google/starlark-go#361) I'd like the assert statement to be correctly parsed so that I'd be able to bind its behavior in this way:

assert that("this").is_not_equal_to("that")

Today I'm able to declare assert as a module and that as its only method. I very awkwardly s/assert that/assert.that/g for the syntax support... so I support its addition to the spec!

@carlosgalvezp
Copy link
Author

So the request would require a change in the syntax (and all Starlark-aware tooling).

I don't quite know the requirements of Starlark - is it a requirement that it must be Python3 - compatible? Creating an assert() function would be trivial if we didn't need to support the same Python3 syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants