Skip to content

ConfigParser has_section() documentation is oblique and not clear #132478

Closed
@pacohope

Description

@pacohope

Documentation

The documentation on has_section() currently says:

Indicates whether the named section is present in the configuration. The default section is not acknowledged.

First, it doesn't give indication of the method's return type (boolean), it's just implied. Secondly "is not acknowledged" is an unhelpful and oblique way of saying "returns False". Documentation should say what code does do, not what it doesn't. My recommendation for clearer documentation:

Returns True if the named section is present in the configuration. Returns False if the named section is not present in the configuration. Returns False if section is 'default'. Returns False if default_section was set on initialization, and section is the same as the value of default_section.

I was surprised at the behaviour of has_section() when I use default_section on initialization. After investigating I see that some of this is expected behaviour. I recommend adding the sentence "Returns False if section is 'default'" to the documentation, because that seems to be true, whether I specified default_section or not. If this is expected behaviour, then the docs should be updated.

The code snippet below produces the following output. Test 3 is the one that surprises me. It doesn't seem consistent with the documentation because in Test 3, default is not the name of the default section. All the other cases make sense and seem consistent with the documentation:

Test 1: default_section='section1' and no section named [default] present
  has_section1: False, has_section2: True, has_default: False

Test 2: default_section not defined and no section named [default] present
  has_section1: True, has_section2: True, has_default: False

Test 3: default_section='section1' and [default] section also present
  has_section1: False, has_section2: True, has_default: False

Test 4: default_section not defined and [default] section is present
  has_section1: True, has_section2: True, has_default: False

code snippet

from configparser import ConfigParser, ExtendedInterpolation

configstring = """
[section1]
testkey = True

[section2]
demokey = True
"""

print("Test 1: default_section='section1' and no section named [default] present")
config = ConfigParser(default_section='section1', interpolation=ExtendedInterpolation())
config.read_string(configstring)
print("  has_section1: {}, has_section2: {}, has_default: {}\n".format(config.has_section('section1'),
  config.has_section('section2'),
  config.has_section('default')))

print("Test 2: default_section not defined and no section named [default] present")
del config
config = ConfigParser(interpolation=ExtendedInterpolation())
config.read_string(configstring)
print("  has_section1: {}, has_section2: {}, has_default: {}\n".format(config.has_section('section1'),
  config.has_section('section2'),
  config.has_section('default')))

configstring = """
[section1]
testkey = True

[section2]
demokey = True
"""
print("Test 3: default_section='section1' and [default] section also present")
config = ConfigParser(default_section='section1', interpolation=ExtendedInterpolation())
config.read_string(configstring)
print("  has_section1: {}, has_section2: {}, has_default: {}\n".format(config.has_section('section1'),
  config.has_section('section2'),
  config.has_section('default')))

print("Test 4: default_section not defined and [default] section is present")
del config
config = ConfigParser(interpolation=ExtendedInterpolation())
config.read_string(configstring)
print("  has_section1: {}, has_section2: {}, has_default: {}\n".format(config.has_section('section1'),
  config.has_section('section2'),
  config.has_section('default')))

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dirpendingThe issue will be closed if no feedback is provided

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions