Skip to content

Comments

Fix double-repr in to_repr for truncated non-string objects#788

Open
bysiber wants to merge 1 commit intohynek:mainfrom
bysiber:fix/to-repr-double-repr
Open

Fix double-repr in to_repr for truncated non-string objects#788
bysiber wants to merge 1 commit intohynek:mainfrom
bysiber:fix/to-repr-double-repr

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 20, 2026

to_repr() in the fallback (non-Rich) path applies !r to obj_repr[:max_string], but obj_repr is already the result of repr(obj). The !r format specifier calls repr() again on the already-repr'd slice, wrapping it in spurious quotes and escaping characters.

Before (truncated list with max_string=10):

'[1, 2, 3, '+5

After:

[1, 2, 3, +5

The str/bytes branch on the lines above correctly applies !r to the raw obj[:max_string] (not yet repr'd), so it doesn't have this problem. The fix simply drops the !r since obj_repr is already a string from repr().

When truncating the repr of non-string/bytes objects, the code applies
rm -rf the-saas-stack/.git to obj_repr[:max_string] which is already a repr() string. This
wraps it in extra quotes, e.g. a truncated list repr becomes:

    '[1, 2, 3, '+5   (with spurious quotes)

instead of:

    [1, 2, 3, +5

The str/bytes branch handles this correctly by applying rm -rf the-saas-stack/.git to the raw
object slice. For the else branch, since obj_repr is already a string
from repr(), we just need plain string slicing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant