Skip to content

gh-127011: Add __str__ and __repr__ to ConfigParser #132966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ def __init__(self, defaults=None, dict_type=_default_dict,
self._optcre = re.compile(self._OPT_TMPL.format(delim=d),
re.VERBOSE)
self._comments = _CommentSpec(comment_prefixes or (), inline_comment_prefixes or ())
self._loaded_sources = []
self._strict = strict
self._allow_no_value = allow_no_value
self._empty_lines_in_values = empty_lines_in_values
Expand Down Expand Up @@ -757,6 +758,7 @@ def read(self, filenames, encoding=None):
if isinstance(filename, os.PathLike):
filename = os.fspath(filename)
read_ok.append(filename)
self._loaded_sources.append(read_ok)
return read_ok

def read_file(self, f, source=None):
Expand All @@ -773,6 +775,7 @@ def read_file(self, f, source=None):
except AttributeError:
source = '<???>'
self._read(f, source)
self._loaded_sources.append(source)

def read_string(self, string, source='<string>'):
"""Read configuration from a given string."""
Expand Down Expand Up @@ -809,6 +812,7 @@ def read_dict(self, dictionary, source='<dict>'):
raise DuplicateOptionError(section, key, source)
elements_added.add((section, key))
self.set(section, key, value)
self._loaded_sources.append(source)

def get(self, section, option, *, raw=False, vars=None, fallback=_UNSET):
"""Get an option value for a given section.
Expand Down Expand Up @@ -1048,6 +1052,38 @@ def __iter__(self):
# XXX does it break when underlying container state changed?
return itertools.chain((self.default_section,), self._sections.keys())

def __str__(self):
config_dict = {
section: dict(self.items(section, raw=True))
for section in self.sections()
}
return f"<ConfigParser: {config_dict}>"

def __repr__(self):
init_params = {
"defaults": self._defaults if self._defaults else None,
"dict_type": type(self._dict).__name__,
"allow_no_value": self._allow_no_value,
"delimiters": self._delimiters,
"strict": self._strict,
"default_section": self.default_section,
"interpolation": type(self._interpolation).__name__,
}
init_params = {k: v for k, v in init_params.items() if v is not None}
sections_count = len(self._sections)
state_summary = {
"loaded_sources": self._loaded_sources,
"sections_count": sections_count,
"sections": list(self._sections)[:5], # limit to 5 section names for readability
}

if sections_count > 5:
state_summary["sections_truncated"] = f"...and {sections_count - 5} more"

return (f"<{self.__class__.__name__}("
f"params={init_params}, "
f"state={state_summary})>")
Comment on lines +1083 to +1085
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use = f-strings:

return f"<{self.__class__.__name__}({params=}, {state=})>"

but this requires the variables to be named "params" and "state" instead.

Copy link
Contributor Author

@Agent-Hellboy Agent-Hellboy Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change if you want but init_params and state_summary are more descriptive for those who are reading the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmh, yes. Let's leave keep it that way (I'll let Jaraco have the final say)


def _read(self, fp, fpname):
"""Parse a sectioned configuration file.

Expand Down
44 changes: 44 additions & 0 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,50 @@ def test_set_nonstring_types(self):
self.assertRaises(TypeError, cf.set, "sect", 123, "invalid opt name!")
self.assertRaises(TypeError, cf.add_section, 123)

def test_str(self):
self.maxDiff = None
cf = self.config_class(allow_no_value=True, delimiters=('=',), strict=True)
cf.add_section("sect1")
cf.add_section("sect2")
cf.set("sect1", "option1", "foo")
cf.set("sect2", "option2", "bar")

expected_str = (
"<ConfigParser: {'sect1': {'option1': 'foo'}, 'sect2': {'option2': 'bar'}}>"
)
self.assertEqual(str(cf), expected_str)

def test_repr(self):
self.maxDiff = None
cf = self.config_class(allow_no_value=True, delimiters=('=',), strict=True)
cf.add_section("sect1")
cf.add_section("sect2")
cf.add_section("sect3")
cf.add_section("sect4")
cf.add_section("sect5")
cf.add_section("sect6")
cf.set("sect1", "option1", "foo")
cf.set("sect2", "option2", "bar")
cf.read_string("") # to trigger the loading of sources

dict_type = type(cf._dict).__name__
params = {
'dict_type': dict_type,
'allow_no_value': True,
'delimiters': ('=',),
'strict': True,
'default_section': 'DEFAULT',
'interpolation': 'BasicInterpolation',
}
state = {
'loaded_sources': ['<string>'],
'sections_count': 6,
'sections': ['sect1', 'sect2', 'sect3', 'sect4', 'sect5'],
'sections_truncated': '...and 1 more',
}
expected = f"<{type(cf).__name__}({params=}, {state=})>"
self.assertEqual(repr(cf), expected)

def test_add_section_default(self):
cf = self.newconfig()
self.assertRaises(ValueError, cf.add_section, self.default_section)
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,7 @@ Joel Rosdahl
Erik Rose
Mark Roseman
Josh Rosenberg
Prince Roshan
Jim Roskind
Brian Rosner
Ignacio Rossi
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Implement :meth:`~object.__str__` and :meth:`~object.__repr__`
for :class:`configparser.RawConfigParser` objects.
Loading