Skip to content

Commit

Permalink
fix: Ensure ints can be treated as booleans (#709)
Browse files Browse the repository at this point in the history
Closes #681
  • Loading branch information
tatiana-s authored Dec 11, 2024
1 parent 2b23f9a commit 6ef6d60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
4 changes: 1 addition & 3 deletions guppylang/checker/expr_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
24 changes: 24 additions & 0 deletions tests/integration/test_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 6ef6d60

Please sign in to comment.