Skip to content
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

Feature Request [Quality of life]: Remove memory addresses/ids of objects from Op versioning #3068

Open
TeoZosa opened this issue Nov 25, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@TeoZosa
Copy link

TeoZosa commented Nov 25, 2024

Background

Right now, memory addresses in the repr of instantiated objects cause spurious diffs when versioning Ops in Weave. This makes it difficult to track meaningful changes between runs.

Image

Image

Workaround

I use the following utility method to remove at <memory_address> from reprs to eliminate those spurious diffs:

import re
def _strip_memory_address_from_repr(obj: object) -> None:
    original_repr = obj.__class__.__repr__
    obj.__class__.__repr__ = lambda self: re.sub(r" at 0x[0-9a-fA-F]+", "", original_repr(self))  # type: ignore[method-assign]

Question

Is there something more foundational that can be done to cover this issue globally?

Code?

Digging around the codebase, are these the appropriate places?

def stringify(obj: Any, limit: int = MAX_STR_LEN) -> str:
"""This is a fallback for objects that we don't have a better way to serialize."""
rep = None
try:
rep = repr(obj)
except Exception:
try:
rep = str(obj)
except Exception:
rep = f"<{type(obj).__name__}: {id(obj)}>"
if isinstance(rep, str):
if len(rep) > limit:
rep = rep[: limit - 3] + "..."
return rep

def fallback_encode(obj: Any) -> Any:
rep = None
try:
rep = repr(obj)
except Exception:
try:
rep = str(obj)
except Exception:
rep = f"<{type(obj).__name__}: {id(obj)}>"
if isinstance(rep, str):
if len(rep) > MAX_STR_LEN:
rep = rep[:MAX_STR_LEN] + "..."
return rep

But I guess that wouldn't cover the case of a bad repr in which case an object's id could create a spurious diff as well, ex.

Image

@TeoZosa TeoZosa changed the title Feature Request [Quality of life]: Remove IDs of objects from Op versioning Feature Request [Quality of life]: Remove memory addresses/ids of objects from Op versioning Nov 25, 2024
@gtarpenning
Copy link
Member

@TeoZosa Thanks for the writeup, this is a great feature request/bug. We had already noted this internally as low-hanging fruit to improve the comparison page. As you have noted, these memory addresses are generated pretty deep in our code capture handling, which is rather delicate.

For now, looks like you have a decent workaround, but we will be working on this in the near future.

@gtarpenning gtarpenning added the enhancement New feature or request label Nov 25, 2024
@TeoZosa
Copy link
Author

TeoZosa commented Dec 3, 2024

@gtarpenning Got it, thanks for getting back to this so fast. Will keep my workaround for the time being. The team has been putting out some pretty major features recently and this one is definitely not as urgent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants