Skip to content

Commit a5669e3

Browse files
authored
Implement TypeMap to enable module specific type map registration (#677)
* implement TypeMap * update tests * fix py39 and pint
1 parent 674094f commit a5669e3

File tree

10 files changed

+1110
-923
lines changed

10 files changed

+1110
-923
lines changed

src/magicgui/signature.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from typing_extensions import Unpack
3030

3131
from magicgui.application import AppRef
32+
from magicgui.type_map import TypeMap
3233
from magicgui.widgets import Container, Widget
3334
from magicgui.widgets.bases._container_widget import ContainerKwargs
3435

@@ -189,12 +190,18 @@ def __str__(self) -> str:
189190
)
190191
)
191192

192-
def to_widget(self, app: AppRef | None = None) -> Widget:
193+
def to_widget(
194+
self,
195+
app: AppRef | None = None,
196+
type_map: TypeMap | None = None,
197+
) -> Widget:
193198
"""Create and return a widget for this object."""
194-
from magicgui.widgets import create_widget
199+
from magicgui.type_map import TypeMap
195200

196201
value = Undefined if self.default in (self.empty, TZ_EMPTY) else self.default
197-
widget = create_widget(
202+
if type_map is None:
203+
type_map = TypeMap.global_instance()
204+
widget = type_map.create_widget(
198205
name=self.name,
199206
value=value,
200207
annotation=self.annotation,
@@ -287,19 +294,26 @@ def from_signature(
287294
raise_on_unknown=raise_on_unknown,
288295
)
289296

290-
def widgets(self, app: AppRef | None = None) -> MappingProxyType:
297+
def widgets(
298+
self,
299+
app: AppRef | None = None,
300+
type_map: TypeMap | None = None,
301+
) -> MappingProxyType:
291302
"""Return mapping from parameters to widgets for all params in Signature."""
292303
return MappingProxyType(
293-
{n: p.to_widget(app) for n, p in self.parameters.items()}
304+
{n: p.to_widget(app, type_map=type_map) for n, p in self.parameters.items()}
294305
)
295306

296307
def to_container(
297-
self, app: AppRef | None = None, **kwargs: Unpack[ContainerKwargs]
308+
self,
309+
app: AppRef | None = None,
310+
type_map: TypeMap | None = None,
311+
**kwargs: Unpack[ContainerKwargs],
298312
) -> Container:
299313
"""Return a ``magicgui.widgets.Container`` for this MagicSignature."""
300314
from magicgui.widgets import Container
301315

302-
kwargs["widgets"] = list(self.widgets(app).values())
316+
kwargs["widgets"] = list(self.widgets(app, type_map=type_map).values())
303317
return Container(**kwargs)
304318

305319
def replace( # type: ignore[override]

src/magicgui/type_map/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
"""Functions that map python types to widgets."""
22

3-
from ._type_map import get_widget_class, register_type, type2callback, type_registered
3+
from ._type_map import (
4+
TypeMap,
5+
get_widget_class,
6+
register_type,
7+
type2callback,
8+
type_registered,
9+
)
410

511
__all__ = [
612
"get_widget_class",
713
"register_type",
814
"type_registered",
915
"type2callback",
16+
"TypeMap",
1017
]

0 commit comments

Comments
 (0)