Skip to content

Commit 4418260

Browse files
author
Tony Crisci
committed
type annotations for con class
1 parent 7bb7e34 commit 4418260

File tree

2 files changed

+45
-23
lines changed

2 files changed

+45
-23
lines changed

i3ipc/con.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import re
22
import sys
3-
from collections import deque
43
from .model import Rect, Gaps
4+
from . import replies
5+
from collections import deque
6+
from typing import List, Optional
57

68

79
class Con:
@@ -158,7 +160,7 @@ def __iter__(self):
158160
queue.extend(con.nodes)
159161
queue.extend(con.floating_nodes)
160162

161-
def root(self):
163+
def root(self) -> 'Con':
162164
"""Gets the root container.
163165
164166
:returns: The root container.
@@ -175,7 +177,7 @@ def root(self):
175177

176178
return con
177179

178-
def descendants(self):
180+
def descendants(self) -> List['Con']:
179181
"""Gets a list of all child containers for the container in
180182
breadth-first order.
181183
@@ -184,7 +186,7 @@ def descendants(self):
184186
"""
185187
return [c for c in self]
186188

187-
def descendents(self):
189+
def descendents(self) -> List['Con']:
188190
"""Gets a list of all child containers for the container in
189191
breadth-first order.
190192
@@ -197,7 +199,7 @@ def descendents(self):
197199
print('WARNING: descendents is deprecated. Use `descendants()` instead.', file=sys.stderr)
198200
return self.descendants()
199201

200-
def leaves(self):
202+
def leaves(self) -> List['Con']:
201203
"""Gets a list of leaf child containers for this container in
202204
breadth-first order. Leaf containers normally contain application
203205
windows.
@@ -213,25 +215,25 @@ def leaves(self):
213215

214216
return leaves
215217

216-
def command(self, command):
218+
def command(self, command: str) -> List[replies.CommandReply]:
217219
"""Runs a command on this container.
218220
219221
.. seealso:: https://i3wm.org/docs/userguide.html#list_of_commands
220222
221223
:returns: A list of replies for each command in the given command
222224
string.
223-
:rtype: list(CommandReply)
225+
:rtype: list(:class:`CommandReply <i3ipc.CommandReply>`)
224226
"""
225227
return self._conn.command('[con_id="{}"] {}'.format(self.id, command))
226228

227-
def command_children(self, command):
229+
def command_children(self, command: str) -> List[replies.CommandReply]:
228230
"""Runs a command on the immediate children of the currently selected
229231
container.
230232
231233
.. seealso:: https://i3wm.org/docs/userguide.html#list_of_commands
232234
233235
:returns: A list of replies for each command that was executed.
234-
:rtype: list(CommandReply)
236+
:rtype: list(:class:`CommandReply <i3ipc.CommandReply>`)
235237
"""
236238
if not len(self.nodes):
237239
return
@@ -242,11 +244,11 @@ def command_children(self, command):
242244

243245
self._conn.command(' '.join(commands))
244246

245-
def workspaces(self):
247+
def workspaces(self) -> List['Con']:
246248
"""Gets a list of workspace containers for this tree.
247249
248250
:returns: A list of workspace containers.
249-
:rtype: List of :class:`Con`.
251+
:rtype: list(:class:`Con`)
250252
"""
251253
workspaces = []
252254

@@ -261,7 +263,7 @@ def collect_workspaces(con):
261263
collect_workspaces(self.root())
262264
return workspaces
263265

264-
def find_focused(self):
266+
def find_focused(self) -> Optional['Con']:
265267
"""Finds the focused container under this container if it exists.
266268
267269
:returns: The focused container if it exists.
@@ -273,7 +275,7 @@ def find_focused(self):
273275
except StopIteration:
274276
return None
275277

276-
def find_by_id(self, id):
278+
def find_by_id(self, id: int) -> Optional['Con']:
277279
"""Finds a container with the given container id under this node.
278280
279281
:returns: The container with this container id if it exists.
@@ -285,7 +287,7 @@ def find_by_id(self, id):
285287
except StopIteration:
286288
return None
287289

288-
def find_by_window(self, window):
290+
def find_by_window(self, window: int) -> Optional['Con']:
289291
"""Finds a container with the given window id under this node.
290292
291293
:returns: The container with this window id if it exists.
@@ -297,7 +299,7 @@ def find_by_window(self, window):
297299
except StopIteration:
298300
return None
299301

300-
def find_by_role(self, pattern):
302+
def find_by_role(self, pattern: str) -> List['Con']:
301303
"""Finds all the containers under this node with a window role that
302304
matches the given regex pattern.
303305
@@ -307,7 +309,7 @@ def find_by_role(self, pattern):
307309
"""
308310
return [c for c in self if c.window_role and re.search(pattern, c.window_role)]
309311

310-
def find_named(self, pattern):
312+
def find_named(self, pattern: str) -> List['Con']:
311313
"""Finds all the containers under this node with a name that
312314
matches the given regex pattern.
313315
@@ -317,7 +319,7 @@ def find_named(self, pattern):
317319
"""
318320
return [c for c in self if c.name and re.search(pattern, c.name)]
319321

320-
def find_titled(self, pattern):
322+
def find_titled(self, pattern: str) -> List['Con']:
321323
"""Finds all the containers under this node with a window title that
322324
matches the given regex pattern.
323325
@@ -327,7 +329,7 @@ def find_titled(self, pattern):
327329
"""
328330
return [c for c in self if c.window_title and re.search(pattern, c.window_title)]
329331

330-
def find_classed(self, pattern):
332+
def find_classed(self, pattern: str) -> List['Con']:
331333
"""Finds all the containers under this node with a window class that
332334
matches the given regex pattern.
333335
@@ -337,7 +339,7 @@ def find_classed(self, pattern):
337339
"""
338340
return [c for c in self if c.window_class and re.search(pattern, c.window_class)]
339341

340-
def find_instanced(self, pattern):
342+
def find_instanced(self, pattern: str) -> List['Con']:
341343
"""Finds all the containers under this node with a window instance that
342344
matches the given regex pattern.
343345
@@ -347,7 +349,7 @@ def find_instanced(self, pattern):
347349
"""
348350
return [c for c in self if c.window_instance and re.search(pattern, c.window_instance)]
349351

350-
def find_marked(self, pattern=".*"):
352+
def find_marked(self, pattern: str = ".*") -> List['Con']:
351353
"""Finds all the containers under this node with a mark that
352354
matches the given regex pattern.
353355
@@ -358,7 +360,7 @@ def find_marked(self, pattern=".*"):
358360
pattern = re.compile(pattern)
359361
return [c for c in self if any(pattern.search(mark) for mark in c.marks)]
360362

361-
def find_fullscreen(self):
363+
def find_fullscreen(self) -> List['Con']:
362364
"""Finds all the containers under this node that are in fullscreen
363365
mode.
364366
@@ -367,7 +369,7 @@ def find_fullscreen(self):
367369
"""
368370
return [c for c in self if c.type == 'con' and c.fullscreen_mode]
369371

370-
def workspace(self):
372+
def workspace(self) -> Optional['Con']:
371373
"""Finds the workspace container for this node if this container is at
372374
or below the workspace level.
373375
@@ -387,7 +389,7 @@ def workspace(self):
387389

388390
return ret
389391

390-
def scratchpad(self):
392+
def scratchpad(self) -> 'Con':
391393
"""Finds the scratchpad container.
392394
393395
:returns: The scratchpad container.

i3ipc/model.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
class Rect:
2+
"""Used by other classes to represent rectangular position and dimensions.
3+
4+
:ivar x: The x coordinate.
5+
:vartype x: int
6+
:ivar y: The y coordinate.
7+
:vartype y: int
8+
:ivar height: The height of the rectangle.
9+
:vartype height: int
10+
:ivar width: The width of the rectangle.
11+
:vartype width: int
12+
"""
13+
214
def __init__(self, data):
315
self.x = data['x']
416
self.y = data['y']
@@ -7,6 +19,14 @@ def __init__(self, data):
719

820

921
class Gaps:
22+
"""For forks that have useless gaps, the dimension of the gaps.
23+
24+
:ivar inner: The inner gaps.
25+
:vartype inner: int
26+
:ivar outer: The outer gaps.
27+
:vartype outer: int
28+
"""
29+
1030
def __init__(self, data):
1131
self.inner = data['inner']
1232
self.outer = data['outer']

0 commit comments

Comments
 (0)