-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
Add __str__ and __repr__ to ConfigParser for Better Debugging and Unit Testing #127011
Comments
I think it makes sense to add a Go ahead and send a PR, and we can decide whether or not to go with it from there :) |
I have used default values |
The main issue to me is that a ConfigParser can have 100s of entries. Of course, the same is true of dicts ans other containers. And unittest special cases susch builtins to condense error messages. |
Why not leave it as is? Users who want to print the output are likely dealing with small configuration files anyway. Alternatively, if necessary, I could truncate the dictionary representation with ... for larger files. |
I don't think import configparser
config = configparser.ConfigParser()
config.read_string("""
[foo]
a = 1
""")
>>> config.get("foo")
Traceback (most recent call last):
File "<python-input-21>", line 1, in <module>
config.get("foo")
~~~~~~~~~~^^^^^^^
TypeError: RawConfigParser.get() missing 1 required positional argument: 'option' If you what you want is a convenient shorthand for debugging, then
This gives you essentially the same result as the proposed |
The example shared highlights a difference between ConfigParser and dict, but it doesn’t invalidate the idea of enhancing ConfigParser for better debugging. I’m not suggesting that ConfigParser should fully mimic a dict—if that were the goal, it would have been inherited from Mapping. The comparison to dict was to emphasize usability and debugging convenience, not to imply complete parity. Relying on private APIs like _sections for debugging isn’t ideal. While people can and do use private attributes occasionally, providing a Pythonic approach like improving str or repr would offer a clean, public way to inspect configurations. This doesn’t conflict with the existing API but complements it, enhancing usability for developers without changing its core behavior. |
I feel like if the parameters are included in the Unfortunately, the most important summary information about the state isn't part of the constructor. For example, there's no indication if the configparser has loaded a file or files (or which ones), or how many sections or keys it might have. That information might be more useful to the user. You may also want to consider providing other state, like |
Hi @jaraco Thanks for the feedback , I have added a new commit what do you think of
|
IMO, this kind of style you suggested for the repr would work fine for the str:
It doesn't break any conventions since it's |
Kind of, but from the Python docs and this StackOverflow post, it says repr should be used to get the object's representation, and str is for the end user. I guess people like us are going to use str only. |
Yes, that follows that, doesn't it? Users will see the |
Yes, I picked you only after your first comment, but after Jaraco’s comments, I thought repr should give every piece of information (object state) possible about the object, and str a dict representation of the object. We can use the parser object as a dict, so we will give a dict to complement its behavior. |
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:
suggested behavior:
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
The text was updated successfully, but these errors were encountered: