Skip to content

Commit 69947be

Browse files
committed
Fix bug and add tests
1 parent 7bd9cd8 commit 69947be

File tree

5 files changed

+91
-33
lines changed

5 files changed

+91
-33
lines changed

mypyc/irbuild/ll_builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2616,7 +2616,7 @@ def _translate_fast_optional_eq_cmp(
26162616
self.activate_block(r_none)
26172617
# None vs not-None
26182618
val = self.false() if expr_op == "==" else self.true()
2619-
self.add(Assign(res, self.false()))
2619+
self.add(Assign(res, val))
26202620
self.goto(out)
26212621
self.activate_block(r_not_none)
26222622
# Both operands are known to be not None, perform specialized comparison

mypyc/test-data/irbuild-bytes.test

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,35 @@ L0:
185185
r10 = CPyBytes_Build(2, var, r9)
186186
b4 = r10
187187
return 1
188+
189+
[case testOptionalBytesEquality]
190+
from typing import Optional
191+
192+
def non_opt_opt(x: bytes, y: Optional[bytes]) -> bool:
193+
return x != y
194+
[out]
195+
def non_opt_opt(x, y):
196+
x :: bytes
197+
y :: union[bytes, None]
198+
r0 :: object
199+
r1 :: bit
200+
r2 :: bool
201+
r3 :: bytes
202+
r4 :: i32
203+
r5, r6 :: bit
204+
L0:
205+
r0 = load_address _Py_NoneStruct
206+
r1 = y == r0
207+
if r1 goto L1 else goto L2 :: bool
208+
L1:
209+
r2 = 1
210+
goto L3
211+
L2:
212+
r3 = unchecked borrow cast(bytes, y)
213+
r4 = CPyBytes_Compare(r3, x)
214+
r5 = r4 >= 0 :: signed
215+
r6 = r4 != 1
216+
r2 = r6
217+
L3:
218+
keep_alive y
219+
return r2

mypyc/test-data/irbuild-str.test

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -740,35 +740,3 @@ L2:
740740
L3:
741741
keep_alive x
742742
return r2
743-
744-
[case testOptionalBytesEquality]
745-
from typing import Optional
746-
747-
def non_opt_opt(x: bytes, y: Optional[bytes]) -> bool:
748-
return x != y
749-
[out]
750-
def non_opt_opt(x, y):
751-
x :: bytes
752-
y :: union[bytes, None]
753-
r0 :: object
754-
r1 :: bit
755-
r2 :: bool
756-
r3 :: bytes
757-
r4 :: i32
758-
r5, r6 :: bit
759-
L0:
760-
r0 = load_address _Py_NoneStruct
761-
r1 = y == r0
762-
if r1 goto L1 else goto L2 :: bool
763-
L1:
764-
r2 = 1
765-
goto L3
766-
L2:
767-
r3 = unchecked borrow cast(bytes, y)
768-
r4 = CPyBytes_Compare(r3, x)
769-
r5 = r4 >= 0 :: signed
770-
r6 = r4 != 1
771-
r2 = r6
772-
L3:
773-
keep_alive y
774-
return r2

mypyc/test-data/run-bytes.test

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,30 @@ class subbytearray(bytearray):
374374
[file userdefinedbytes.py]
375375
class bytes:
376376
pass
377+
378+
[case testBytesOptionalEquality]
379+
from __future__ import annotations
380+
381+
def eq_b_opt_b(x: bytes | None, y: bytes) -> bool:
382+
return x == y
383+
384+
def ne_b_b_opt(x: bytes, y: bytes | None) -> bool:
385+
return x != y
386+
387+
def test_optional_eq() -> None:
388+
b = b'x'
389+
assert eq_b_opt_b(b, b)
390+
assert eq_b_opt_b(b + bytes([int()]), b + bytes([int()]))
391+
392+
assert not eq_b_opt_b(b'x', b'y')
393+
assert not eq_b_opt_b(b'y', b'x')
394+
assert not eq_b_opt_b(None, b'x')
395+
396+
def test_optional_ne() -> None:
397+
b = b'x'
398+
assert not ne_b_b_opt(b, b)
399+
assert not ne_b_b_opt(b + b'y', b + bytes() + b'y')
400+
401+
assert ne_b_b_opt(b'x', b'y')
402+
assert ne_b_b_opt(b'y', b'x')
403+
assert ne_b_b_opt(b'x', None)

mypyc/test-data/run-strings.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,3 +1063,34 @@ class subc(str):
10631063
[file userdefinedstr.py]
10641064
class str:
10651065
pass
1066+
1067+
[case testStrOptionalEquality]
1068+
from __future__ import annotations
1069+
1070+
def eq_s_opt_s_opt(x: str | None, y: str | None) -> bool:
1071+
return x == y
1072+
1073+
def ne_s_opt_s_opt(x: str | None, y: str | None) -> bool:
1074+
return x != y
1075+
1076+
def test_optional_eq() -> None:
1077+
s = 'x'
1078+
assert eq_s_opt_s_opt(s, s)
1079+
assert eq_s_opt_s_opt(s + str(int()), s + str(int()))
1080+
assert eq_s_opt_s_opt(None, None)
1081+
1082+
assert not eq_s_opt_s_opt('x', 'y')
1083+
assert not eq_s_opt_s_opt('y', 'x')
1084+
assert not eq_s_opt_s_opt(None, 'x')
1085+
assert not eq_s_opt_s_opt('x', None)
1086+
1087+
def test_optional_ne() -> None:
1088+
s = 'x'
1089+
assert not ne_s_opt_s_opt(s, s)
1090+
assert not ne_s_opt_s_opt(s + str(int()), s+ str(int()))
1091+
assert not ne_s_opt_s_opt(None, None)
1092+
1093+
assert ne_s_opt_s_opt('x', 'y')
1094+
assert ne_s_opt_s_opt('y', 'x')
1095+
assert ne_s_opt_s_opt(None, 'x')
1096+
assert ne_s_opt_s_opt('x', None)

0 commit comments

Comments
 (0)