pywin32 icon indicating copy to clipboard operation
pywin32 copied to clipboard

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

Open Avasam opened this issue 1 year ago • 2 comments

Follow-up to https://github.com/mhammond/pywin32/pull/2122

Merging https://github.com/mhammond/pywin32/pull/2278 first will reduce changes

Ran using ruff check --select=UP031 --exclude=adodbapi --unsafe-fixes --fix 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",)

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 avatar Jun 04 '24 19:06 Avasam

Ruff was updated to catch more cases that are not autofixable. I'll have to split this one in more gradual parts.

Avasam avatar Jan 07 '25 21:01 Avasam

I'm having real trouble getting excited about changes dictated by an external tool which don't actually fix or change any functionality. These kinds of changes (and many of the other PRs, particularly ones described as "prefer x over y") are IMO best done when the code is otherwise being changed. Some of the typing related changes seem good if they help consumers of these packages (eg, typing params to public functions), but in the same way, internal changes which only type things but otherwise make no changes seem like busy work.

mhammond avatar Jan 08 '25 16:01 mhammond