# Customizable repr()

**URL:** <https://discuss.python.org/t/customizable-repr/55690>\
**Category:** Python Help\
**Created:** [June 13, 2024, 3:25pm UTC](https://discuss.python.org/t/customizable-repr/55690 "2024-06-13T15:25:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![15r10nk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/15r10nk/32/12183_2.png) [@15r10nk](https://discuss.python.org/u/15r10nk)\
**Post date:** [June 13, 2024, 3:25pm UTC](https://discuss.python.org/t/customizable-repr/55690/1 "2024-06-13T15:25:57Z")

</div>

hi, I’m the author of [inline-snapshot](https://github.com/15r10nk/inline-snapshot/) which uses repr() to create the source representation from the value you want to snapshot.

But I have a problem with the way how repr() works.

```python
>>> from enum import Enum
>>> E=Enum("E",["a","b"])
>>> repr(E.a)
'<E.a: 1>'
>>> repr([E.a])
'[<E.a: 1>]'
>>> repr(int)
"<class 'int'>"

```

There are some types which do not return a valid python representation, but could.  
I know that it is not possible to change cpython just for my use cases, but I search for a way to customize it.

My current solution looks like this:

```python
from functools import singledispatch
from unittest import mock

real_repr=repr

@singledispatch
def code_repr_dispatch(v):
    return real_repr(v)

def code_repr(obj):
    with mock.patch("builtins.repr", code_repr):
        return code_repr_dispatch(obj)

@code_repr_dispatch.register
def _(v: Enum):
    return f"{type(v). __qualname__ }.{v.name}"

```

The problem is that I have to re-implement all possible container types to make it work recursively like

```python
@code_repr_dispatch.register
def _(v: list):
    # repr calls actually code_repr
    return "[" + ", ".join(map(repr, v)) + "]" 

```

This is because code\_repr works recursive if it is called with `repr()` but not if it is called with `f"{value!r}" or `PyObject\_Repr` in C.

My question now is: Does anyone know a way to make this work for the other two cases?

One of my Ideas is to change python and add a second optional argument to `repr(obj, handler)` . The `handler` would be called called before `obj. __repr__ ` and can be used to overwrite the default behavior for a object. The implementation would be similar to my `code_repr` approach but on `PyObject_Repr` level. It would require a global thread-local variable to store the handler and to make recursive repr(obj) calls work. I don’t know if this idea would work out or lead to other issues.

A solution would not only be useful for inline-snapshot but also for reprlib which has the same problems with recursive calls to custom types.

My hope is to find a way to customize only the types which need customization and not all the container types. Maybe someone has an idea.

---

<div class="post-metadata">

**Author:** ![blhsing](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/blhsing/32/25812_2.png) [@blhsing](https://discuss.python.org/u/blhsing)\
**Post date:** [June 14, 2024, 3:11am UTC](https://discuss.python.org/t/customizable-repr/55690/2 "2024-06-14T03:11:28Z")

</div>

To achieve what you want without making changes to CPython I would take an entirely different approach of building a dedicated parser that parses representations of all known types (i.e. a parser that understands `'<...>'` reprs, with a grammar slightly modified from Python’s) into an AST, from which a representation that can be evaluated by Python can be generated by applying custom repr logics to types with bad reprs.

---

<div class="post-metadata">

**Author:** ![skirpichev](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/skirpichev/32/10996_2.png) [@skirpichev](https://discuss.python.org/u/skirpichev)\
**Post date:** [June 14, 2024, 6:21am UTC](https://discuss.python.org/t/customizable-repr/55690/3 "2024-06-14T06:21:09Z")

</div>

> [@blhsing](#):
>
> (i.e. a parser that understands `'<...>'` reprs

According to [docs](https://docs.python.org/3.14/reference/datamodel.html#object. __repr__ ), this is just “some useful description”, not something that could give you precise instructions to build the object.

---

<div class="post-metadata">

**Author:** ![blhsing](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/blhsing/32/25812_2.png) [@blhsing](https://discuss.python.org/u/blhsing)\
**Post date:** [June 14, 2024, 6:37am UTC](https://discuss.python.org/t/customizable-repr/55690/4 "2024-06-14T06:37:02Z")

</div>

With the repr string alone, certainly not, but the OP is trying to make an improved `repr` function here, and the reprs of known types all give us enough details to identify objects being represented at the time of the repr calls. These details include either class names or object ids, with which we can obtain relevant objects by looking up names in local/closure/global namespaces, by evaluating the names, or by converting the identities to objects with `ctypes.cast`. We can then extract from these objects further details necessary to produce new reprs with enough information to rebuild the objects.

---

<div class="post-metadata">

**Author:** ![blhsing](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/blhsing/32/25812_2.png) [@blhsing](https://discuss.python.org/u/blhsing)\
**Post date:** [June 14, 2024, 9:07am UTC](https://discuss.python.org/t/customizable-repr/55690/5 "2024-06-14T09:07:50Z")

</div>

Another entirely different approach that should work well for the purpose of your project:

Instead of trying to produce an evaluatable representation of an object so that it can be used as the expected value of an assertion in your project, take the repr of the object as-is as a string, and compare it with the repr of the target value:

```python
from enum import Enum

class Snapshot:
    def __init__ (self, snapshot):
        self.snapshot = snapshot

    def __eq__ (self, value):
        return repr(value) == self.snapshot

E = Enum("E", ["a", "b"])

assert E.a == Snapshot('<E.a: 1>')
assert [E.a] == Snapshot('[<E.a: 1>]')
assert int == Snapshot("<class 'int'>")

```

You can add additional wildcard logics to allow matching reprs with object ids.

If every detail of an object is important, consider using a proper serializer such as `pickle.dumps` instead.

---

<div class="post-metadata">

**Author:** ![15r10nk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/15r10nk/32/12183_2.png) [@15r10nk](https://discuss.python.org/u/15r10nk)\
**Post date:** [June 15, 2024, 6:23pm UTC](https://discuss.python.org/t/customizable-repr/55690/6 "2024-06-15T18:23:39Z")

</div>

Thank you for your tips Ben

> [@blhsing](#):
>
> take the repr of the object as-is as a string, and compare it with the repr of the target value

I convert objects which have non parsable representations into `assert [E.a] == snapshot([HasRepr("<E.a: 1>")])` as a kind of fallback in my current feature branch.

Doing it for the whole expression is not possible because inline-snapshot supports also `<=` `in` and `snapshot[key]`. I need the python objects in these cases.

> [@blhsing](#):
>
> building a dedicated parser that parses representations of all known types (i.e. a parser that understands `'<...>'` reprs, with a grammar slightly modified from Python’s) into an AST

This solution was also proposed in the original issue, but the problem is that two types might produce two repr strings which are indistinguishable from each other but need to be converted to different code.

Things like pandas DataFrames have completely different repr without \<\>

Going down this path could lead to a lot of problems later.

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [June 16, 2024, 2:02am UTC](https://discuss.python.org/t/customizable-repr/55690/7 "2024-06-16T02:02:33Z")

</div>

I _think_ the underlying idea here is really that the algorithm for traversing an object graph (and detecting cycles) doesn’t seem to be factored out and exposed in the standard library. The built-in `repr` uses this, and a custom `repr` could basically just use the same algorithm but with a different method on each node. The same algorithm, fundamentally, is also needed by `copy.deepcopy`, as far as I’m aware.
