Skip to content

Warn if .param accessed without super call #1071

Open

Description

@jlstevens

This is a mistake I am sure a lot of users can get confused by as shown in this screenshot. While this example uses panel, the principles are all based on param:

Image

Cell 3 doesn't update the Markdown where the mistake is that super() wasn't called in the __init__ of the parameterized class.

It would be very helpful if a warning could be issued if super() hasn't been called. Here I believe bind is watching the class parameter instead - if using the .param accessor warned when the object hasn't been properly initialized, that might be quite helpful.

Activity

  1. added
    type-feature
    TRIAGE
    on Aug 29, 2025
  2. jbednar commented on Aug 29, 2025

    @jbednar
    Member

    Thanks! It would be worth prototyping a warning like this and running it against a large codebase like Panel to see if it finds such issues (either real or false).

  3. maximlt commented on Sep 12, 2025

    @maximlt
    Member

    I would rather offer an alternative over re-implementing __init__ as it's indeed easy to forget calling super(), it's also always longer.

    • attrs has __attrs_post_init__
    • dataclasses has __post_init__
    • pydantic has model_post_init
  4. jbednar commented on Sep 12, 2025

    @jbednar
    Member

    @maximlt , are you suggesting recommending that most people don't provide an __init__, and instead steer them towards some post-init method we provide instead? If so, sounds good, but we have a lot of existing material suggesting that people override __init__, and some people will try it just from first principles, so having a warning seems useful regardless.

  5. maximlt commented on Nov 4, 2025

    @maximlt
    Member

    Yes, I'd like to steer users towards some post-init method we provide.

    As for the warning, I'm not sure how to disambiguate these two cases.

    class P(Parameterized):
        p = param.String()
    
       def __init__(self, **params):
           self.foo = 'bar'
    
    inst = P()
    class P(Parameterized):
        p = param.String()
    
       def __init__(self, **params):
           self.foo = 'bar'
           super().__init__(**params)
    
    inst = P()

    I'm sure setting an attribute (often private) before calling super().__init__ is a common pattern. I've easily found a few occurrences of that in a quick search in Panel and HoloViews, one example from HoloViews:

    class Link(param.Parameterized):
        """A Link defines ...
    
        """
    
        # Mapping from a source id to a Link instance
        registry = weakref.WeakKeyDictionary()
    
        # Mapping to define callbacks by backend and Link type.
        # e.g. Link._callbacks['bokeh'][Stream] = Callback
        _callbacks = defaultdict(dict)
    
        # Whether the link requires a target
        _requires_target = False
    
        def __init__(self, source, target=None, **params):
            if source is None:
                raise ValueError(f'{type(self).__name__} must define a source')
            if self._requires_target and target is None:
                raise ValueError(f'{type(self).__name__} must define a target.')
    
            # Source is stored as a weakref to allow it to be garbage collected
            self._source = None if source is None else weakref.ref(source)
            self._target = None if target is None else weakref.ref(target)
            super().__init__(**params)
            self.link()
  6. jbednar commented on Nov 4, 2025

    @jbednar
    Member

    I'm not sure how to disambiguate these two cases.

    That's true if we are warning by catching attribute access in the constructor, but I don't think attribute access is the thing to worry about anyway, because presumably we'd want to warn in this hypothetical situation as well, which does not involve attribute access:

    class P(Parameterized):
        p = param.String()
    
       def __init__(self, **params):
           os.system("echo 'in constructor'")
    
    inst = P()

    Here the constructor is failing to invoke the superclass constructor and we'd want to warn about that, right? Not sure how to check for that, but maybe somehow in the metaclass code.

  7. jlstevens commented on Nov 5, 2025

    @jlstevens
    ContributorAuthor

    Found something that might be relevant here:

    Adding this to ParmeterizedMetaclass:

        def __call__(cls, *args, **kwargs):
            obj = type.__call__(cls, *args, **kwargs)
            obj.__post_init__()
            return obj

    And this to Parameterized:

        def __post_init__(self):
            print('POST INIT')

    Seems to work in that 'POST INIT' is called whether a user supplied __init__ exists or not:

    >>> from param import Parameterized
    >>> class Foo(Parameterized):
    ...    pass
    ...
    >>> Foo()
    POST INIT
    Foo(name='Foo00002')
    

    With a badly formed user defined __init__:

    >>> from param import Parameterized
    >>> class Foo(Parameterized):
    ...    def __init__(self, *args, **kwargs):
    ...       print('BAD USER INIT')
    ...
    >>> Foo()
    BAD USER INIT
    POST INIT
    Foo(name='Foo')
    

    Seems like it should be possible to implement a __post_init__ that would check that super has been called appropriately...

  8. jbednar commented on Nov 5, 2025

    @jbednar
    Member

    That does sound good, assuming we set some attribute in the superclass __init__. Thanks!

  9. jlstevens commented on Nov 5, 2025

    @jlstevens
    ContributorAuthor

    I note that this POC doesn't immediately work with ParameterizedFunction but it is simple enough to skip the __post_init__ in that case. It might be possible to get working with ParameterizedFunction but I would need to think and experiment some more...

  10. removed
    TRIAGE
    on Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions