Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
only_once will be called multiple times #22
Comments
|
I'd argue against making this explicit. I think the main use-case for |
|
We have the choice between:
|
|
My vote goes to deprecation. The main use-case for
Also, removing stuff just feels good :), removing complexity and such... |
|
Over the years, I've written deprecate decorators of the form: import functools
def deprecate(warning, logger=None):
def wrap(f):
_call_count = 0
@functools.wraps(f)
def wrapped_f(*args, **kwargs):
nonlocal _call_count
if not _call_count:
# log or print deprecation here
print(warning)
_call_count = 1
return f(*args, **kwargs)
return wrapped_f
return wrap
@deprecate("Don't use this")
def deprecated_function():
# do something
print("deprecated_function")
@deprecate("This one either")
def f2():
print("f2 away")
deprecated_function()
deprecated_function()
f2()
deprecated_function()
f2()
f2()with the corresponding output:
This has the advantage where the deprecated function will only log once, regardless of how many calls there are. It's then fairly easy to add a date to be removed and just grep the source to see when to remove it. |
|
Well, this will give a warning in the juju log, but not at build time. We
could do a reactive event loop probe to register all the handlers and do
deprecation notices at that time. That way we catch them at build.
…
|
|
If it was only wanted at build time (which, on reflection, might be more useful), then either a simple-ish regex of the source code would reveal if there were any only_once decorators. An alternative solution might be to mock the only_once decorator and try to walk the built charm and import /reload all the modules which would spit out if it was being used. We do something similar in charms.openstack to verify which reactive decorators we have used during unit testing. |
|
+1 for a regex. I have charms that require the hook env to be present
during module load.
|
|
regular experssion feels wonky to me, but if it can be done well and clean I'm not going to fuss either way. |
stub42 commentedNov 9, 2015
The guard used to stop only_once decorated code from running more than once includes the code line number in its key. The method will be run again if it is moved around the source file, or into a different source file for that matter. Which is fine, as I can argue that running only_once decorated methods once after every upgrade-charm hook is a good thing.
I think this should be explicit, rather than random, and the was_invoked/marked_invoked caches cleared at the beginning of an upgrade-charm hook. If people do need a only_once_really decorator, the developer would need to provide a key rather than a volatile one generated for them.