Skip to content

gh-130928: Fix error message during bytes formatting for the 'i' flag#130967

Merged
sobolevn merged 5 commits into
python:mainfrom
ApostolFet:fix-issue-130928
Mar 24, 2025
Merged

gh-130928: Fix error message during bytes formatting for the 'i' flag#130967
sobolevn merged 5 commits into
python:mainfrom
ApostolFet:fix-issue-130928

Conversation

@ApostolFet

@ApostolFet ApostolFet commented Mar 7, 2025

Copy link
Copy Markdown
Contributor

Error output before correction:

>>> b"%i" % "str"
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    b"%i" % "str"
    ~~~~~~^~~~~~~
TypeError: %d format: a real number is required, not str

Error output after correction:

>>> b"%i" % "str"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    b"%i" % "str"
    ~~~~~~^~~~~~~
TypeError: %i format: a real number is required, not str

@jstasiak jstasiak left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think I'd add a unit test for this.

@sobolevn sobolevn 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.

Yes, please, add a unit test :)
And also a NEWS entry. This is a user-visible change.

@sobolevn sobolevn 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.

Thanks, I think that the fix is correct.
But, let's wait for @serhiy-storchaka review, because his is the code owner.

Comment thread Lib/test/test_bytes.py
@sobolevn

sobolevn commented Mar 9, 2025

Copy link
Copy Markdown
Member

You can also add yourself to https://github.com/python/cpython/blob/main/Misc/ACKS

Co-authored-by: sobolevn <mail@sobolevn.me>
@ApostolFet

Copy link
Copy Markdown
Contributor Author

You can also add yourself to https://github.com/python/cpython/blob/main/Misc/ACKS

Thanks, but I've already taken advantage of that opportunity earlier

@sobolevn

sobolevn commented Mar 9, 2025

Copy link
Copy Markdown
Member

I would also suggest to create a new issue to add tests for bytes formatting options. Please, tag me - I can assign you this issue if you want.

@sobolevn

Copy link
Copy Markdown
Member

@serhiy-storchaka friendly ping, what do you think? :)

@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.

I think that test_common_format in test_format.py may be a better place for the tests. There are already tests for %d and %x. You can just add

        test_exc_common('%i', '1', TypeError,
                        "%i format: a real number is required, not str")

Or you can leave tests in both places.

@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.

LGTM.

@sobolevn sobolevn 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.

Thanks again!

@sobolevn sobolevn merged commit 7c3692f into python:main Mar 24, 2025
@sobolevn

Copy link
Copy Markdown
Member

We usually do not backport error message changes.

@ApostolFet ApostolFet deleted the fix-issue-130928 branch March 26, 2025 07:26
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 1, 2025
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.

4 participants