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

Unexpected behaviour of param.edit_constant() context manager #1014

Open
dinya opened this issue Feb 10, 2025 · 4 comments · May be fixed by #1015
Open

Unexpected behaviour of param.edit_constant() context manager #1014

dinya opened this issue Feb 10, 2025 · 4 comments · May be fixed by #1015
Labels
type-bug Bug report

Comments

@dinya
Copy link

dinya commented Feb 10, 2025

From param.edit_constant() docstring:

Temporarily set parameters on Parameterized object to constant=False to allow editing them.

Should "temporarily" mean "set editable inside context manager and unset outside"?

If so the following behaviour is unexpected.

import param

class A(param.Parameterized):
    val = param.Integer(1, constant=True)

a = A()

#print(a.param.val.constant, a.param.val.readonly)

with param.edit_constant(a):
    a.val = 6

print(a.param.val.constant, a.param.val.readonly)

# "TypeError: Constant parameter 'val' cannot be modified" is expected to be raised:
a.val = 7

... but nothing is raised and it prints:

False False

But if I change my code slightly (uncomment first print)

import param

class A(param.Parameterized):
    val = param.Integer(1, constant=True)

a = A()

print(a.param.val.constant, a.param.val.readonly)
# or just call a.param.val

with param.edit_constant(a):
    a.val = 6

print(a.param.val.constant, a.param.val.readonly)

# "TypeError: Constant parameter 'val' cannot be modified" is expected to be raised:
a.val = 7

then it prints

True False
True False

and TypeError: Constant parameter 'val' cannot be modified is raised as expected.

It seems that the param.edit_constant() context manager doesn't restore the state if a.param.val has not been called before.


My environment:

  1. python 3.11.9
  2. param 2.2.0
@maximlt
Copy link
Member

maximlt commented Feb 10, 2025

Hi @dinya, it seems like when you copied your code over to this issue the indentation got wrong, for instance this is not valid Python:

with param.edit_constant(a):
a.val = 6

Could you please edit your post to fix the indentation?

@dinya
Copy link
Author

dinya commented Feb 10, 2025

@maximlt Thanks, I fixed it.

@maximal
Copy link

maximal commented Feb 10, 2025

@dinya, you‘re welcome!

@maximlt
Copy link
Member

maximlt commented Feb 10, 2025

This is indeed a bug and a potential duplicate of #931, thanks for reporting it @dinya !

@maximlt maximlt added the type-bug Bug report label Feb 10, 2025
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