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

Stop using printf-style string formatting for direct string interpolation (%s) #2277

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Avasam
Copy link
Collaborator

@Avasam Avasam commented Jun 4, 2024

Follow-up to #2122

Ran using ruff check --fix --unsafe-fixes --select=UP031 --exclude=adodbapi then ruff format
This replaces all usages of exclusively %s for string formatting with newer style .format introduced in Python 2.6 .
This autofix is considered "unsafe" when Ruff cannot statically establish that the following case is always false:

A single %s in the string, formatted with a 1-item tuple. ie: "foor%s" % ("bar",)

Due to the sheer amount o changes (359) this is kept to automated changes only.

%d and %r will be done in separate PRs as they're not handled by UP031.
Preferring f-strings will come as a follow-up since that can make the line go over the 100 chars limit.

References:

https://docs.astral.sh/ruff/rules/printf-string-formatting/#why-is-this-bad

printf-style string formatting has a number of quirks, and leads to less readable code than using str.format calls or f-strings. In general, prefer the newer str.format and f-strings constructs over printf-style string formatting.

https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting

Note The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals, the str.format() interface, or template strings may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.

@Avasam Avasam requested a review from mhammond October 18, 2024 18:24
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

Successfully merging this pull request may close these issues.

1 participant