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

fix: Ensure ints can be treated as booleans #709

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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)
Loading