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

fix: Support nested Annotated types #536

Merged
merged 3 commits into from
Feb 24, 2023
Merged
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
8 changes: 6 additions & 2 deletions src/magicgui/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ def make_annotated(annotation: Any = Any, options: dict | None = None) -> Any:
_options = (options or {}).copy()

if get_origin(annotation) is Annotated:
annotation, anno_options = get_args(annotation)
_options.update(anno_options)
# nested Annotated type results in multiple dicts.
annotation, *anno_options = get_args(annotation)
for opt in anno_options:
if not isinstance(opt, dict):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... was I doing this when it worked before? I'm not sure I want to prevent people from using Annotated for any other reason.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I guess I'm already preventing it 😂 let's go with it!

raise TypeError("Every Annotated option must be a dict")
_options.update(opt)
return Annotated[annotation, _options]


Expand Down
4 changes: 4 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def test_pick_widget_builtins_forward_refs(cls, string):
Annotated[float, {"widget_type": "FloatSlider", "step": 9}],
widgets.FloatSlider,
),
(
Annotated[Annotated[int, {"widget_type": "Slider"}], {"max": 10}],
widgets.Slider,
),
],
)
def test_annotated_types(hint, expected_wdg):
Expand Down