Skip to content

[mypyc] Dict true test #10333

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

Merged
merged 5 commits into from
Apr 22, 2021
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
5 changes: 3 additions & 2 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
is_list_rprimitive, is_tuple_rprimitive, is_dict_rprimitive, is_set_rprimitive, PySetObject,
none_rprimitive, RTuple, is_bool_rprimitive, is_str_rprimitive, c_int_rprimitive,
pointer_rprimitive, PyObject, PyListObject, bit_rprimitive, is_bit_rprimitive,
object_pointer_rprimitive, c_size_t_rprimitive
object_pointer_rprimitive, c_size_t_rprimitive, dict_rprimitive
)
from mypyc.ir.func_ir import FuncDecl, FuncSignature
from mypyc.ir.class_ir import ClassIR, all_concrete_classes
Expand Down Expand Up @@ -960,7 +960,8 @@ def add_bool_branch(self, value: Value, true: BasicBlock, false: BasicBlock) ->
return
elif is_same_type(value.type, str_rprimitive):
value = self.call_c(str_check_if_true, [value], value.line)
elif is_same_type(value.type, list_rprimitive):
elif (is_same_type(value.type, list_rprimitive)
or is_same_type(value.type, dict_rprimitive)):
length = self.builtin_len(value, value.line)
zero = Integer(0)
value = self.binary_op(length, zero, '!=', value.line)
Expand Down
23 changes: 23 additions & 0 deletions mypyc/test-data/run-dicts.test
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,26 @@ def test_dict_subclass_setdefault() -> None:
assert d.setdefault('a') == 1
assert d.setdefault('e') == None
assert d.setdefault('e', 100) == 110

[case testDictToBool]
from typing import Dict, List

def is_true(x: dict) -> bool:
if x:
return True
else:
return False

def is_false(x: dict) -> bool:
if not x:
return True
else:
return False

def test_dict_to_bool() -> None:
assert is_false({})
assert not is_true({})
tmp_list: List[Dict] = [{2: bool}, {'a': 'b'}]
for x in tmp_list:
assert is_true(x)
assert not is_false(x)