@@ -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
0 commit comments