-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Subclassing PrettyPrinter.format doesn't work in all cases #132855
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
Comments
For a little background about impact, I ran into this trying to do something like: class JSONPrettyPrinter(pprint.PrettyPrinter):
def __init__(self, json_encoder=None, *args, **kwargs):
super().__init__(*args, **kwargs)
self.json_encoder = json_encoder or json.JSONEncoder
def format(self, obj, context, maxlevels, level):
if isinstance(obj, (list, tuple, dict)):
return super().format(obj, context, maxlevels, level)
if (
isinstance(obj, (str, int, float))
or obj is None
or obj is True
or obj is False
):
return self.json_encoder.encode(obj), True, False
return self.format(self.json_encoder.default(obj), context, maxlevels, level) This gives a bogus result when formatting something like |
Would you like to submit a PR? |
Hi @ZeroIntensity , I can add a PR if she is not interested Line 184 in fd0f5d0
and we can skip primitive type like Line 651 in 5ea9010
we can call specific dispatcher here but i don't know if it's a thoughtful and reasonable choice Line 647 in 5ea9010
|
This comment was marked as spam.
This comment was marked as spam.
@Agent-Hellboy Go ahead! I'm not going to any time soon. |
@gsnedders added a PR, please review |
Bug report
Bug description:
See also the earlier fixed #73036. This appears to have only been a partial fix, or it has regressed.
This outputs:
Every instance of
<
in that output is a bug.CPython versions tested on:
3.13
Operating systems tested on:
macOS
Linked PRs
The text was updated successfully, but these errors were encountered: