Skip to content

Added ability to add a method __rollbar_repr__() to change how an object is serialized.#479

Merged
danielmorell merged 4 commits intomasterfrom
task/issue-67-add-custom-repr
Mar 21, 2025
Merged

Added ability to add a method __rollbar_repr__() to change how an object is serialized.#479
danielmorell merged 4 commits intomasterfrom
task/issue-67-add-custom-repr

Conversation

@danielmorell
Copy link
Collaborator

@danielmorell danielmorell commented Mar 7, 2025

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:

  • The SerializableTransform. This is the tranform that handles serialization, and so it should probably be included here to prevent the use of reprlib by default.
  • The ShortenerTransform. This transform will also serialize an object using reprlib, so I have included the logic here as well.

Type of change

  • New feature (non-breaking change that adds functionality)

Related issues

Checklists

Development

  • Lint rules pass locally
  • The code changed/added as part of this pull request has been covered with tests
  • All tests related to the changed code pass in development

Code review

  • This pull request has a descriptive title and information useful to a reviewer. There may be a screenshot or screencast attached
  • "Ready for review" label attached to the PR and reviewers assigned
  • Issue from task tracker has a link to this pull request
  • Changes have been reviewed by at least one other engineer

@danielmorell danielmorell added this to the v1.3.0 milestone Mar 7, 2025
@linear
Copy link

linear bot commented Mar 7, 2025

@danielmorell danielmorell marked this pull request as ready for review March 7, 2025 16:42
@danielmorell danielmorell requested a review from brianr March 7, 2025 16:42
@brianr brianr requested review from matux and removed request for brianr March 10, 2025 18:18
Copy link

@matux matux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, let's return a proper error message with the type error, tho.

if hasattr(obj, '__rollbar_repr__'):
r = obj.__rollbar_repr__()
if not isinstance(r, str):
raise TypeError(...)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably return an error message like __repr__ does: __repr__ returned non-string (type X).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 Yep that would have been smart. That has been fixed now.

@danielmorell danielmorell requested a review from matux March 21, 2025 16:30
Copy link

@matux matux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you, @danielmorell!

@danielmorell danielmorell merged commit 13574e7 into master Mar 21, 2025
104 checks passed
@danielmorell danielmorell deleted the task/issue-67-add-custom-repr branch March 21, 2025 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to customize how objects are repr()'d

2 participants