Added ability to add a method __rollbar_repr__() to change how an object is serialized.#479
Merged
danielmorell merged 4 commits intomasterfrom Mar 21, 2025
Merged
Conversation
…s serialized. Issue #67
matux
requested changes
Mar 17, 2025
matux
left a comment
There was a problem hiding this comment.
Looks good to me, let's return a proper error message with the type error, tho.
rollbar/lib/transform.py
Outdated
| if hasattr(obj, '__rollbar_repr__'): | ||
| r = obj.__rollbar_repr__() | ||
| if not isinstance(r, str): | ||
| raise TypeError(...) |
There was a problem hiding this comment.
We should probably return an error message like __repr__ does: __repr__ returned non-string (type X).
Collaborator
Author
There was a problem hiding this comment.
🤦 Yep that would have been smart. That has been fixed now.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description of the change
This PR addresses #67. It adds a check during traversal, serialization, and shortning that looks to see if a
__rollbar_repr__()method exists on an object. If it does exist, that method is called and the returned result is used instead of a the standard reprlib approach.I have included the check for the
__rollbar_repr__()method in two places:SerializableTransform. This is the tranform that handles serialization, and so it should probably be included here to prevent the use of reprlib by default.ShortenerTransform. This transform will also serialize an object using reprlib, so I have included the logic here as well.Type of change
Related issues
Checklists
Development
Code review