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

Avoid recursive repr issues #499

Merged
merged 3 commits into from
Jul 2, 2021
Merged

Avoid recursive repr issues #499

merged 3 commits into from
Jul 2, 2021

Conversation

jbednar
Copy link
Member

@jbednar jbednar commented Jun 30, 2021

Addresses #209 and #396 to avoid infinite recursion when printing a Parameterized object that contains an indirect reference to itself.

First tried using reprlib.recursive_repr, which seemed to work for repr itself, but did not address pprint, because it did not allow the decorated method to have any extra arguments. Luckily the implementation is straightforward, so I copied it from https://github.com/python/cpython/blob/3.2/Lib/reprlib.py and changed it to handle args and kwargs. I did have to change from _thread import get_ident to avoid private API, but it seems the same as from threading import get_ident, which appears to work the same.

I haven't run the test suite yet, but it addresses #209:

import param
class A(param.Parameterized):
    
    b = param.Parameter()
    
class B(param.Parameterized):
    
    a = param.Parameter()
    
a = A()
b = B(a=a)
a.b = b
a.pprint()

'A(b=main.B(a=...))`

and #396:

import param

class InputDataComponent(param.Parameterized):
    update_data = param.Action()
    # download_data = param.Action()

    def __init__(self, **params):
        super().__init__(**params)

        self.update_data = self._update_data
        # self.download_data = self._download_data

    def _update_data(self, event):
        raise NotImplementedError()

    def _download_data(self, event):
        raise NotImplementedError()

repr(InputDataComponent())

"InputDataComponent(name='InputDataComponent00004', update_data=<bound method InputDataComponent._update_data of ...>)"

@jbednar jbednar requested a review from philippjfr June 30, 2021 18:53
@philippjfr
Copy link
Member

Finally! Thanks for tackling this one.

Copy link
Member

@philippjfr philippjfr 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, and nice solution.

param/parameterized.py Outdated Show resolved Hide resolved
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.

2 participants