ConfigParser interpolation and case folding can change how a stored option is read.
Make this comfortable
Python ConfigParser: read literal percent signs without hidden interpolation
Operation contract
A controlled export configuration contains a literal percent sign. With interpolation disabled, the application reads those bytes as text. A second configuration repeats the same option with different letter case; strict parsing rejects that duplicate before it can replace the first value.
Failure boundary
Disabling interpolation does not validate business values, protect secrets, or decide which configuration file is trusted. ConfigParser stores string values; parse and range-check each field before publishing settings to workers.
Working program
import configparser
settings = configparser.ConfigParser(interpolation=None, strict=True)
settings.read_string("[export]\npattern = paid%complete\nretention_days = 47\n")
print("pattern", settings["export"]["pattern"])
print("retention", settings.getint("export", "retention_days"))
duplicate = configparser.ConfigParser(interpolation=None, strict=True)
try:
duplicate.read_string("[export]\nRetention = 47\nretention = 73\n")
except configparser.DuplicateOptionError:
print("case_duplicate_rejected", True)Output
pattern paid%complete
retention 47
case_duplicate_rejected TrueCosts and limits
Configuration parsing allocates values proportional to the source text. The input file still needs an ownership and size policy before parsing.
Common Mistakes
- Default interpolation treats percent syntax as special.
- Option names are case-insensitive by default, which affects duplicate detection.
- getint converts text but does not enforce a domain range.
Connected lessons
python
configparser-literal-percent
