-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
bpo-28850: Fix PrettyPrinter.format overrides being ignored for contents of small containers #22120
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
Conversation
…the buggy behaviour
…t part of the PrettyPrinter class (no functional change)
…on container contents
Misc/NEWS.d/next/Library/2020-09-06-21-55-44.bpo-28850.HJNggD.rst
Outdated
Show resolved
Hide resolved
def saferepr(object): | ||
"""Version of repr() which can handle recursive data structures.""" | ||
return _safe_repr(object, {}, None, 0, True)[0] | ||
return PrettyPrinter()._safe_repr(object, {}, None, 0)[0] | ||
|
||
def isreadable(object): | ||
"""Determine if saferepr(object) is readable by eval().""" | ||
return _safe_repr(object, {}, None, 0, True)[1] | ||
return PrettyPrinter()._safe_repr(object, {}, None, 0)[1] | ||
|
||
def isrecursive(object): | ||
"""Determine if object requires a recursive representation.""" | ||
return _safe_repr(object, {}, None, 0, True)[2] | ||
return PrettyPrinter()._safe_repr(object, {}, None, 0)[2] |
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.
It seems rather unfortunate that a PrettyPrinter()
instance is now created here for every function call.
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 know. We could:
-
leave _safe_repr where it was and pass in a function that it should call when it needs to recurse (default is it calls itself, but PrettyPrinter passes in self.format)
-
move _safe_repr to a stateless superclass of PrettyPrinter, and instantiate a module level _defaultPrettyPrinter instance of this superclass for use in these functions.
Any other options?
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.
IMO this is fine, and better than the alternatives.
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.
LGTM
… small containers (pythonGH-22120)
This PR fixes a bug where subclassing PrettyPrinter and overriding format doesn't work for container contents (see issue 28850 for details).
The solution suggested here is to make _safe_repr a method of PrettyPrinter, and make it call self.format() when it needs to recurse on container contents.
The diff looks substantial, but that's mostly due to the indentation of _safe_repr code. To make it easier to review, I split it into several commits, where the indentation is done in one commit as a noop change. The other commits have much smaller diffs.
https://bugs.python.org/issue28850