Skip to content

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

Merged
merged 4 commits into from
Sep 30, 2021
Merged

Fixes error message for f string and str-bytes-safe #11139

merged 4 commits into from
Sep 30, 2021

Conversation

sobolevn
Copy link
Member

It does not mention .format() anymore.

Closes #10979

@@ -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 '
Copy link
Member

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:

Suggested change
'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?

Copy link
Member Author

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?

Copy link
Member

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().

Copy link
Member Author

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

@sobolevn
Copy link
Member Author

sobolevn commented Sep 29, 2021

@JelleZijlstra please, check out new error message. What do you think?

@JelleZijlstra JelleZijlstra merged commit 209a719 into python:master Sep 30, 2021
@Olaktal
Copy link

Olaktal commented Mar 23, 2022

Hello

I encounter this error also ...

poetry run mypy --version        
mypy 0.931

poetry run mypy helpers/aws/s3.py
helpers/aws/s3.py:490: error: On Python 3 formatting "b'abc'" with "{}" produces "b'abc'", not "abc"; use "{!r}" if this is desired behavior

helpers/aws/s3.py

                        raise S3Exception(
                            f"cannot convert following line to json: {line}"
                        ) from json_convert_error

Any idea why ? My version seem pretty recent so I don't understand why it is not fixed 🤔

hauntsaninja added a commit to hauntsaninja/mypy that referenced this pull request Mar 26, 2023
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.
hauntsaninja added a commit that referenced this pull request Mar 29, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants