From 6ef6d608dc8c92c7045575fe579637ca482f3516 Mon Sep 17 00:00:00 2001 From: tatiana-s Date: Wed, 11 Dec 2024 17:41:23 +0000 Subject: [PATCH] fix: Ensure `int`s can be treated as booleans (#709) Closes #681 --- guppylang/checker/expr_checker.py | 4 +--- tests/integration/test_if.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/guppylang/checker/expr_checker.py b/guppylang/checker/expr_checker.py index 61e24674..be866ffa 100644 --- a/guppylang/checker/expr_checker.py +++ b/guppylang/checker/expr_checker.py @@ -1069,9 +1069,7 @@ def to_bool(node: ast.expr, node_ty: Type, ctx: Context) -> tuple[ast.expr, Type return node, node_ty synth = ExprSynthesizer(ctx) exp_sig = FunctionType([FuncInput(node_ty, InputFlags.Inout)], bool_type()) - return synth.synthesize_instance_func( - node, [node], "__bool__", "truthy", exp_sig, True - ) + return synth.synthesize_instance_func(node, [], "__bool__", "truthy", exp_sig, True) def synthesize_comprehension( diff --git a/tests/integration/test_if.py b/tests/integration/test_if.py index 23067e5a..a497ccbf 100644 --- a/tests/integration/test_if.py +++ b/tests/integration/test_if.py @@ -274,3 +274,27 @@ def foo() -> bool: return x == y validate(foo) + + +def test_zero_as_bool(validate): + + @compile_guppy + def foo() -> bool: + if 0: + return False + else: + return True + + validate(foo) + + +def test_one_as_bool(validate): + + @compile_guppy + def foo() -> bool: + if 1: + return True + else: + return False + + validate(foo)