From b82b3c8578676cd87ad992a14934a51bd22d1ace Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Fri, 22 Sep 2023 17:08:29 -0400 Subject: [PATCH] Revert "fix: fix literal with explicit widget type" This reverts commit 35f35fe49e78963223c2a13be6058e45d35411d9. --- src/magicgui/type_map/_type_map.py | 32 ++++++++---------------------- tests/test_types.py | 10 ---------- 2 files changed, 8 insertions(+), 34 deletions(-) diff --git a/src/magicgui/type_map/_type_map.py b/src/magicgui/type_map/_type_map.py index ff16f5f2a..dbe3e8931 100644 --- a/src/magicgui/type_map/_type_map.py +++ b/src/magicgui/type_map/_type_map.py @@ -93,8 +93,14 @@ def match_type(type_: Any, default: Any | None = None) -> WidgetTuple | None: return widgets.FunctionGui, {"function": default} origin = get_origin(type_) or type_ - choices, nullable = _literal_choices(type_) - if choices is not None: # it's a Literal type + if origin is Literal: + choices = [] + nullable = False + for choice in get_args(type_): + if choice is None: + nullable = True + else: + choices.append(choice) return widgets.ComboBox, {"choices": choices, "nullable": nullable} # sequence of paths @@ -187,24 +193,6 @@ def _type_optional( return type_, nullable -def _literal_choices(annotation: Any) -> tuple[list | None, bool]: - """Return choices and nullable for a Literal type. - - if annotation is not a Literal type, returns (None, False) - """ - origin = get_origin(annotation) or annotation - choices: list | None = None - nullable = False - if origin is Literal: - choices = [] - for choice in get_args(annotation): - if choice is None: - nullable = True - else: - choices.append(choice) - return choices, nullable - - def _pick_widget_type( value: Any = Undefined, annotation: Any = Undefined, @@ -231,10 +219,6 @@ def _pick_widget_type( _type, optional = _type_optional(value, annotation) options.setdefault("nullable", optional) choices = choices or (isinstance(_type, EnumMeta) and _type) - literal_choices, nullable = _literal_choices(annotation) - if literal_choices is not None: - choices = literal_choices - options["nullable"] = nullable if "widget_type" in options: widget_type = options.pop("widget_type") diff --git a/tests/test_types.py b/tests/test_types.py index 83d89a62b..aa98798f9 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -187,13 +187,3 @@ def test_type_registered_warns(): register_type(Path, widget_type=widgets.TextEdit) assert isinstance(widgets.create_widget(annotation=Path), widgets.TextEdit) assert isinstance(widgets.create_widget(annotation=Path), widgets.FileEdit) - - -def test_pick_widget_literal(): - from typing import Literal - - cls, options = type_map.get_widget_class( - annotation=Annotated[Literal["a", "b"], {"widget_type": "RadioButtons"}] - ) - assert cls == widgets.RadioButtons - assert set(options["choices"]) == {"a", "b"}