Skip to content
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

constant fold literals in f", .format and % formatting #353

Open
graingert opened this issue Nov 3, 2020 · 6 comments
Open

constant fold literals in f", .format and % formatting #353

graingert opened this issue Nov 3, 2020 · 6 comments

Comments

@graingert
Copy link
Contributor

I have a project with a bunch of tests that check logging messages (with testfixtures.LogCapture) they look like this:

log.check((module.__name__, "INFO", "the user {user!r} did a bad".format(user=u"user")))

The reason - because the repr of u"user" is different on py3 and py2. I'd like to see pyupgrade --py3-plus rewrite them all to:

log.check((module.__name__, "INFO", "the user 'user' did a bad"))

@graingert
Copy link
Contributor Author

It might also be good to hit the explicit str concat "the user" + repr(u"user") + "did a bad". and %r formatting

@graingert graingert changed the title constant fold string literals, .format and % formatting constant fold literals in f", .format and % formatting Apr 19, 2021
@graingert
Copy link
Contributor Author

this also applies to numeric literals, I just noticed pyupgrade apply this change:

-                        "hostname": "{}:{}".format(dc, 636),
+                        "hostname": f"{dc}:{636}",

but I'd expect f"{dc}:636"

@adamantike
Copy link

adamantike commented Oct 23, 2021

Also found this issue when upgrading a codebase with --py36-plus, using pyupgrade 2.29.0:

-BROKER_URL = "{redis}/{db}".format(redis=REDIS_URL, db=0)
+BROKER_URL = f"{REDIS_URL}/{0}"

@henryiii
Copy link

Different issue but possibly similar fix:

f"{repr(x)}"

Could be converted to:

f"{x!r}"

These happen to be common from "{0}".format(repr(x)) conversions.

@mambelli

This comment was marked as off-topic.

@asottile

This comment was marked as resolved.

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

No branches or pull requests

5 participants