AustinScola / seligimus

A mono-library.
MIT License
1 stars 0 forks source link

Handle recursive objects in standard repr #189

Closed AustinScola closed 3 years ago

AustinScola commented 3 years ago

Add the ability to handle recursive objects to seligimus.python.decorators.standard_representation.standard_representation.

For example:

class Foo():
    __init__(self, bar: Optional[Bar] = None):
        bar: Optional[Bar] = bar

    @standard_representation
    def __repr__(self) -> str: pass

class Bar():
    __init__(self, foo: Optional[Foo] = None):
        foo: Optional[Foo] = foo

    @standard_representation
    def __repr__(self) -> str: pass

foo: Foo = Foo()
bar: Bar = Bar(foo)
foo.bar = bar

assert repr(foo) == 'Foo(bar=Bar(<..>))'

I like the idea of using some sort of "dot" syntax to refer to other relative objects.

It would be nice to have a way to output actual runnable code for the repr at some point too. This would require a recursive constructor, or a "reference constructor" to enable you to make any sort of graph structure. For example, if you had a class Foo with two attributes, bar and baz and both attributes where assigned to the same object, then you would want your repr to tell you that they are the same object. Maybe the reference constructor could use a dictionary specification of the self-referencing structure?