You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
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).
@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.
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:
classLink(param.Parameterized):
"""A Link defines ... """# Mapping from a source id to a Link instanceregistry=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=Falsedef__init__(self, source, target=None, **params):
ifsourceisNone:
raiseValueError(f'{type(self).__name__} must define a source')
ifself._requires_targetandtargetisNone:
raiseValueError(f'{type(self).__name__} must define a target.')
# Source is stored as a weakref to allow it to be garbage collectedself._source=NoneifsourceisNoneelseweakref.ref(source)
self._target=NoneiftargetisNoneelseweakref.ref(target)
super().__init__(**params)
self.link()
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:
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.
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...
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...
Activity
jbednar commentedon Aug 29, 2025
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).
maximlt commentedon Sep 12, 2025
I would rather offer an alternative over re-implementing
__init__as it's indeed easy to forget callingsuper(), it's also always longer.attrshas__attrs_post_init__dataclasseshas__post_init__pydantichasmodel_post_initjbednar commentedon Sep 12, 2025
@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.maximlt commentedon Nov 4, 2025
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.
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:jbednar commentedon Nov 4, 2025
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:
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.
jlstevens commentedon Nov 5, 2025
Found something that might be relevant here:
Adding this to
ParmeterizedMetaclass:And this to
Parameterized:Seems to work in that 'POST INIT' is called whether a user supplied
__init__exists or not:With a badly formed user defined
__init__:Seems like it should be possible to implement a
__post_init__that would check thatsuperhas been called appropriately...jbednar commentedon Nov 5, 2025
That does sound good, assuming we set some attribute in the superclass
__init__. Thanks!jlstevens commentedon Nov 5, 2025
I note that this POC doesn't immediately work with
ParameterizedFunctionbut it is simple enough to skip the__post_init__in that case. It might be possible to get working withParameterizedFunctionbut I would need to think and experiment some more...