Skip to content

configparser: BasicInterpolation raises TypeError interpolating a value-less option #153855

Description

@abdoulrasheed

Bug report

Bug description:

With allow_no_value=True and the default BasicInterpolation, interpolating a reference to a value-less option raises TypeError instead of resolving or raising a configparser error. ExtendedInterpolation treats the missing value as empty in the same situation.

import configparser
cfg = configparser.ConfigParser(allow_no_value=True)
cfg.read_string("[s]\na\nb = x%(a)sy\n")
cfg.get("s", "b")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    cfg.get("s", "b")
    ~~~~~~~^^^^^^^^^^
  File "../Lib/configparser.py", line 855, in get
    return self._interpolation.before_get(self, section, option, value,
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                          d)
                                          ^^
  File "../Lib/configparser.py", line 433, in before_get
    self._interpolate_some(parser, option, L, value, section, defaults, 1)
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "../Lib/configparser.py", line 474, in _interpolate_some
    if "%" in v:
      ^^^^^^^^
TypeError: argument of type 'NoneType' is not a container or iterable

The same pattern with ExtendedInterpolation (b = x${s:a}y) returns "xy".

ExtendedInterpolation._interpolate_some guards this (if v is None: continue, added in gh-130941); BasicInterpolation._interpolate_some does not and reaches if "%" in v: with v set to None (Lib/configparser.py).

Observed on 3.16.0a0.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions