Skip to content

Commit fd38b65

Browse files
Lena Kashtelyanmeta-codesync[bot]
authored andcommitted
Debug testutils._build_comparison_str (#4724)
Summary: Pull Request resolved: #4724 Previously we could get absent (and thus confusing) output in some edge cases (unequal type in top-level objects; unequal type for two instances of Ax `Base); this covers. {F1984377139} Reviewed By: mgarrard Differential Revision: D89909334 fbshipit-source-id: c24b054f35fe0d9b649c02fc14653177146fd572
1 parent 94f78c7 commit fd38b65

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

ax/utils/common/equality.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ def object_attribute_dicts_equal(
149149
return not bool(unequal_type or unequal_value)
150150

151151

152-
# pyre-fixme[3]: Return annotation cannot contain `Any`.
153152
def object_attribute_dicts_find_unequal_fields(
154153
one_dict: dict[str, Any],
155154
other_dict: dict[str, Any],

ax/utils/common/tests/test_testutils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def test_equal(self) -> None:
4646
self.assertEqual(MyBase("red"), MyBase("panda"))
4747
except AssertionError as err:
4848
expected_suffix = (
49-
"\n\nFields with different values:\n\n1) field: red "
50-
"(type <class 'str'>) != panda (type <class 'str'>)"
49+
"Fields with different values:\n\n1) field: \nred "
50+
"(type <class 'str'>) \n!=\npanda (type <class 'str'>)"
5151
)
5252
self.assertIn(expected_suffix, str(err))
5353

ax/utils/common/testutils.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def _build_comparison_str(
174174
"""
175175

176176
def _unequal_str(first: Any, second: Any) -> str:
177-
return f"{first} (type {type(first)}) != {second} (type {type(second)})."
177+
return f"\n{first} (type {type(first)}) \n!=\n{second} (type {type(second)})."
178178

179179
if first == second:
180180
return ""
@@ -189,12 +189,20 @@ def _unequal_str(first: Any, second: Any) -> str:
189189

190190
msg = ""
191191
indent = " " * level * 4
192-
unequal_types, unequal_val = object_attribute_dicts_find_unequal_fields(
193-
one_dict=first.__dict__ if isinstance(first, Base) else first,
194-
other_dict=second.__dict__ if isinstance(second, Base) else second,
195-
fast_return=False,
196-
skip_db_id_check=skip_db_id_check,
197-
)
192+
unequal_types, unequal_val = {}, {}
193+
if type(first) is not type(second):
194+
unequal_types["__type__"] = (first, second)
195+
else:
196+
unequal_types_recursive, unequal_val_recursive = (
197+
object_attribute_dicts_find_unequal_fields(
198+
one_dict=first.__dict__ if isinstance(first, Base) else first,
199+
other_dict=second.__dict__ if isinstance(second, Base) else second,
200+
fast_return=False,
201+
skip_db_id_check=skip_db_id_check,
202+
)
203+
)
204+
unequal_types.update(unequal_types_recursive)
205+
unequal_val.update(unequal_val_recursive)
198206
unequal_types_suffixed = {
199207
f"{k} (field had values of unequal type)": v for k, v in unequal_types.items()
200208
}
@@ -223,7 +231,11 @@ def _unequal_str(first: Any, second: Any) -> str:
223231
"unreachable."
224232
)
225233
msg += f"\n{indent}{bul} {field}: {_unequal_str(first=first, second=second)}\n"
226-
if isinstance(first, (dict, Base)) and isinstance(second, (dict, Base)):
234+
if "(field had values of unequal type)" in field:
235+
return msg
236+
if type(first) is not type(second) or (
237+
isinstance(first, (dict, Base)) and isinstance(second, (dict, Base))
238+
):
227239
msg += _build_comparison_str(
228240
first=first,
229241
second=second,

0 commit comments

Comments
 (0)