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

Singletons configurable allow multiple instances #867

Open
Carreau opened this issue Sep 19, 2023 · 2 comments
Open

Singletons configurable allow multiple instances #867

Carreau opened this issue Sep 19, 2023 · 2 comments

Comments

@Carreau
Copy link
Member

Carreau commented Sep 19, 2023

See ipython/ipython#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.

@azjps
Copy link
Collaborator

azjps commented Sep 19, 2023

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 #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.

@Carreau
Copy link
Member Author

Carreau commented Sep 19, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants