Skip to content

Commit 146e65d

Browse files
committed
test fixes
1 parent bdf729f commit 146e65d

File tree

4 files changed

+31
-32
lines changed

4 files changed

+31
-32
lines changed

src/textual/binding.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ def __iter__(self) -> Iterator[tuple[str, Binding]]:
126126
for binding in bindings
127127
]
128128
)
129-
# for key, bindings in self.keys.items():
130-
# for binding in bindings:
131-
# yield key, binding
132129

133130
@classmethod
134131
def from_keys(cls, keys: dict[str, list[Binding]]) -> BindingsMap:
@@ -198,16 +195,18 @@ def bind(
198195
"""
199196
all_keys = [key.strip() for key in keys.split(",")]
200197
for key in all_keys:
201-
self.keys[key] = Binding(
202-
key,
203-
action,
204-
description,
205-
show=bool(description and show),
206-
key_display=key_display,
207-
priority=priority,
198+
self.keys.setdefault(key, []).append(
199+
Binding(
200+
key,
201+
action,
202+
description,
203+
show=bool(description and show),
204+
key_display=key_display,
205+
priority=priority,
206+
)
208207
)
209208

210-
def get_key(self, key: str) -> Binding:
209+
def get_key(self, key: str) -> list[Binding]:
211210
"""Get a binding if it exists.
212211
213212
Args:

src/textual/dom.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ def _merge_bindings(cls) -> BindingsMap:
610610
keys: dict[str, list[Binding]] = {}
611611
for bindings_ in bindings:
612612
for key, key_bindings in bindings_.keys.items():
613-
keys.setdefault(key, []).extend(key_bindings)
613+
keys[key] = key_bindings
614614

615615
new_bindings = BindingsMap().from_keys(keys)
616616
return new_bindings

tests/test_binding.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ def more_bindings():
2727

2828

2929
def test_bindings_get_key(bindings):
30-
assert bindings.get_key("b") == Binding(
31-
"b", action="action1", description="description1"
32-
)
33-
assert bindings.get_key("c") == BINDING2
30+
assert bindings.get_key("b") == [
31+
Binding("b", action="action1", description="description1")
32+
]
33+
assert bindings.get_key("c") == [BINDING2]
3434
with pytest.raises(NoBinding):
3535
bindings.get_key("control+meta+alt+shift+super+hyper+t")
3636

3737

3838
def test_bindings_get_key_spaced_list(more_bindings):
39-
assert more_bindings.get_key("d").action == more_bindings.get_key("e").action
39+
assert more_bindings.get_key("d")[0].action == more_bindings.get_key("e")[0].action
4040

4141

4242
def test_bindings_merge_simple(bindings):
@@ -51,8 +51,11 @@ def test_bindings_merge_overlap():
5151
"a", action="another_action", description="another_description"
5252
)
5353
assert BindingsMap.merge([left, BindingsMap([another_binding])]).keys == {
54-
"a": another_binding,
55-
"b": Binding("b", action="action1", description="description1"),
54+
"a": [
55+
Binding("a", action="action1", description="description1"),
56+
another_binding,
57+
],
58+
"b": [Binding("b", action="action1", description="description1")],
5659
}
5760

5861

@@ -64,12 +67,9 @@ def test_bad_binding_tuple():
6467

6568

6669
def test_binding_from_tuples():
67-
assert (
68-
BindingsMap(((BINDING2.key, BINDING2.action, BINDING2.description),)).get_key(
69-
"c"
70-
)
71-
== BINDING2
72-
)
70+
assert BindingsMap(
71+
((BINDING2.key, BINDING2.action, BINDING2.description),)
72+
).get_key("c") == [BINDING2]
7373

7474

7575
def test_shown():

tests/test_binding_inheritance.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async def test_just_app_no_bindings() -> None:
4343
"ctrl+c",
4444
"ctrl+backslash",
4545
]
46-
assert pilot.app._bindings.get_key("ctrl+c").priority is True
46+
assert pilot.app._bindings.get_key("ctrl+c")[0].priority is True
4747

4848

4949
##############################################################################
@@ -67,8 +67,8 @@ async def test_just_app_alpha_binding() -> None:
6767
assert sorted(pilot.app._bindings.keys.keys()) == sorted(
6868
["ctrl+c", "ctrl+backslash", "a"]
6969
)
70-
assert pilot.app._bindings.get_key("ctrl+c").priority is True
71-
assert pilot.app._bindings.get_key("a").priority is True
70+
assert pilot.app._bindings.get_key("ctrl+c")[0].priority is True
71+
assert pilot.app._bindings.get_key("a")[0].priority is True
7272

7373

7474
##############################################################################
@@ -91,8 +91,8 @@ async def test_just_app_low_priority_alpha_binding() -> None:
9191
assert sorted(pilot.app._bindings.keys.keys()) == sorted(
9292
["ctrl+c", "ctrl+backslash", "a"]
9393
)
94-
assert pilot.app._bindings.get_key("ctrl+c").priority is True
95-
assert pilot.app._bindings.get_key("a").priority is False
94+
assert pilot.app._bindings.get_key("ctrl+c")[0].priority is True
95+
assert pilot.app._bindings.get_key("a")[0].priority is False
9696

9797

9898
##############################################################################
@@ -121,7 +121,7 @@ def on_mount(self) -> None:
121121
async def test_app_screen_with_bindings() -> None:
122122
"""Test a screen with a single key binding defined."""
123123
async with AppWithScreenThatHasABinding().run_test() as pilot:
124-
assert pilot.app.screen._bindings.get_key("a").priority is True
124+
assert pilot.app.screen._bindings.get_key("a")[0].priority is True
125125

126126

127127
##############################################################################
@@ -150,7 +150,7 @@ def on_mount(self) -> None:
150150
async def test_app_screen_with_low_bindings() -> None:
151151
"""Test a screen with a single low-priority key binding defined."""
152152
async with AppWithScreenThatHasALowBinding().run_test() as pilot:
153-
assert pilot.app.screen._bindings.get_key("a").priority is False
153+
assert pilot.app.screen._bindings.get_key("a")[0].priority is False
154154

155155

156156
##############################################################################

0 commit comments

Comments
 (0)