Repository navigation
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 frompoints) and eliminates the accumulator.I don't think this merits a news entry.