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

Implement TypeMap to enable module specific type map registration #677

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/magicgui/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from typing_extensions import Unpack

from magicgui.application import AppRef
from magicgui.type_map import TypeMap
from magicgui.widgets import Container, Widget
from magicgui.widgets.bases._container_widget import ContainerKwargs

Expand Down Expand Up @@ -188,12 +189,18 @@ def __str__(self) -> str:
)
)

def to_widget(self, app: AppRef | None = None) -> Widget:
def to_widget(
self,
app: AppRef | None = None,
type_map: TypeMap | None = None,
) -> Widget:
"""Create and return a widget for this object."""
from magicgui.widgets import create_widget
from magicgui.type_map import TypeMap

value = Undefined if self.default in (self.empty, TZ_EMPTY) else self.default
widget = create_widget(
if type_map is None:
type_map = TypeMap.global_instance()
widget = type_map.create_widget(
name=self.name,
value=value,
annotation=self.annotation,
Expand Down Expand Up @@ -286,19 +293,26 @@ def from_signature(
raise_on_unknown=raise_on_unknown,
)

def widgets(self, app: AppRef | None = None) -> MappingProxyType:
def widgets(
self,
app: AppRef | None = None,
type_map: TypeMap | None = None,
) -> MappingProxyType:
"""Return mapping from parameters to widgets for all params in Signature."""
return MappingProxyType(
{n: p.to_widget(app) for n, p in self.parameters.items()}
{n: p.to_widget(app, type_map=type_map) for n, p in self.parameters.items()}
)

def to_container(
self, app: AppRef | None = None, **kwargs: Unpack[ContainerKwargs]
self,
app: AppRef | None = None,
type_map: TypeMap | None = None,
**kwargs: Unpack[ContainerKwargs],
) -> Container:
"""Return a ``magicgui.widgets.Container`` for this MagicSignature."""
from magicgui.widgets import Container

kwargs["widgets"] = list(self.widgets(app).values())
kwargs["widgets"] = list(self.widgets(app, type_map=type_map).values())
return Container(**kwargs)

def replace( # type: ignore[override]
Expand Down
9 changes: 8 additions & 1 deletion src/magicgui/type_map/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
"""Functions that map python types to widgets."""

from ._type_map import get_widget_class, register_type, type2callback, type_registered
from ._type_map import (
TypeMap,
get_widget_class,
register_type,
type2callback,
type_registered,
)

__all__ = [
"get_widget_class",
"register_type",
"type_registered",
"type2callback",
"TypeMap",
]
Loading
Loading