Skip to content

write_config converts values of type NoneType to str #99

Description

@me-kell

--remember-answers converts values of type NoneType to str and writes the string 'None' to the config file .mrbob.ini.
This causes problems when using the answers file in non-interactive mode with the option --config. The ConfigParser only reads strings and reads the string 'None' as a str and not as a NoneType as we would expect.

mr.bob/mrbob/parsing.py

Lines 87 to 98 in ccc91b4

def write_config(fs_config, section, data):
parser = configparser.ConfigParser(dict_type=OrderedDict)
parser.add_section(section)
for key, value in data.items():
if not isinstance(value, six.string_types):
value = str(value)
if not six.PY3: # pragma: no cover
value = value.encode("utf-8")
parser.set(section, key, value)
with open(fs_config, "w") as f:
parser.write(f)

One possible solution could be not to convert values of type NoneType to str and let them as None.
The RawConfigParser already writes only non None values and it changes None values to empty strings:

https://github.com/python/cpython/blob/605022aeb69ae19cae1c020a6993ab5c433ce907/Lib/configparser.py#L978-L991

            if value is not None or not self._allow_no_value:
                value = delimiter + str(value).replace('\n', '\n\t')
            else:
                value = ""

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions