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: small getitem fixes for napari #44

Merged
merged 4 commits into from
Jul 19, 2022
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
2 changes: 1 addition & 1 deletion src/app_model/registries/_commands_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def execute_command(
If the command is not registered or has no callbacks.
"""
try:
cmd = self._commands[id].run_injected
cmd = self[id].run_injected
except KeyError as e:
raise KeyError(f"Command {id!r} not registered") from e

Expand Down
14 changes: 9 additions & 5 deletions src/app_model/registries/_menus_reg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Callable,
Dict,
Final,
Iterable,
Iterator,
List,
Optional,
Expand All @@ -18,6 +19,8 @@
from ..types import MenuItem, MenuOrSubmenu
from ..types._constants import DisposeCallable

MenuId = str


class MenusRegistry:
"""Registry for menu and submenu items."""
Expand All @@ -26,10 +29,10 @@ class MenusRegistry:
menus_changed = Signal(set)

def __init__(self) -> None:
self._menu_items: Dict[str, List[MenuOrSubmenu]] = {}
self._menu_items: Dict[MenuId, List[MenuOrSubmenu]] = {}

def append_menu_items(
self, items: Sequence[Tuple[str, MenuOrSubmenu]]
self, items: Sequence[Tuple[MenuId, MenuOrSubmenu]]
) -> DisposeCallable:
"""Append menu items to the registry.

Expand Down Expand Up @@ -71,14 +74,15 @@ def _dispose() -> None:

def __iter__(
self,
) -> Iterator[Tuple[str, List[MenuOrSubmenu]]]:
) -> Iterator[Tuple[MenuId, Iterable[MenuOrSubmenu]]]:
yield from self._menu_items.items()

def __contains__(self, id: object) -> bool:
return id in self._menu_items

def get_menu(self, menu_id: str) -> List[MenuOrSubmenu]:
def get_menu(self, menu_id: MenuId) -> List[MenuOrSubmenu]:
"""Return menu items for `menu_id`."""
# using method rather than __getitem__ so that subclasses can use arguments
return self._menu_items[menu_id]

def __repr__(self) -> str:
Expand Down Expand Up @@ -114,7 +118,7 @@ def _render(self) -> List[str]:
lines.append("")
return lines

def iter_menu_groups(self, menu_id: str) -> Iterator[List[MenuOrSubmenu]]:
def iter_menu_groups(self, menu_id: MenuId) -> Iterator[List[MenuOrSubmenu]]:
"""Iterate over menu groups for `menu_id`.

Groups are broken into sections (lists of menu or submenu items) based on
Expand Down