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

Validate dependencies in constructor #813

Merged
merged 2 commits into from Aug 2, 2023

Conversation

philippjfr
Copy link
Member

@philippjfr philippjfr commented Aug 2, 2023

The dynamic resolution of dependencies loosened the validation for dependencies. However this was only intended to allow dynamic dependency resolution of the leafs or branches of nested dependencies, i.e. the root of a nested dependency has to be declared. This adds validation to ensure that the root dependency can be resolved and adds an error otherwise.

Copy link
Member

@maximlt maximlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the error is not raised at class creation time but on instantiation and only when watch=True, it's a big improvement anyway.

@philippjfr
Copy link
Member Author

It cannot be done at class creation time because the class cannot know what happens in the constructor before super is called.

@philippjfr philippjfr merged commit 44c2c95 into main Aug 2, 2023
10 checks passed
@philippjfr philippjfr deleted the validate_non_existent_dependency branch August 2, 2023 15:26
@maximlt
Copy link
Member

maximlt commented Aug 3, 2023

Is it because we allow people depending on objects that aren't declared as Parameter but are set in the __init__? If so, I'd be happy to see a warning in cases like that, it's a bit of an anti-pattern isn't it?

A recent change made this fail when watch=False:

import param

class P(param.Parameterized):
    x = param.Parameter()
    
    @param.depends('y')
    def debug(self): pass

Traceback:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[42], line 3
      1 import param
----> 3 class P(param.Parameterized):
      4     x = param.Parameter()
      6     @param.depends('y')

File ~/dev/param/param/parameterized.py:3097, in ParameterizedMetaclass.__init__(mcs, name, bases, dict_)
   3094 on_init = dinfo.get('on_init', False)
   3095 minfo = MInfo(cls=mcs, inst=None, name=name,
   3096               method=method)
-> 3097 deps, dynamic_deps = _params_depended_on(minfo, dynamic=False)
   3098 if watch:
   3099     _watch.append((name, watch == 'queued', on_init, deps, dynamic_deps))

File ~/dev/param/param/parameterized.py:649, in _params_depended_on(minfo, dynamic, intermediate)
    647 dinfo = getattr(minfo.method, "_dinfo", {})
    648 for d in dinfo.get('dependencies', list(minfo.cls.param)):
--> 649     ddeps, ddynamic_deps = (minfo.cls if minfo.inst is None else minfo.inst).param._spec_to_obj(d, dynamic, intermediate)
    650     dynamic_deps += ddynamic_deps
    651     for dep in ddeps:

File ~/dev/param/param/parameterized.py:2661, in Parameters._spec_to_obj(self_, spec, dynamic, intermediate)
   2659     return [], [] if intermediate == 'only' else [DInfo(spec=spec)]
   2660 else:
-> 2661     raise AttributeError(f"Attribute {attr!r} could not be resolved on {src}.")
   2663 if obj is None or not intermediate:
   2664     return [info], []

AttributeError: Attribute 'y' could not be resolved on <class '__main__.P'>.

@philippjfr
Copy link
Member Author

I'm a little on the fence on it and likely have to revert your change. I've seen many people use the pattern and I have also used it on occasion because declaring parameters is sometimes awkward, e.g. often I use parameters for public API declarations but internally I want to be able to set up dependencies on static attributes and not have to set up parameter declarations.

@maximlt
Copy link
Member

maximlt commented Aug 3, 2023

I use parameters for public API declarations but internally I want to be able to set up dependencies on static attributes and not have to set up parameter declarations.

Yes that's a weakness of Param that has no way to tell whether a parameter is an input or some state you compute later on. Param has @output but I haven't seen that used much. dataclasses.field has an init that is True by default and that you can use to indicate whether the generated init should contain this field, I think Param should have something similar.

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.

None yet

2 participants