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

WidgetStore for module specific type-to-widget mapping #676

Open
hanjinliu opened this issue Nov 5, 2024 · 1 comment · May be fixed by #677
Open

WidgetStore for module specific type-to-widget mapping #676

hanjinliu opened this issue Nov 5, 2024 · 1 comment · May be fixed by #677

Comments

@hanjinliu
Copy link
Contributor

magicgui has a global map to determine which widget to use for each type. A problem I'm facing now is that if I updated the type map in my module, it will affect all other modules that depend on magicgui. More specifically, I want to use LineEdit for all the simple types including int and float for my purpose but I don't want to change the behaviors of others (for example, if Annotated[int, {"max": 10}] is used in another module, it stops working if I change the widget for int to LineEdit). I think this problem can be solved by using type_registered context manager, but here's much simpler solution and I'd like to discuss whether we should have this feature.

In the type_map submodule, we can define a WidgetStore class that holds a mapping from type to widget and has all the methods useful for the type mapping tasks:

class WidgetStore:
    def magicgui(self, ...): ...
    def register_type(self, ...): ...
    def match_type(self, ...): ...
    def get_widget_class(self, ...): ...
    ...

This does not affect the current usages because we can just define a default global WidgetStore instance.

_WIDGET_STORE = WidgetStore()

magicgui = _WIDGET_STORE.magicgui
register_type = _WIDGET_STORE.register_type
# ... and so on

If one wants to use their own rule, they can just create their own widget store.

my_store = WidgetStore()

my_store.register_type(int, widget_type=MyIntEdit)

@my_store.magicgui
def func(x: int):
    print(x)

If you think it's reasonable, I'm happy to send a PR.

@tlambert03
Copy link
Member

Yep, I think it's very reasonable!

Globals always pose problems, and it looks like you found it :) happy to see a pr

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

Successfully merging a pull request may close this issue.

2 participants