-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-124294: str.format() raises misleading error when using negative indices #124372
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
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 |
|
@ericvsmith I think I've found the right place for the check. |
|
Maybe you should added NEWS, Used to describe your changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And you, probably, should add a test, right?
Lib/test/test_str.py
Outdated
| self.assertRaises(TypeError, "{[s]}".format, "abc") | ||
| self.assertRaises(TypeError, "{[-1]}".format, "abc") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is something, that already fails with TypeError in the main. I think you should test actual error messages with assertRaises's msg argument or using assertRaisesRegex().
Objects/stringlib/unicode_format.h
Outdated
| if (PySequence_Check(obj)) { | ||
| /* string index can't be passed to sequence */ | ||
| PyObject *str = SubString_new_object(&name); | ||
| if (str != NULL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (str != NULL) | |
| if (str) |
Also beware PEP 7: "braces are required everywhere, even where C permits them to be omitted".
|
I still am not convinced this is a good idea. I don't think it's worth complicating things to get a slightly better error message. For example, is changing all possible errors from |
| self.assertEqual("{!s}".format(n), 'N(data)') | ||
| self.assertRaises(TypeError, "{}".format, n) | ||
|
|
||
| # String index for sequences |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could delete this comment since the user won't see it anyway.
|
@ericvsmith I can't say if it's worth, but this issue has quite a long history already:
As long as I'm not totally satisfied with proposed message
The index value could be omitted to not complicate things, as you suggested (no intermediate
Or there is a better wording? |
|
This code works on 3.12, but fails with your patch: class F:
def __getitem__(self, key):
return f'key:{key}'
f = F()
print(f['foo'])
print('{f[foo]}'.format(f=f))With 3.12, this gives: With this PR, it gives: |
|
@ericvsmith now I see how naive was my attempt. The only solution could be parsing negative integers from the very beginning, that is already impossible. Thank you for your clarification and hints which I didn't take seriously. |
|
No problem, @makukha! I happen to be very familiar with this code, and it's not reasonable for everyone to know all the ins and outs. I'm glad you learned something, and please don't be discouraged from continuing to contribute. If you agree, I'll close this, and hopefully people can find it and refer to it in the future. But if you want to leave it open and continue to try and find a way to improve this (anything is possible!), then that's okay, too. |
|
@ericvsmith, being realistic, I think this PR should be closed. Thank you for your warm attitude! |
Fixes #124294.
TypeError is raised instead of ValueError for backward compatibility with current behaviour.
Detailed error message includes invalid string index value to avoid ambiguity.
Without these details, user might be confused by error pointing to the wrong place.