-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Fixes error message for f
string and str-bytes-safe
#11139
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
mypy/checkstrformat.py
Outdated
@@ -380,8 +380,9 @@ def perform_special_format_checks(self, spec: ConversionSpecifier, call: CallExp | |||
if (has_type_component(actual_type, 'builtins.bytes') and | |||
not custom_special_method(actual_type, '__str__')): | |||
self.msg.fail( | |||
"On Python 3 '{}'.format(b'abc') produces \"b'abc'\", not 'abc'; " | |||
"use '{!r}'.format(b'abc') if this is desired behavior", | |||
'On Python 3 formatting string with "{}" and bytes argument ' |
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.
The new wording is harder to understand. I'd suggest something closer to the original wording but using an f-string, like this:
'On Python 3 formatting string with "{}" and bytes argument ' | |
"On Python 3 f'{b"abc"}' produces \"b'abc'\", not 'abc'; " |
However, it looks like the same code path is used for f-strings and .format()
. Is it easy to use separate messages for each of these?
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.
Is it easy to use separate messages for each of these?
Currently mypy
uses the same exact ast
to represent both .format
and f
strings. I can add new metadata to CallExpr
(which is used for representation). But, I've decided that simply changing an error message is the way to go.
Maybe you can come up with better wording?
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.
Then I suggest we just always use the f-string version of the message if we're type checking Python 3, since I would assume that's the more likely case. Mypy doesn't support 3.5 any more. If this message is triggered when we're checking Python 2 code (can it?) we should still mention .format()
.
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.
Then I suggest we just always use the f-string version of the message if we're type checking Python 3, since I would assume that's the more likely case.
I cannot agree, for example in my projects f
string is not allowed by our styleguide: https://wemake-python-stylegui.de/en/latest/pages/usage/violations/consistency.html#wemake_python_styleguide.violations.consistency.FormattedStringViolation So, I know that a lot of people still use .format()
🙂
But, we can use the common part like {}
and {!r}
. Maybe something like
On Python 3 '{b"abc"}' produces \"b'abc'\", not 'abc'; use "{b"abc"!r}" if this is desired behavior
@JelleZijlstra please, check out new error message. What do you think? |
Hello I encounter this error also ...
Any idea why ? My version seem pretty recent so I don't understand why it is not fixed 🤔 |
Fixes python#11806 This error message was last changed in python#11139 to make it more applicable to f-strings. Since users are still getting confused, it makes sense to be more explicit about what the recommended fix is. There was also an extra set of quotes and a mention of Python 3 that didn't help.
Fixes #11806 This error message was last changed in #11139 to make it more applicable to f-strings. Since users are still getting confused, it makes sense to be more explicit about what the recommended fix is. There was also an extra set of quotes and a mention of Python 3 that didn't help. I think this is still very much a useful error, it caught a bug I had recently.
It does not mention
.format()
anymore.Closes #10979