Fixes error message for f string and str-bytes-safe#11139
Fixes error message for f string and str-bytes-safe#11139JelleZijlstra merged 4 commits intopython:masterfrom sobolevn:issue-10979
f string and str-bytes-safe#11139Conversation
| 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.
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.
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.
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.
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