-
Notifications
You must be signed in to change notification settings - Fork 99
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
Update handling for Pydantic objects in mf_pformat
#1666
Conversation
remaining_line_width=remaining_line_width, | ||
) | ||
# For Pydantic-like objects with a `dict` method that returns field keys / values. | ||
dict_method = getattr(obj, "dict", None) |
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.
@plypaul isn't there a chance that it breaks with pydantic v2 where dict
was deprecated in favor of model_dump
?
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 actually still works, though now I see the deprecation warning. Updated the case handling though.
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.
Also to provide context on these formatting methods - in case there's a problem formatting, it falls back to the standard pretty printer or str(...)
.
This PR updates the handling for Pydantic objects in
mf_pformat
to check if thedict()
methods exists instead of theisinstance(BaseModel)
check. TheBaseModel
is actually from thedsi_shim
and in some cases with different Pydantic versions, it's possible that the check does not pass. Since it's not foolproof, the formatter falls back topprint
if there is an error.