Skip to content

[mypyc] Fix unions of bools and ints #15066

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 1 commit into from
Apr 17, 2023
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
2 changes: 1 addition & 1 deletion mypyc/rt_subtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def visit_rinstance(self, left: RInstance) -> bool:
return is_subtype(left, self.right)

def visit_runion(self, left: RUnion) -> bool:
return is_subtype(left, self.right)
return not self.right.is_unboxed and is_subtype(left, self.right)

def visit_rprimitive(self, left: RPrimitive) -> bool:
if is_short_int_rprimitive(left) and is_int_rprimitive(self.right):
Expand Down
6 changes: 6 additions & 0 deletions mypyc/test-data/run-bools.test
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,9 @@ def test_mixed_comparisons_i64() -> None:
assert neq_mixed_i64(n, x) == (n != int(x))
assert lt_mixed_i64(x, n) == (int(x) < n)
assert gt_mixed_i64(n, x) == (n > int(x))

[case testBoolMixInt]
y = False
print((y or 0) and True)
[out]
0
7 changes: 7 additions & 0 deletions mypyc/test/test_typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def test_bool(self) -> None:
assert not is_runtime_subtype(bool_rprimitive, bit_rprimitive)
assert not is_runtime_subtype(bool_rprimitive, int_rprimitive)

def test_union(self) -> None:
bool_int_mix = RUnion([bool_rprimitive, int_rprimitive])
assert not is_runtime_subtype(bool_int_mix, short_int_rprimitive)
assert not is_runtime_subtype(bool_int_mix, int_rprimitive)
assert not is_runtime_subtype(short_int_rprimitive, bool_int_mix)
assert not is_runtime_subtype(int_rprimitive, bool_int_mix)


class TestUnionSimplification(unittest.TestCase):
def test_simple_type_result(self) -> None:
Expand Down