1313import typing as t
1414from collections .abc import MutableMapping
1515from distutils .version import LooseVersion
16- from typing import Any , Dict , List , Optional , Union
16+ from typing import Any , Dict , Generic , List , Optional , TypeVar , Union , overload
1717
1818from . import exc
1919from ._compat import console_to_str , str_from_console
@@ -315,8 +315,11 @@ def __getattr__(self, key: str) -> str:
315315 raise AttributeError (f"{ self .__class__ } has no property { key } " )
316316
317317
318- class TmuxRelationalObject :
318+ O = TypeVar ("O" , "Pane" , "Window" , "Session" )
319+ D = TypeVar ("D" , "PaneDict" , "WindowDict" , "SessionDict" )
319320
321+
322+ class TmuxRelationalObject (Generic [O , D ]):
320323 """Base Class for managing tmux object child entities. .. # NOQA
321324
322325 Manages collection of child objects (a :class:`Server` has a collection of
@@ -345,6 +348,9 @@ class TmuxRelationalObject:
345348 ================ ================================== ==============
346349 """
347350
351+ children : t .List [O ]
352+ child_id_attribute : str
353+
348354 def find_where (
349355 self , attrs : Dict [str , str ]
350356 ) -> Optional [Union ["Pane" , "Window" , "Session" ]]:
@@ -359,9 +365,20 @@ def find_where(
359365 except IndexError :
360366 return None
361367
362- def where (
363- self , attrs : Dict [str , str ], first : bool = False
364- ) -> List [Union ["Session" , "Pane" , "Window" , t .Any ]]:
368+ @overload
369+ def where (self , attrs : Dict [str , str ], first : t .Literal [True ]) -> O :
370+ ...
371+
372+ @overload
373+ def where (self , attrs : Dict [str , str ], first : t .Literal [False ]) -> t .List [O ]:
374+ ...
375+
376+ @overload
377+ def where (self , attrs : Dict [str , str ]) -> t .List [O ]:
378+ ...
379+
380+ def where (self , attrs : Dict [str , str ], first : bool = False ) -> t .Union [List [O ], O ]:
381+ # ) -> List[Union["Session", "Pane", "Window", t.Any]]:
365382 """
366383 Return objects matching child objects properties.
367384
@@ -376,7 +393,7 @@ def where(
376393 """
377394
378395 # from https://github.com/serkanyersen/underscore.py
379- def by (val : WindowDict ) -> bool :
396+ def by (val : D ) -> bool :
380397 for key in attrs .keys ():
381398 try :
382399 if attrs [key ] != val [key ]:
0 commit comments