[Python-Dev] configparser: should optionxform be idempotent?

Inada Naoki songofacandy at gmail.com
Thu Mar 7 04:18:27 EST 2019

Hi, all.

I came from https://bugs.python.org/issue35838
Since there are no "expert" for configparser in
Expert Index, I ask here to make design decision.

The default behavior of CofigParser.optionxform
is str.lowercase().  This is used to canonicalize
option key names.

The document of the optionxform shows example
overrides it to identity function `lambda option: option`.

BPO-35838 is issue about optionxform can be called twice
while ConfigParser.read_dict().
If optionxfrom is not idempotent, it creates unexpected option

But even if all APIs calls optionxform exactly once, user may
read option name and value, and write updated value with same name.
In this case, user read option name already optionxform-ed
(canonicalized).  So non-idempotent optionxform will break
option name.

So what should we do about optionxform?

a)  Document "optionxform must be idempotent".

b) Ensure all APIs calls optionxform exactly once, and document
   "When you get option name from section objects, it is already
    optionxform-ed.  You can not reuse the option name if
    optionxform is not idempotent, because optionxform will be
    applied to the name again."

I prefer (a) to (b) because it's simple and easy solution.

But for some use cases (e.g. read only, write only, use only
predefined option name and read only it's value), (b) works.
At least issue reporter try this use case and be trapped by
this behavior.

How do you think?

Inada Naoki  <songofacandy at gmail.com>

More information about the Python-Dev mailing list