Skip to content

gh-141540: use %r instead of %s in error messages - #141541

Closed
alexwlchan wants to merge 1 commit into
python:mainfrom
alexwlchan:use-repr-format-code
Closed

alexwlchan wants to merge 1 commit into
python:mainfrom
alexwlchan:use-repr-format-code

Conversation

@alexwlchan

@alexwlchan alexwlchan commented Nov 14, 2025 •

Copy link
Copy Markdown

I fixed the original example, then looked for other examples of '%s' and "%s" in error messages that might want to be replaced with %r. This isn't all of them, but it was all the cases where I felt confident I understood the original code, the printed value was a str, and this seemed like a sensible suggestion.

I had issues running the tests locally and it's late here, so I'm going to open this PR now and review any CI failures in the morning. 😴

@bedevere-app

bedevere-app Bot commented Nov 14, 2025

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 Nov 14, 2025

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.

1 similar comment
@bedevere-app

bedevere-app Bot commented Nov 14, 2025

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.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some error messages match corresponding error messages in the C code which do not use repr.

Some cases are not so simple. For example the repr of the path on Windows is less readable, and paths on Windows cannot contain newlines, quotes or other control characters. On other hand, if the string which is supposed to represent a path contain weird characters, the repr is more helpful. We use repr in some cases (especially if the path can be bytes) and str in other cases (especially if the path can be Path). Each case should be considered carefully.

Comment thread Lib/test/support/smtpd.py Outdated
Comment thread Lib/operator.py Outdated
Comment thread Lib/idlelib/iomenu.py Outdated
Comment thread Lib/_pydecimal.py Outdated
Comment thread Lib/_strptime.py Outdated
@bedevere-app

bedevere-app Bot commented Nov 15, 2025

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.

@alexwlchan

Copy link
Copy Markdown
Author

Since I only expanded beyond the initial _strptime.py cases based on a suggestion on the original issue, and I don't care to go through these cases in more detail, I've reduced the scope to just that file. I've also converted all the string formatting in that file to f-strings for consistency.

Comment thread Lib/_strptime.py Outdated
Comment thread Lib/_strptime.py Outdated
@bedevere-app

bedevere-app Bot commented Nov 15, 2025

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 Nov 15, 2025

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.

@alexwlchan alexwlchan closed this by deleting the head repository Feb 16, 2026
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.

3 participants