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

Python Bindings: fix Python bool -> Expr implicit conversion #6657

Merged
merged 5 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions python_bindings/correctness/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,13 @@ def test_scalar_funcs():

g.compile_jit()

def test_bool_conversion():
x = hl.Var('x')
f = hl.Func('f')
f[x] = x
s = bool(True)
# Verify that this doesn't fail with 'Argument passed to specialize must be of type bool'
f.compute_root().specialize(True)

if __name__ == "__main__":
test_compiletime_error()
Expand All @@ -317,3 +324,4 @@ def test_scalar_funcs():
test_basics4()
test_basics5()
test_scalar_funcs()
test_bool_conversion()
4 changes: 4 additions & 0 deletions python_bindings/src/PyExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ void define_expr(py::module &m) {
auto expr_class =
py::class_<Expr>(m, "Expr")
.def(py::init<>())
.def(py::init([](bool b) {
return Internal::make_bool(b);
}))
// PyBind11 searches in declared order,
// int should be tried before float conversion
.def(py::init<int>())
Expand Down Expand Up @@ -55,6 +58,7 @@ void define_expr(py::module &m) {

// implicitly_convertible declaration order matters,
// int should be tried before float conversion
py::implicitly_convertible<bool, Expr>();
py::implicitly_convertible<int, Expr>();
py::implicitly_convertible<float, Expr>();
py::implicitly_convertible<double, Expr>();
Expand Down