GeezerVison is showing github.com — gh-159684: Modernize string formatting and simplify constructs in configparser by jaraco · Pull Request #159687 · python/cpython
Conversation
jaraco
commented
Oct 18, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Brings Lib/configparser.py and Lib/test/test_configparser.py up to Ruff's
default rule set, which reported 72 findings across the two files. CPython's
own Ruff configuration only selects F401 and F811 for Lib/, so nothing
here was failing; the motivation is readability plus keeping the module
convergent with its PyPI backport, which lints with the defaults and would
otherwise need a per-file exemption for exactly these two files
(jaraco/configparser#73).
Four commits, each reviewable on its own:
- f-strings (
UP030, UP031, UP032) — the autofixable conversions,
with the lines that would have exceeded 79 columns rewrapped by hand. The
rendered strings are unchanged; %% escapes collapse to % as the %
operator did.
- Simplifications —
C405, C408, C414, RUF019, RUF059, F841,
SIM102, SIM117, and parenthesizing an implicit string concatenation
inside a list literal (ISC004) so it does not read as a missing comma.
- The remaining
% sites that Ruff declines to autofix because the %d
conversions are not provably integral. Converted by hand, keeping an
explicit :d so a non-integer still raises rather than being coerced.
- Imports,
__all__, and ClassVar (I001, RUF022, RUF012).
The last one is the only part with a design decision in it. RUF012 wants
BOOLEAN_STATES annotated ClassVar, and the obvious spelling would import
typing at runtime — which this module deliberately avoids, per the
gh-117703 note above its imports. So it adds from __future__ import annotations and imports ClassVar only under TYPE_CHECKING, the same shape
Lib/tomllib/_parser.py and Lib/importlib/metadata/__init__.py use (both
also kept in sync with a PyPI backport). python -X importtime -c "import configparser" confirms typing stays out of the import graph. Deferring
annotations also lets Iterable['ParsingError'] drop its quotes (UP037).
That does make TYPE_CHECKING a module-level name, so it joins Error in
not_exported in the check__all__ test.
C901 on ExtendedInterpolation._interpolate_some (13 > 10) is the one
finding not fixed. It is a measurement, not a defect, and no small edit
satisfies it, so it gets # noqa: C901 # gh-159686; gh-159686 records why the
branching is inherent to the interpolation grammar and sketches a possible
refactoring, so the suppression is documented rather than silent.
With all four applied, both CPython's Ruff configuration and the default rule
set report no findings for either file, at py310 and py314 targets.
test_configparser, test_logging, test_collections, test___all__ and
test_pydoc pass.
NEWS entry added under Library; there is no user-visible behavior change.
🤖 Generated with Claude Code
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Convert the remaining printf-style and str.format() call sites in
Lib/configparser.py and Lib/test/test_configparser.py to f-strings
(Ruff UP030, UP031, UP032), rewrapping the lines that would otherwise
exceed 79 columns. No behavior change; the rendered strings are
identical.
Co-Authored-By: Claude Opus 5 <[email protected]>
Apply the straightforward simplifications Ruff reports for these two
modules: redundant list() calls (C408, C414), a literal set (C405),
a key check that get() already covers (RUF019), an unpacked value that
is never used (RUF059), an unused assertRaises binding (F841),
collapsible if statements (SIM102) and nested with statements (SIM117).
Also parenthesize an implicit string concatenation inside a list
literal (ISC004) so it is not mistaken for a missing comma.
Co-Authored-By: Claude Opus 5 <[email protected]>
Ruff leaves these UP031 sites alone because the %d conversions are not
provably integral, so convert them by hand, keeping the explicit :d
format spec so a non-integer still raises rather than being coerced.
Co-Authored-By: Claude Opus 5 <[email protected]>
Sort the imports (I001) and __all__ (RUF022), and annotate the mutable
class attributes as ClassVar (RUF012). The ClassVar annotation would
otherwise mean importing typing at runtime, which this module avoids,
so defer annotations with __future__ instead and import ClassVar only
under TYPE_CHECKING, as tomllib and importlib.metadata do. Deferring
annotations also lets ParsingError be referenced unquoted (UP037).
ExtendedInterpolation._interpolate_some gets an explicit noqa: C901,
tagged with pythongh-159686, which records why the complexity is inherent to
the interpolation grammar and what a refactoring might look like.
Co-Authored-By: Claude Opus 5 <[email protected]>
|
|
config_string = f"""\
|
|
|
[Foo Bar]
|
|
|
foo{0[0]}bar1
|
|
|
foo{self.delimiters[0]}bar1
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A good name is better than 0[0], but the repetitive long indexed name is better and worse. Add a human
touch by adding `open, close = self.delimiters at the top and then replace s.d[0] and s.d[1] with open and close.
Same where appropriate for other methods. Need to be cognizant that some calls to newconfig change delimiters. (This seems to be the main use of the method.)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think refactors need a news entry.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|