Skip to content

Commit b7579b8

Browse files
committed
comments and naming
1 parent 35df151 commit b7579b8

File tree

8 files changed

+47
-30
lines changed

8 files changed

+47
-30
lines changed

src/textual/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3048,7 +3048,7 @@ async def _check_bindings(self, key: str, priority: bool = False) -> bool:
30483048
if priority
30493049
else self.screen._modal_binding_chain
30503050
):
3051-
key_bindings = bindings.keys.get(key, ())
3051+
key_bindings = bindings.key_to_bindings.get(key, ())
30523052
for binding in key_bindings:
30533053
if binding.priority == priority:
30543054
if await self.run_action(binding.action, namespace):

src/textual/binding.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def make_bindings(bindings: Iterable[BindingType]) -> Iterable[Binding]:
9292
raise BindingError(
9393
f"BINDINGS must contain a tuple of two or three strings, not {binding!r}"
9494
)
95-
binding = Binding(*binding)
95+
# `binding` is a tuple of 2 or 3 values at this point
96+
binding = Binding(*binding) # type: ignore[reportArgumentType]
9697

9798
# At this point we have a Binding instance, but the key may
9899
# be a list of keys, so now we unroll that single Binding
@@ -114,23 +115,32 @@ def make_bindings(bindings: Iterable[BindingType]) -> Iterable[Binding]:
114115
priority=binding.priority,
115116
)
116117

117-
self.keys: dict[str, list[Binding]] = {}
118+
self.key_to_bindings: dict[str, list[Binding]] = {}
118119
for binding in make_bindings(bindings or {}):
119-
self.keys.setdefault(binding.key, []).append(binding)
120+
self.key_to_bindings.setdefault(binding.key, []).append(binding)
120121

121122
def __iter__(self) -> Iterator[tuple[str, Binding]]:
123+
"""Iterating produces a sequence of (KEY, BINDING) tuples."""
122124
return iter(
123125
[
124126
(key, binding)
125-
for key, bindings in self.keys.items()
127+
for key, bindings in self.key_to_bindings.items()
126128
for binding in bindings
127129
]
128130
)
129131

130132
@classmethod
131133
def from_keys(cls, keys: dict[str, list[Binding]]) -> BindingsMap:
134+
"""Construct a BindingsMap from a dict of keys and bindings.
135+
136+
Args:
137+
keys: A dict that maps a key on to a list of `Binding` objects.
138+
139+
Returns:
140+
New `BindingsMap`
141+
"""
132142
bindings = cls()
133-
bindings.keys = keys
143+
bindings.key_to_bindings = keys
134144
return bindings
135145

136146
def copy(self) -> BindingsMap:
@@ -140,35 +150,34 @@ def copy(self) -> BindingsMap:
140150
New bindings object.
141151
"""
142152
copy = BindingsMap()
143-
copy.keys = self.keys.copy()
153+
copy.key_to_bindings = self.key_to_bindings.copy()
144154
return copy
145155

146156
def __rich_repr__(self) -> rich.repr.Result:
147-
yield self.keys
157+
yield self.key_to_bindings
148158

149159
@classmethod
150160
def merge(cls, bindings: Iterable[BindingsMap]) -> BindingsMap:
151-
"""Merge a bindings. Subsequent bound keys override initial keys.
161+
"""Merge a bindings.
152162
153163
Args:
154164
bindings: A number of bindings.
155165
156166
Returns:
157-
New bindings.
167+
New `BindingsMap`.
158168
"""
159169
keys: dict[str, list[Binding]] = {}
160170
for _bindings in bindings:
161-
for key, key_bindings in _bindings.keys.items():
171+
for key, key_bindings in _bindings.key_to_bindings.items():
162172
keys.setdefault(key, []).extend(key_bindings)
163-
164173
return BindingsMap.from_keys(keys)
165174

166175
@property
167176
def shown_keys(self) -> list[Binding]:
168177
"""A list of bindings for shown keys."""
169178
keys = [
170179
binding
171-
for bindings in self.keys.values()
180+
for bindings in self.key_to_bindings.values()
172181
for binding in bindings
173182
if binding.show
174183
]
@@ -195,7 +204,7 @@ def bind(
195204
"""
196205
all_keys = [key.strip() for key in keys.split(",")]
197206
for key in all_keys:
198-
self.keys.setdefault(key, []).append(
207+
self.key_to_bindings.setdefault(key, []).append(
199208
Binding(
200209
key,
201210
action,
@@ -219,6 +228,6 @@ def get_bindings_for_key(self, key: str) -> list[Binding]:
219228
A list of bindings associated with the key.
220229
"""
221230
try:
222-
return self.keys[key]
231+
return self.key_to_bindings[key]
223232
except KeyError:
224233
raise NoBinding(f"No binding for {key}") from None

src/textual/dom.py

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

615615
new_bindings = BindingsMap().from_keys(keys)

src/textual/screen.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,22 +327,29 @@ def active_bindings(self) -> dict[str, ActiveBinding]:
327327
This property may be used to inspect current bindings.
328328
329329
Returns:
330-
A map of keys to a tuple containing (namespace, binding, enabled boolean).
330+
A map of keys to a tuple containing (NAMESPACE, BINDING, ENABLED).
331331
"""
332332

333333
bindings_map: dict[str, ActiveBinding] = {}
334334
for namespace, bindings in self._modal_binding_chain:
335335
for key, binding in bindings:
336+
# This will call the nodes `check_action` method.
336337
action_state = self.app._check_action_state(binding.action, namespace)
337338
if action_state is False:
339+
# An action_state of False indicates the action is disabled and not shown
340+
# Note that None has a different meaning, which is why there is an `is False`
341+
# rather than a truthy check.
338342
continue
339343
if existing_key_and_binding := bindings_map.get(key):
344+
# This key has already been bound
340345
_, existing_binding, _ = existing_key_and_binding
346+
# Replace priority bindings
341347
if binding.priority and not existing_binding.priority:
342348
bindings_map[key] = ActiveBinding(
343349
namespace, binding, bool(action_state)
344350
)
345351
else:
352+
# New binding
346353
bindings_map[key] = ActiveBinding(
347354
namespace, binding, bool(action_state)
348355
)

src/textual/widgets/_footer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,8 @@ def compose(self) -> ComposeResult:
164164
for (_, binding, enabled) in self.screen.active_bindings.values()
165165
if binding.show
166166
]
167-
action_to_bindings: defaultdict[str, list[tuple[Binding, bool]]] = defaultdict(
168-
list
169-
)
167+
action_to_bindings: defaultdict[str, list[tuple[Binding, bool]]]
168+
action_to_bindings = defaultdict(list)
170169
for binding, enabled in bindings:
171170
action_to_bindings[binding.action].append((binding, enabled))
172171

tests/test_binding.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ def test_bindings_get_key_spaced_list(more_bindings):
4545
def test_bindings_merge_simple(bindings):
4646
left = BindingsMap([BINDING1])
4747
right = BindingsMap([BINDING2])
48-
assert BindingsMap.merge([left, right]).keys == bindings.keys
48+
assert BindingsMap.merge([left, right]).key_to_bindings == bindings.keys
4949

5050

5151
def test_bindings_merge_overlap():
5252
left = BindingsMap([BINDING1])
5353
another_binding = Binding(
5454
"a", action="another_action", description="another_description"
5555
)
56-
assert BindingsMap.merge([left, BindingsMap([another_binding])]).keys == {
56+
assert BindingsMap.merge(
57+
[left, BindingsMap([another_binding])]
58+
).key_to_bindings == {
5759
"a": [
5860
Binding("a", action="action1", description="description1"),
5961
another_binding,

tests/test_binding_inheritance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class NoBindings(App[None]):
3939
async def test_just_app_no_bindings() -> None:
4040
"""An app with no bindings should have no bindings, other than the app's hard-coded ones."""
4141
async with NoBindings().run_test() as pilot:
42-
assert list(pilot.app._bindings.keys.keys()) == [
42+
assert list(pilot.app._bindings.key_to_bindings.keys()) == [
4343
"ctrl+c",
4444
"ctrl+backslash",
4545
]
@@ -64,7 +64,7 @@ class AlphaBinding(App[None]):
6464
async def test_just_app_alpha_binding() -> None:
6565
"""An app with a single binding should have just the one binding."""
6666
async with AlphaBinding().run_test() as pilot:
67-
assert sorted(pilot.app._bindings.keys.keys()) == sorted(
67+
assert sorted(pilot.app._bindings.key_to_bindings.keys()) == sorted(
6868
["ctrl+c", "ctrl+backslash", "a"]
6969
)
7070
assert pilot.app._bindings.get_bindings_for_key("ctrl+c")[0].priority is True
@@ -88,7 +88,7 @@ class LowAlphaBinding(App[None]):
8888
async def test_just_app_low_priority_alpha_binding() -> None:
8989
"""An app with a single low-priority binding should have just the one binding."""
9090
async with LowAlphaBinding().run_test() as pilot:
91-
assert sorted(pilot.app._bindings.keys.keys()) == sorted(
91+
assert sorted(pilot.app._bindings.key_to_bindings.keys()) == sorted(
9292
["ctrl+c", "ctrl+backslash", "a"]
9393
)
9494
assert pilot.app._bindings.get_bindings_for_key("ctrl+c")[0].priority is True

tests/test_dom.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,19 @@ class E(D):
9797
BINDINGS = [("e", "e", "e")]
9898

9999
a = A()
100-
assert list(a._bindings.keys.keys()) == ["a"]
100+
assert list(a._bindings.key_to_bindings.keys()) == ["a"]
101101

102102
b = B()
103-
assert list(b._bindings.keys.keys()) == ["a", "b"]
103+
assert list(b._bindings.key_to_bindings.keys()) == ["a", "b"]
104104

105105
c = C()
106-
assert list(c._bindings.keys.keys()) == ["c"]
106+
assert list(c._bindings.key_to_bindings.keys()) == ["c"]
107107

108108
d = D()
109-
assert not list(d._bindings.keys.keys())
109+
assert not list(d._bindings.key_to_bindings.keys())
110110

111111
e = E()
112-
assert list(e._bindings.keys.keys()) == ["e"]
112+
assert list(e._bindings.key_to_bindings.keys()) == ["e"]
113113

114114

115115
def test__get_default_css():

0 commit comments

Comments
 (0)