traitlets icon indicating copy to clipboard operation
traitlets copied to clipboard

Singletons configurable allow multiple instances

Open Carreau opened this issue 3 years ago • 2 comments

See https://github.com/ipython/ipython/issues/14006

Basically if you create a singleton via its constructor it does not check for other instances.

This is problematic as Applications are SingletonConfigurable and can have subapps, so inherently traitlets is unsound. The order in which you create those will change the result of Application.instance() I believe.

Carreau avatar Sep 19 '23 08:09 Carreau

This is intentional I believe, the singleton API is only accessed via the instance() / clear_instance() / instantiated() methods: https://traitlets.readthedocs.io/en/stable/config.html#the-main-concepts

Singletons are not strictly enforced - you can have many instances of a given singleton class, but the instance() method will always return the same one.

There's an example in the docs under examples/docs/multiple_apps.py, although I added it based on related threads in this tracker.

I'm not sure about the ordering issue, normally SubApp.instance() would raise if another subapp is already instantiated. See for example related discussion in https://github.com/ipython/traitlets/issues/474 and the (inactive) PR which aims to handles the case of a non-singleton subcommand.

Edit: I do agree the docs could use further clarification on this, it definitely is a bit confusing and the docstring for SingletonConfigurable is misleading as per the current design. But I think trying to change Application.__init__() to be truly a singleton would break a significant amount of downstream code.

azjps avatar Sep 19 '23 11:09 azjps

Yeah, the problem is I believe that a few places are not using .instance() which can lead to assumptions about having singletons.

Carreau avatar Sep 19 '23 12:09 Carreau