Skip to content

gh-159686: reduce the code complexity of configparser interpolation - #162605

Open
sirosen wants to merge 4 commits into
python:mainfrom
sirosen:simplify-configparser-interpolation
Open

sirosen wants to merge 4 commits into
python:mainfrom
sirosen:simplify-configparser-interpolation

Conversation

@sirosen

@sirosen sirosen commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

I've tried to make the code for interpolation easier to follow, as called out in #159686.
Although the issue primarily flags complexity limits, I've made some additional changes in pursuit of readability.

@jaraco, I'm curious if this matches up with your intent and expectations around that issue.
I've tried to leave a sequence of readable commits here, and the first one is the most closely aligned with the original request.
Converting to generators is also fairly significant: it makes the flow of control more explicit (you can see the yield from points) and eliminates the accumulator.

I don't think this merits a news entry.

In configparser, interpolation -- both basic and extended -- follows a
pattern in which an interpolation delimiter is used to detect the start
of a match, and then the suite under that guard:

1. scans for the next interpolation match
2. errors if there was no match
3. slices the remainder string (thus advancing the overall state of the
   string scanning at work)
4. pulls out the reference information for what values to interpolate
5. looks up that reference information
6. recurses, doing further interpolations on the result

We can bundle together the responsibilities of 1-5 of these steps into a
new helper, `_do_one_interpolation()`, with the same shape for both
basic and extended interpolation (basic interpolation will not use all
of the return values) to improve readability.

As an included readability change, the return values `opt` and `sect`
(used in extended interpolation) are renamed to `ref_option` and
`ref_section`, in order to better disambiguate them from the originating
`option` and `section` variables, which are necessarily handled in the
same scope.
For basic and extended interpolation, improve the naming of local
variables to be clearer -- `pos` for position and `next_char` rather
than `c` -- and use a walrus-inside a comparison for the loop conditions
so that the loops are "while there is more interpolation to do" rather
than "while there is more string to consume".

The overall result is slightly less nested control flow and slightly more
readable naming.
Rather than appending data to an accumulator list, yield items in a
generator. This has two key benefits:

1. Reduces complexity, by removing a variable we had to pass around

2. The sub-accumulators in recursive calls are now explicitly marked
   with `yield from` syntax
(parser, section, option, value, ...) is the ordering used in public
APIs, so make it the ordering used in private APIs as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant