-
-
Notifications
You must be signed in to change notification settings - Fork 33.6k
gh-127011: Add __str__ and __repr__ to ConfigParser #127014
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
Closed
+73
−0
Closed
Changes from 12 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
b3ce3f3
gh-127011: Add __str__ and __repr__ to ConfigParser
Agent-Hellboy d603aa8
Add test
Agent-Hellboy 91db566
Remove unnecessary comments
Agent-Hellboy 7f3633d
Fix whitespaces
Agent-Hellboy 5cd1d1f
Revert unittest
Agent-Hellboy 5255025
Remove unnecessary changes
Agent-Hellboy dc71b39
change approach for repr
Agent-Hellboy c59d4fa
📜🤖 Added by blurb_it.
blurb-it[bot] d5e944f
Add test and fix parsing error of news
Agent-Hellboy 0883b52
Remove extra whitespace
Agent-Hellboy d76eec4
Track all config source for repr
Agent-Hellboy 433432b
Remove extra whitespaces
Agent-Hellboy acd31ae
Change str response
Agent-Hellboy 43b7ff3
Merge branch 'main' into fix-issue-127011
Agent-Hellboy 79e4892
Add my name Misc/ACKS
Agent-Hellboy 913c2a4
Fix alphabetical order in Misc/ACKS
Agent-Hellboy e60ffe4
gh-127011: Add __str__ and __repr__ to ConfigParser
Agent-Hellboy 0ed179a
Add test
Agent-Hellboy 726b152
Remove unnecessary comments
Agent-Hellboy 70fb9a5
Fix whitespaces
Agent-Hellboy 38809c9
Revert unittest
Agent-Hellboy 081c340
Remove unnecessary changes
Agent-Hellboy 50838d5
change approach for repr
Agent-Hellboy 2560eb9
📜🤖 Added by blurb_it.
blurb-it[bot] cb53ff2
Add test and fix parsing error of news
Agent-Hellboy 155b8ef
Remove extra whitespace
Agent-Hellboy 1a03c72
Track all config source for repr
Agent-Hellboy acfa969
Remove extra whitespaces
Agent-Hellboy 8415943
Change str response
Agent-Hellboy ee219a6
Add my name Misc/ACKS
Agent-Hellboy 63d58e0
Fix alphabetical order in Misc/ACKS
Agent-Hellboy 9b107e2
fix merge conflicts
Agent-Hellboy 50fdb2b
Merge branch 'main' into fix-issue-127011
Agent-Hellboy 6427a0d
fix test
Agent-Hellboy d547497
Merge branch 'fix-issue-127011' of github.com:Agent-Hellboy/cpython i…
Agent-Hellboy 3817877
Merge branch 'python:main' into fix-issue-127011
Agent-Hellboy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2024-11-23-13-34-55.gh-issue-127011.dT88uF.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The ``__str__`` and ``__repr__`` methods have been added to the :class:`configparser.RawConfigParser` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this implementation leaves too much risk for confusion. With this, you can have
str(config)render as"{'a': {'b': XYZ}}", but haveconfig["a"]["b"]evaluate to something other thanXYZ(or even raise an exception). That seems rather undesirable, especially for something that's intended to be used for debugging/logging.Personally, I don't see the benefit in defining
__str__. I think it would be better to let uses pick the dict-like representation they want and the tradeoffs to go with. For example, users that want an exception-safe dict representation without interpolations can get one withConfigParser._sections, and users that a want dict representation that matches the runtime indexing behavior (but which may raise exceptions) can use{k: dict(v) for k, v in config.items()}.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @brianschubert
Thank you for the feedback. Could you please share an example to help clarify your concerns?
Based on your input, I plan to make it fallback to repr for string representations and add a new API, to_dict, which could be useful for scenarios like dumping the configuration into JSON and several other use cases.
I’m waiting for others to agree with you before proceeding.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leave a new API to another PR, let's just focus on
__str__and__repr__here. Regarding what__str__should do, I suggested in the issue that it display<ConfigParser: {json representation ...}>to mitigate confusion with dictionaries. I think that (or a variation of it) is the best approach here.IMO, recommending
_sectionsis a terrible idea for debugging, and we definitely should not write off a feature because it exists. Namely:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ZeroIntensity , should I create an issue for adding to_dict ?