You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Calling unittest.assertDictEqual for medium-size dictionaries takes too long #99151
I think a simple difflib.ndiff over pprint is a bad choice for 'assertDictEqual`. The output it gives is often very confusing, like this:
E AssertionError: {'second': 2, 'seventh': 7, 'sixth': 6, 'fo[53 chars]': 3} != {'first': 1, 'seventh': 7, 'ninth': 9, 'sec[66 chars]': 3}
E {'eighth': 8,
E 'fifth': 5,
E 'first': 1,
E 'fourth': 4,
E + 'ninth': 9,
E 'second': 2,
E 'seventh': 7,
E - 'sixth': 6,
E - 'third': 3}
E ? ^
E
E + 'third': 3,
E ? ^
E
E + 'zeroth': 0}
I put together a better implementation which gives much nicer output, handles nested structures, and is WAY faster than the current implementation for the test given.
E AssertionError: {'second': 2, 'seventh': 7, 'sixth': 6, 'fo[53 chars]': 3} != {'first': 1, 'seventh': 7, 'ninth': 9, 'sec[66 chars]': 3}
E
E Diff:
E {
E 'eighth': 8,
E 'fifth': 5,
E 'first': 1,
E 'fourth': 4,
E 'second': 2,
E 'seventh': 7,
E 'third': 3,
E - 'sixth': 6,
E + 'ninth': 9,
E + 'zeroth': 0,
E }
I would be happy to contribute my code if that is desired.
Activity
ronaldoussoren commentedon Dec 22, 2022
See also #51180 about slowness in difflib.ndiff (used by assertDictEquals to report about differences).
One avenue to explore would be to pick one of the other diff functions: unified_diff appears to be a lot faster, although this does change the output.
merlinz01 commentedon Nov 16, 2024
I think a simple
difflib.ndiffoverpprintis a bad choice for 'assertDictEqual`. The output it gives is often very confusing, like this:I put together a better implementation which gives much nicer output, handles nested structures, and is WAY faster than the current implementation for the test given.
I would be happy to contribute my code if that is desired.