1313import typing as t
1414from collections .abc import MutableMapping
1515from distutils .version import LooseVersion
16+ from typing import Any , Dict , List , Optional , Union
1617
1718from . import exc
1819from ._compat import console_to_str , str_from_console
1920
21+ if t .TYPE_CHECKING :
22+ from libtmux .pane import Pane
23+ from libtmux .session import Session
24+ from libtmux .window import Window
25+
26+
2027logger = logging .getLogger (__name__ )
2128
2229
@@ -40,10 +47,10 @@ class EnvironmentMixin:
4047
4148 _add_option = None
4249
43- def __init__ (self , add_option = None ):
50+ def __init__ (self , add_option : Optional [ str ] = None ) -> None :
4451 self ._add_option = add_option
4552
46- def set_environment (self , name , value ) :
53+ def set_environment (self , name : str , value : str ) -> None :
4754 """
4855 Set environment ``$ tmux set-environment <name> <value>``.
4956
@@ -67,7 +74,7 @@ def set_environment(self, name, value):
6774 proc .stderr = proc .stderr [0 ]
6875 raise ValueError ("tmux set-environment stderr: %s" % proc .stderr )
6976
70- def unset_environment (self , name ) :
77+ def unset_environment (self , name : str ) -> None :
7178 """
7279 Unset environment variable ``$ tmux set-environment -u <name>``.
7380
@@ -88,7 +95,7 @@ def unset_environment(self, name):
8895 proc .stderr = proc .stderr [0 ]
8996 raise ValueError ("tmux set-environment stderr: %s" % proc .stderr )
9097
91- def remove_environment (self , name ) :
98+ def remove_environment (self , name : str ) -> None :
9299 """Remove environment variable ``$ tmux set-environment -r <name>``.
93100
94101 Parameters
@@ -108,7 +115,7 @@ def remove_environment(self, name):
108115 proc .stderr = proc .stderr [0 ]
109116 raise ValueError ("tmux set-environment stderr: %s" % proc .stderr )
110117
111- def show_environment (self ):
118+ def show_environment (self ) -> Optional [ Dict [ str , Union [ bool , str ]]] :
112119 """Show environment ``$ tmux show-environment -t [session]``.
113120
114121 Return dict of environment variables for the session.
@@ -139,7 +146,7 @@ def show_environment(self):
139146
140147 return vars_dict
141148
142- def getenv (self , name ) :
149+ def getenv (self , name : str ) -> Optional [ str ] :
143150 """Show environment variable ``$ tmux show-environment -t [session] <name>``.
144151
145152 Return the value of a specific variable if the name is specified.
@@ -214,7 +221,7 @@ class tmux_cmd:
214221 Renamed from ``tmux`` to ``tmux_cmd``.
215222 """
216223
217- def __init__ (self , * args , ** kwargs ):
224+ def __init__ (self , * args , ** kwargs ) -> None :
218225 tmux_bin = which (
219226 "tmux" ,
220227 default_paths = kwargs .get (
@@ -279,10 +286,10 @@ class TmuxMappingObject(MutableMapping):
279286 ================ ================================== ==============
280287 """
281288
282- def __getitem__ (self , key ) :
289+ def __getitem__ (self , key : str ) -> str :
283290 return self ._info [key ]
284291
285- def __setitem__ (self , key , value ) :
292+ def __setitem__ (self , key : str , value : str ) -> None :
286293 self ._info [key ] = value
287294 self .dirty = True
288295
@@ -297,10 +304,10 @@ def keys(self):
297304 def __iter__ (self ):
298305 return self ._info .__iter__ ()
299306
300- def __len__ (self ):
307+ def __len__ (self ) -> int :
301308 return len (self ._info .keys ())
302309
303- def __getattr__ (self , key ) :
310+ def __getattr__ (self , key : str ) -> str :
304311 try :
305312 return self ._info [self .formatter_prefix + key ]
306313 except KeyError :
@@ -337,7 +344,9 @@ class TmuxRelationalObject:
337344 ================ ================================== ==============
338345 """
339346
340- def find_where (self , attrs ):
347+ def find_where (
348+ self , attrs : Dict [str , str ]
349+ ) -> Optional [Union ["Pane" , "Window" , "Session" ]]:
341350 """Return object on first match.
342351
343352 .. versionchanged:: 0.4
@@ -349,7 +358,9 @@ def find_where(self, attrs):
349358 except IndexError :
350359 return None
351360
352- def where (self , attrs , first = False ):
361+ def where (
362+ self , attrs : Dict [str , str ], first : bool = False
363+ ) -> List [Union ["Session" , "Pane" , "Window" , t .Any ]]:
353364 """
354365 Return objects matching child objects properties.
355366
@@ -379,7 +390,7 @@ def by(val) -> bool:
379390 return target_children [0 ]
380391 return target_children
381392
382- def get_by_id (self , id ) :
393+ def get_by_id (self , id : str ) -> Optional [ Union [ "Pane" , "Window" , "Session" ]] :
383394 """
384395 Return object based on ``child_id_attribute``.
385396
@@ -638,7 +649,7 @@ def has_minimum_version(raises: bool = True) -> bool:
638649 return True
639650
640651
641- def session_check_name (session_name : str ):
652+ def session_check_name (session_name : str ) -> None :
642653 """
643654 Raises exception session name invalid, modeled after tmux function.
644655
0 commit comments