Python dicts preserve insertion order since 3.6 and this is behavior is guaranteed since 3.7. This order can carry meaningful information. reprlib currently discards this information even when the caller only wants its size-limiting and recursive representation features.
Proposal: Add a sort_dicts parameter to reprlib.Repr, defaulting to True for backward compatibility:
repr = reprlib.Repr(sort_dicts=False)
When False, dictionary entries should be represented in their native iteration order rather than sorted order. Keeping the default as True avoids changing existing output, while sort_dicts=False gives users who rely on insertion order an explicit opt-in.
This proposed API mirrors pprint.PrettyPrinter(sort_dicts=...), providing users with an established and familiar way to control dictionary ordering.
The implementation is very simple: Just guard the call to _possibly_sorted(x)in the repr_dict method with a self.sort_dicts check. I would link to the source file, but Github is having issues currently.
References:
- dict insertion order What’s New In Python 3.7 — Python 3.14.7 documentation
- pprint/sort_dicts What’s New In Python 3.8 — Python 3.14.7 documentation