Skip to content

Add __str__ and __repr__ to ConfigParser for Better Debugging and Unit Testing #127011

Open
@Agent-Hellboy

Description

@Agent-Hellboy

Feature or enhancement

Proposal:

The ConfigParser class supports dictionary-like operations, including the contains protocol for checking section existence using the in operator. However, it lacks str and repr methods, essential for providing human-readable and developer-friendly representations of the configuration data. Adding these methods would enhance its usability, particularly for debugging, logging, and unit testing, by offering clear insights into its internal state.

current behavior:

str_resp: <configparser.ConfigParser object at 0x7f57e7cc5160>
repr_resp: <configparser.ConfigParser object at 0x7f57e7cc5160>

suggested behavior:

str_resp: {'GERRIT': {'api_key': 'val'}}
repr_resp: <ConfigParser: {'GERRIT': {'api_key': 'val'}}>

I haven’t started a thread on Python Discourse as this is a straightforward feature request requiring a simple accept or reject decision. I'd appreciate the clarification on whether there was a specific design reason for not including str and repr. If approved, I will raise a PR.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to the previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions