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

Ensure constant attribute is inherited #1017

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,10 @@ class hierarchy (see ParameterizedMetaclass).
self.precedence = precedence
self.default = default
self.doc = doc
self.constant = constant is True or readonly is True # readonly => constant
if constant is True or readonly is True: # readonly => constant
self.constant = True
else:
self.constant = constant
Copy link
Member

Choose a reason for hiding this comment

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

Ooh; good catch! I wonder if there were any other cases where it wouldn't inherit properly due to computation like this.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wonder if there were any other cases where it wouldn't inherit properly due to computation like this.

Good question 🙃 !

self.readonly = readonly
self._label = label
self._set_instantiate(instantiate)
Expand Down
25 changes: 20 additions & 5 deletions tests/testparameterizedobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -1276,21 +1276,36 @@ class B(A):
assert b.param.p.instantiate is True


def test_inheritance_constant_behavior():
def test_inheritance_readonly_behavior():
class A(param.Parameterized):
p = param.Parameter(readonly=True)

class B(A):
p = param.Parameter()


# Normally, param.Parameter(readonly=True) ends up with constant being
# True.
assert B.param.p.constant is False
assert B.param.p.readonly is True
assert B.param.p.constant is True

b = B()

assert b.param.p.readonly is True
assert b.param.p.constant is True


def test_inheritance_constant_behavior():
class A(param.Parameterized):
p = param.Parameter(constant=True)

class B(A):
p = param.Parameter()


assert B.param.p.constant is True

b = B()

assert b.param.p.constant is False
assert b.param.p.constant is True


def test_inheritance_set_Parameter_instantiate_constant_before_instantation():
Expand Down