traitlets icon indicating copy to clipboard operation
traitlets copied to clipboard

The behaviour of traitlets.All in observe/unobserve

Open thomasaarholt opened this issue 7 years ago • 2 comments

I'm finding the behaviour of traits.All very confusing in observe and unobserve. Since I can't find much documentation on it, I thought I'd ask here.

My confusion started when I was using observe and unobserve over at ipywidgets. I created jupyter-widgets/ipywidgets#2057 as I thought it was an ipywidgets issue.

That issue is more descriptive (and will clarify this issue for anyone confused), but in short: I expected .unobserve(names=traitlets.All) to unobserve all traits for a given function. Instead, I find that I need to specify the same name as I did when I called .observe().

This is of course fine, but then I don't understand the point of All, or why it is listed in the docstring as a default argument.

To sum things up: I am confused by the behaviour and purpose of traitlets.All, and by the docstring of observe and unobserve.

To add: I am very impressed by traitlets in general, and I'm really excited to see what more I can use it for - I've already enjoyed using it very much.

thomasaarholt avatar Apr 25 '18 19:04 thomasaarholt

Unfortunately I'm not sure there's too much we can do at this point. I think changing this behavior might break too many things (which we're really trying to avoid). I think the best solution at this point would be to add some explicit examples in the docs that address these quarks.

rmorshea avatar Sep 03 '20 21:09 rmorshea

Just commenting here since it still seems to be relevant... I second the opinion that the docstrings could be a bit more descriptive and give a hint that traitlets.All is actually treated as an "explicit name" and not as a placeholder for "all possible names".

Why not add another sentinel (or simply None) as a possible way to unobserve a callable from any possible name?

  • widget.unobserve(handler, names=None)

As I see it, this could even become the new default without causing too much troubles since it would also disconnect from traitlets.All (the current default)...

The only thing that comes to my mind that this would break is if people have a handler connected to an explicit name and traitlets.All at the same time... (and I don't know why people would do that)

raphaelquast avatar Mar 08 '24 13:03 raphaelquast