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

Inherited default is not validated #714

Closed
maximlt opened this issue Mar 14, 2023 · 4 comments · Fixed by #820
Closed

Inherited default is not validated #714

maximlt opened this issue Mar 14, 2023 · 4 comments · Fixed by #820
Assignees
Labels
type-bug Bug report
Milestone

Comments

@maximlt
Copy link
Member

maximlt commented Mar 14, 2023

The value of a Parameter attribute - like default, doc, etc. - is inherited in a hierarchy of Parameterized classes. In practice, the inheritance only applies when the default attribute value is None.

In the example below, B is a subclass of A and the user intent seems to be to override the definitions of p1 and p2, from String and String to Action to Number, respectively. The result of that operation is that:

  • Because the default value of the default attribute of Action is None, B.p1.param.default will inherit from A.p1.param.default which is '1'. The value of p1 is then set based on that new default, leading to b.p1 == '1'. This is an invalid value for a Number.
  • Because the default value of the default attribute of Action is not None but 0.0, it is used directly to set B.p1.param.default, leading to b.p1 == 0.0.
import param

class A(param.Parameterized):
    p1 = param.String(default='1')
    p2 = param.String(default='1')

class B(A):
    p1 = param.Action()
    p2 = param.Number()

b = B()

assert b.p1 == '1'  # passes
assert b.p2 == 0.0  # passes

The focus of this issue is on the default attribute as it can be used to set the actual Parameter value, if the user does not provide a default when instantiating the class. But this issue is more general, the Parameters of a subclass can inherit other invalid attributes without any error/warning.

This discussion is linked to the proposal of enforcing an "is-a" relationship (#97 (comment)) between Parameters in a class hierarchy.

A pragmatic approach to this issue might be to run the Parameter validation code (Parameter._validate) after the whole inheritance procedure is executed, and emit a warning if that fails. As to avoid the issue above with p1, the author of B should define a default value for the overriding p1.

@maximlt maximlt added the type-bug Bug report label Mar 14, 2023
@maximlt maximlt added this to the 2.0 milestone Apr 5, 2023
@jbednar
Copy link
Member

jbednar commented May 12, 2023

I agree that enforcing is-a would address this, but it also seems appropriate to run _validate on whatever the result of inheritance is.

@jbednar
Copy link
Member

jbednar commented May 12, 2023

Note that the behavior of main is now more reasonable (no longer type dependent), though still not validating as it should:

image

@jbednar
Copy link
Member

jbednar commented Aug 3, 2023

Fixed by #812.

@jbednar jbednar closed this as completed Aug 3, 2023
@maximlt maximlt reopened this Aug 3, 2023
@maximlt
Copy link
Member Author

maximlt commented Aug 3, 2023

@jbednar this is not completely fixed. #812 only added validation of default when a Parameter is not a subclass of it parent Parameter. So there's no validation in that kind of more common case:

import param

class A(param.Parameterized):
    x = param.Number(5, bounds=(3, 10))

class B(A):
    x = param.Number(0)

b = B()
b.x
# 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug Bug report
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants