Skip to content

Commit 5a73fe2

Browse files
authored
Merge pull request #1617 from codeflash-ai/comparator-uniontype
fix: add types.UnionType support to comparator
2 parents bc0f9d5 + 0021895 commit 5a73fe2

2 files changed

Lines changed: 45 additions & 22 deletions

File tree

codeflash/verification/comparator.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,28 +145,27 @@ def comparator(orig: Any, new: Any, superset_obj: bool = False) -> bool:
145145
return _normalize_temp_path(orig) == _normalize_temp_path(new)
146146
return False
147147

148-
if isinstance(
149-
orig,
150-
(
151-
int,
152-
bool,
153-
complex,
154-
type(None),
155-
type(Ellipsis),
156-
decimal.Decimal,
157-
set,
158-
bytes,
159-
bytearray,
160-
memoryview,
161-
frozenset,
162-
enum.Enum,
163-
type,
164-
range,
165-
slice,
166-
OrderedDict,
167-
types.GenericAlias,
168-
),
169-
):
148+
_equality_types = (
149+
int,
150+
bool,
151+
complex,
152+
type(None),
153+
type(Ellipsis),
154+
decimal.Decimal,
155+
set,
156+
bytes,
157+
bytearray,
158+
memoryview,
159+
frozenset,
160+
enum.Enum,
161+
type,
162+
range,
163+
slice,
164+
OrderedDict,
165+
types.GenericAlias,
166+
*((_union_type,) if (_union_type := getattr(types, "UnionType", None)) else ()),
167+
)
168+
if isinstance(orig, _equality_types):
170169
return orig == new
171170
if isinstance(orig, float):
172171
if math.isnan(orig) and math.isnan(new):

tests/test_comparator.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5216,3 +5216,27 @@ def test_python_tempfile_pattern_regex(self):
52165216
assert PYTHON_TEMPFILE_PATTERN.search("/tmp/tmp123456/")
52175217
assert not PYTHON_TEMPFILE_PATTERN.search("/tmp/mydir/file.txt")
52185218
assert not PYTHON_TEMPFILE_PATTERN.search("/home/tmp123/file.txt")
5219+
5220+
5221+
@pytest.mark.skipif(sys.version_info < (3, 10), reason="types.UnionType requires Python 3.10+")
5222+
class TestUnionType:
5223+
def test_union_type_equal(self):
5224+
assert comparator(int | str, int | str)
5225+
5226+
def test_union_type_not_equal(self):
5227+
assert not comparator(int | str, int | float)
5228+
5229+
def test_union_type_order_independent(self):
5230+
assert comparator(int | str, str | int)
5231+
5232+
def test_union_type_multiple_args(self):
5233+
assert comparator(int | str | float, int | str | float)
5234+
5235+
def test_union_type_in_list(self):
5236+
assert comparator([int | str, 1], [int | str, 1])
5237+
5238+
def test_union_type_in_dict(self):
5239+
assert comparator({"key": int | str}, {"key": int | str})
5240+
5241+
def test_union_type_vs_none(self):
5242+
assert not comparator(int | str, None)

0 commit comments

Comments
 (0)