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

Prevent overriding Parameterized internal attributes? #690

Closed
maximlt opened this issue Mar 6, 2023 · 3 comments
Closed

Prevent overriding Parameterized internal attributes? #690

maximlt opened this issue Mar 6, 2023 · 3 comments
Labels
type-bug Bug report
Milestone

Comments

@maximlt
Copy link
Member

maximlt commented Mar 6, 2023

Parameterized classes and instances have attributes that users of those objects should not override, as doing so could break their code in hard-to-debug ways. Could/should ParameterizedMetaclass.__setattr__ be updated to raise a warning/error when the attribute name is one used by Param? This behavior should at least be documented.

Opening this issue was motivated by seeing the public initialized attribute on a Parameterized instance, which gave me goosebumps as I'm pretty sure I used that in some of my code. Fortunately it's planned to be updated to _initialized (#543).

import param
from pprint import pprint

class P(param.Parameterized):
    s = param.String()

pprint(P.__dict__)
print()
    
p = P(s='test')

pprint(p.__dict__)

EDIT: adding the output of the snippet above...

mappingproxy({'_P__params': {'name': <param.parameterized.String object at 0x1076ca040>,
                             's': <param.parameterized.String object at 0x10769af40>},
              '__doc__': 'params(s=String, name=String)\n'
                         "\x1b[1;32mParameters of 'P'\n"
                         '=================\n'
                         '\x1b[0m\n'
                         '\x1b[1;31mParameters changed from their default '
                         'values are marked in red.\x1b[0m\n'
                         '\x1b[1;36mSoft bound values are marked in '
                         'cyan.\x1b[0m\n'
                         'C/V= Constant/Variable, RO/RW = ReadOnly/ReadWrite, '
                         'AN=Allow None\n'
                         '\n'
                         '\x1b[1;34mNameValue  Type   Mode \x1b[0m\n'
                         '\n'
                         "s   ''  String  V RW \n"
                         '\n'
                         '\x1b[1;32mParameter docstrings:\n'
                         '=====================\x1b[0m\n'
                         '\n'
                         '\x1b[1;34ms: < No docstring available >\x1b[0m',
              '__module__': '__main__',
              '_param': <param.parameterized.Parameters object at 0x107651550>,
              '_parameters_state': {'BATCH_WATCH': False,
                                    'TRIGGER': False,
                                    'events': [],
                                    'watchers': []},
              'name': <param.parameterized.String object at 0x1076ca040>,
              's': <param.parameterized.String object at 0x10769af40>})

{'_dynamic_watchers': defaultdict(<class 'list'>, {}),
 '_instance__params': {},
 '_name_param_value': 'P00023',
 '_param_watchers': {},
 '_parameters_state': {'BATCH_WATCH': False,
                       'TRIGGER': False,
                       'events': [],
                       'watchers': []},
 '_s_param_value': 'test',
 'initialized': True}
@maximlt maximlt added the type-bug Bug report label Mar 6, 2023
@jlstevens
Copy link
Contributor

Not sure it is a bug as such, just something you have to be aware of when using param.

That said, if it isn't too hard to help guard against such mistakes, then this is a good suggestion for an enhancement!

@maximlt maximlt added this to the 2.0 milestone Apr 5, 2023
@jbednar
Copy link
Member

jbednar commented May 12, 2023

Agreed, it would be nice to catch those.

@maximlt
Copy link
Member Author

maximlt commented Jul 12, 2023

The Parameterized namespace being now reduced and documented after #766, I consider this issue resolved.

@maximlt maximlt closed this as completed Jul 12, 2023
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

No branches or pull requests

3 participants