Fix inspect() not respecting sort=False for instance attributes #3913
+51
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Type of changes
AI?
Checklist
Description
Fixed issue #3432 where
inspect()was not respecting thesort=Falseparameter for instance attributes.Problem
When calling
rich.inspect(obj, sort=False), the attributes were still displayed in alphabetically sorted order instead of preserving their insertion order.Root Cause
The
_render()method was usingdir()to get the list of attributes. Since Python'sdir()always returns attributes in sorted order, even whensort=Falsewas specified, the attributes were already sorted before the sorting logic was applied.Solution
Modified the
_render()method inrich/_inspect.pyto:sort=Falseand the object has a__dict__, usevars(obj)to get instance attributes in their insertion order (Python 3.7+ guarantees dict insertion order)dir()sort=Trueis explicitly setChanges
_render()method inrich/_inspect.pytest_inspect_sort_parameter()test caseTesting
sort=Falsepreserves insertion order andsort=Truesorts alphabeticallyImportant: Fixes #3432