-
Couldn't load subscription status.
- Fork 53
Nested containers #704
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
base: main
Are you sure you want to change the base?
Nested containers #704
Changes from 4 commits
8faf455
26ab96e
7c4aa25
0b88814
5ab31e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ | |
|
|
||
| from typing_extensions import TypeGuard, get_args, get_origin | ||
|
|
||
| from magicgui.types import JsonStringFormats, Undefined, _Undefined | ||
| from magicgui.types import JsonStringFormats, NestedValueWidgets, Undefined, _Undefined | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterable, Iterator, Mapping | ||
|
|
@@ -394,7 +394,9 @@ | |
| kwargs.pop("name", None) | ||
| return dc.replace(self, **kwargs) | ||
|
|
||
| def create_widget(self, value: T | _Undefined = Undefined) -> BaseValueWidget[T]: | ||
| def create_widget( | ||
| self, value: T | _Undefined = Undefined | ||
| ) -> BaseValueWidget[T] | NestedValueWidgets: | ||
| """Create a new Widget for this field.""" | ||
| from magicgui.type_map import get_widget_class | ||
|
|
||
|
|
@@ -441,7 +443,20 @@ | |
| opts["min"] = d["exclusive_minimum"] + m | ||
|
|
||
| value = value if value is not Undefined else self.get_default() # type: ignore | ||
| cls, kwargs = get_widget_class(value=value, annotation=self.type, options=opts) | ||
| try: | ||
| cls, kwargs = get_widget_class( | ||
| value=value, annotation=self.type, options=opts | ||
| ) | ||
| except ValueError: | ||
|
||
| try: | ||
| wdg = build_widget(self.type) | ||
| wdg.label = self.name if self.name else "" | ||
| return wdg | ||
| except TypeError as e: | ||
| raise TypeError( | ||
| f"Could not create widget for field {self.name!r} ", | ||
| f"with value {value!r}", | ||
| ) from e | ||
| return cls(**kwargs) # type: ignore | ||
|
|
||
|
|
||
|
|
@@ -786,7 +801,7 @@ | |
| values: Mapping[str, Any] | None = None, | ||
| *, | ||
| container_kwargs: Mapping | None = None, | ||
| ) -> ContainerWidget[BaseValueWidget]: | ||
| ) -> ContainerWidget[NestedValueWidgets]: | ||
| """Create a container widget from a sequence of UiFields. | ||
| This function is the heart of build_widget. | ||
|
|
@@ -849,7 +864,7 @@ | |
|
|
||
|
|
||
| # TODO: unify this with magicgui | ||
| def build_widget(cls_or_instance: Any) -> ContainerWidget[BaseValueWidget]: | ||
| def build_widget(cls_or_instance: Any) -> ContainerWidget[NestedValueWidgets]: | ||
| """Build a magicgui widget from a dataclass, attrs, pydantic, or function.""" | ||
| values = None if isinstance(cls_or_instance, type) else _get_values(cls_or_instance) | ||
| fields = get_ui_fields(cls_or_instance) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to avoid functions that return unions if at all possible. Isn't a
NestedValueWidgetultimately also just aValueWidget? Doesn't it have teh same interface? Is there anything in the PR here that actually requires a new type? Can we revert these NestedValueWidget changes without mypy breaking?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created that just to make mypy happy. But I agree not having unions is better.
Looking at
QuantityEditforpint.Quantity- which is aValueWidget, yes it should be aValueWidget, too. On the other hand we now haveValuedContainerWidgete.g. for tuples, so maybe it should rather be that?