Skip to content

Add sort_dicts parameter to reprlib.Repr (gh-155812) - #155817

Closed
SparshGarg999 wants to merge 2 commits into
python:mainfrom
SparshGarg999:feat-reprlib-sort-dicts-155812
Closed

Add sort_dicts parameter to reprlib.Repr (gh-155812)#155817
SparshGarg999 wants to merge 2 commits into
python:mainfrom
SparshGarg999:feat-reprlib-sort-dicts-155812

Conversation

@SparshGarg999

@SparshGarg999 SparshGarg999 commented Aug 15, 2026

Copy link
Copy Markdown

Fixes gh-155812

Context & Feature Motivation

reprlib.Repr currently sorts dictionary keys unconditionally when generating truncated string representations (repr_dict). In applications where dictionary insertion order conveys structural or semantic meaning (such as ordered configuration dictionaries or execution AST maps), key sorting distorts the original sequence.

Solution & Changes

  • Added a sort_dicts: bool = True keyword parameter to reprlib.Repr.__init__ in Lib/reprlib.py (defaulting to True to maintain backward compatibility).
  • Updated repr_dict in Lib/reprlib.py to respect self.sort_dicts when formatting dictionary keys.

Testing & Verification

  • Tested reprlib.Repr(sort_dicts=False) and reprlib.Repr(sort_dicts=True) formatting on dictionaries with non-alphabetical insertion orders.

@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@bedevere-app

bedevere-app Bot commented Aug 15, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app

bedevere-app Bot commented Aug 15, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz picnixz closed this Aug 15, 2026
@picnixz

picnixz commented Aug 15, 2026

Copy link
Copy Markdown
Member

Do not use AI to make your work without undersranding or we will ban you from our repositories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add sort_dicts parameter to reprlib.Repr

2 participants