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

Update test to work with app-model==0.2.4 #6573

Merged
merged 1 commit into from
Jan 7, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 50 additions & 24 deletions napari/utils/_tests/test_key_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from unittest.mock import patch

import pytest
from app_model.types import KeyCode, KeyMod
from app_model.types import KeyBinding, KeyCode, KeyMod

from napari.utils import key_bindings
from napari.utils.key_bindings import (
Expand All @@ -25,7 +25,7 @@ def forty_two():
return 42

bind_key(kb, 'A', forty_two)
assert kb == {"A": forty_two}
assert kb == {KeyBinding.from_str("A"): forty_two}

# overwrite
def spam():
Expand All @@ -35,7 +35,7 @@ def spam():
bind_key(kb, 'A', spam)

bind_key(kb, 'A', spam, overwrite=True)
assert kb == {"A": spam}
assert kb == {KeyBinding.from_str("A"): spam}

# unbind
bind_key(kb, 'A', None)
Expand All @@ -44,11 +44,11 @@ def spam():
# check signature
# blocker
bind_key(kb, 'A', ...)
assert kb == {'A': ...}
assert kb == {KeyBinding.from_str('A'): ...}

# catch-all
bind_key(kb, ..., ...)
assert kb == {'A': ..., ...: ...}
assert kb == {KeyBinding.from_str('A'): ..., ...: ...}

# typecheck
with pytest.raises(TypeError):
Expand All @@ -58,7 +58,7 @@ def spam():
kb = {}
bind_key(kb, KeyMod.Shift | KeyCode.KeyA, ...)
(key,) = kb.keys()
assert key == 'Shift-A'
assert key == KeyBinding.from_str('Shift-A')


def test_bind_key_decorator():
Expand All @@ -68,7 +68,7 @@ def test_bind_key_decorator():
def foo():
...

assert kb == {"A": foo}
assert kb == {KeyBinding.from_str("A"): foo}


def test_keymap_provider():
Expand All @@ -89,7 +89,7 @@ class Bar(Foo):
class Baz(KeymapProvider):
class_keymap = {'A': ...}

assert Baz.class_keymap == {'A': ...}
assert Baz.class_keymap == {KeyBinding.from_str('A'): ...}


def test_bind_keymap():
Expand Down Expand Up @@ -149,9 +149,15 @@ def test_handle_single_keymap_provider():
_bind_keymap(foo.class_keymap, foo),
]
assert handler.active_keymap == {
'A': types.MethodType(foo.class_keymap['A'], foo),
'B': types.MethodType(foo.keymap['B'], foo),
'E': types.MethodType(foo.keymap['E'], foo),
KeyBinding.from_str('A'): types.MethodType(
foo.class_keymap[KeyBinding.from_str('A')], foo
),
KeyBinding.from_str('B'): types.MethodType(
foo.keymap[KeyBinding.from_str('B')], foo
),
KeyBinding.from_str('E'): types.MethodType(
foo.keymap[KeyBinding.from_str('E')], foo
),
}

# non-overwritten class keybinding
Expand Down Expand Up @@ -198,10 +204,16 @@ def abc():
x = 42

assert handler.active_keymap == {
'A': types.MethodType(foo.class_keymap['A'], foo),
'B': types.MethodType(foo.keymap['B'], foo),
'D': abc,
'E': types.MethodType(bar.class_keymap['E'], bar),
KeyBinding.from_str('A'): types.MethodType(
foo.class_keymap[KeyBinding.from_str('A')], foo
),
KeyBinding.from_str('B'): types.MethodType(
foo.keymap[KeyBinding.from_str('B')], foo
),
KeyBinding.from_str('D'): abc,
KeyBinding.from_str('E'): types.MethodType(
bar.class_keymap[KeyBinding.from_str('E')], bar
),
}

handler.press_key('D')
Expand All @@ -223,9 +235,15 @@ def test_handle_multiple_keymap_providers():
_bind_keymap(foo.class_keymap, foo),
]
assert handler.active_keymap == {
'A': types.MethodType(foo.class_keymap['A'], foo),
'B': types.MethodType(foo.keymap['B'], foo),
'E': types.MethodType(bar.class_keymap['E'], bar),
KeyBinding.from_str('A'): types.MethodType(
foo.class_keymap[KeyBinding.from_str('A')], foo
),
KeyBinding.from_str('B'): types.MethodType(
foo.keymap[KeyBinding.from_str('B')], foo
),
KeyBinding.from_str('E'): types.MethodType(
bar.class_keymap[KeyBinding.from_str('E')], bar
),
}

# check 'bar' callback
Expand All @@ -247,7 +265,9 @@ def catch_all(x):
bar.class_keymap[...] = catch_all
assert handler.active_keymap == {
...: types.MethodType(catch_all, bar),
'E': types.MethodType(bar.class_keymap['E'], bar),
KeyBinding.from_str('E'): types.MethodType(
bar.class_keymap[KeyBinding.from_str('E')], bar
),
}
assert not hasattr(bar, 'catch_all')
handler.press_key('Z')
Expand All @@ -256,7 +276,9 @@ def catch_all(x):
# empty
bar.class_keymap[...] = ...
assert handler.active_keymap == {
'E': types.MethodType(bar.class_keymap['E'], bar)
KeyBinding.from_str('E'): types.MethodType(
bar.class_keymap[KeyBinding.from_str('E')], bar
),
}
del foo.B
handler.press_key('B')
Expand All @@ -275,8 +297,12 @@ def test_inherited_keymap():
_bind_keymap(Bar.class_keymap, baz),
]
assert handler.active_keymap == {
'F': types.MethodType(baz.class_keymap['F'], baz),
'E': types.MethodType(Bar.class_keymap['E'], baz),
KeyBinding.from_str('F'): types.MethodType(
baz.class_keymap[KeyBinding.from_str('F')], baz
),
KeyBinding.from_str('E'): types.MethodType(
Bar.class_keymap[KeyBinding.from_str('E')], baz
),
}


Expand Down Expand Up @@ -334,14 +360,14 @@ class Foo2(KeymapProvider):

# instance binding
foo.bind_key('A', lambda: 42)
assert foo.keymap['A']() == 42
assert foo.keymap[KeyBinding.from_str('A')]() == 42

# class binding
@Foo2.bind_key('B')
def bar():
return 'SPAM'

assert Foo2.class_keymap['B'] is bar
assert Foo2.class_keymap[KeyBinding.from_str('B')] is bar


def test_bind_key_doc():
Expand Down
10 changes: 9 additions & 1 deletion napari/utils/key_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,15 @@ class KeymapProvider:

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.keymap = {}
self._keymap = {}

@property
def keymap(self):
return self._keymap

@keymap.setter
def keymap(self, value):
self._keymap = {coerce_keybinding(k): v for k, v in value.items()}

def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
Expand Down
2 changes: 1 addition & 1 deletion resources/constraints/constraints_py3.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ alabaster==0.7.13
# via sphinx
annotated-types==0.6.0
# via pydantic
app-model==0.2.3
app-model==0.2.4
# via
# -r napari_repo/resources/constraints/version_denylist.txt
# napari (napari_repo/setup.cfg)
Expand Down
2 changes: 1 addition & 1 deletion resources/constraints/constraints_py3.10_docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
alabaster==0.7.13
# via sphinx
app-model==0.2.3
app-model==0.2.4
# via
# -r napari_repo/resources/constraints/version_denylist.txt
# napari (napari_repo/setup.cfg)
Expand Down
2 changes: 1 addition & 1 deletion resources/constraints/constraints_py3.10_pydantic_1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
alabaster==0.7.13
# via sphinx
app-model==0.2.3
app-model==0.2.4
# via
# -r napari_repo/resources/constraints/version_denylist.txt
# napari (napari_repo/setup.cfg)
Expand Down
2 changes: 1 addition & 1 deletion resources/constraints/constraints_py3.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ alabaster==0.7.13
# via sphinx
annotated-types==0.6.0
# via pydantic
app-model==0.2.3
app-model==0.2.4
# via
# -r napari_repo/resources/constraints/version_denylist.txt
# napari (napari_repo/setup.cfg)
Expand Down
2 changes: 1 addition & 1 deletion resources/constraints/constraints_py3.11_pydantic_1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
alabaster==0.7.13
# via sphinx
app-model==0.2.3
app-model==0.2.4
# via
# -r napari_repo/resources/constraints/version_denylist.txt
# napari (napari_repo/setup.cfg)
Expand Down
2 changes: 1 addition & 1 deletion resources/constraints/constraints_py3.8.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ alabaster==0.7.13
# via sphinx
annotated-types==0.6.0
# via pydantic
app-model==0.2.3
app-model==0.2.4
# via
# -r napari_repo/resources/constraints/version_denylist.txt
# napari (napari_repo/setup.cfg)
Expand Down
2 changes: 1 addition & 1 deletion resources/constraints/constraints_py3.8_pydantic_1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
alabaster==0.7.13
# via sphinx
app-model==0.2.3
app-model==0.2.4
# via
# -r napari_repo/resources/constraints/version_denylist.txt
# napari (napari_repo/setup.cfg)
Expand Down
2 changes: 1 addition & 1 deletion resources/constraints/constraints_py3.9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ alabaster==0.7.13
# via sphinx
annotated-types==0.6.0
# via pydantic
app-model==0.2.3
app-model==0.2.4
# via
# -r napari_repo/resources/constraints/version_denylist.txt
# napari (napari_repo/setup.cfg)
Expand Down
2 changes: 1 addition & 1 deletion resources/constraints/constraints_py3.9_examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ alabaster==0.7.13
# via sphinx
annotated-types==0.6.0
# via pydantic
app-model==0.2.3
app-model==0.2.4
# via
# -r napari_repo/resources/constraints/version_denylist.txt
# napari (napari_repo/setup.cfg)
Expand Down
2 changes: 1 addition & 1 deletion resources/constraints/constraints_py3.9_pydantic_1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
alabaster==0.7.13
# via sphinx
app-model==0.2.3
app-model==0.2.4
# via
# -r napari_repo/resources/constraints/version_denylist.txt
# napari (napari_repo/setup.cfg)
Expand Down