11import logging
22import subprocess
3+ from typing import cast
4+
35import attr
46
7+ from labgrid .resource .common import Resource
8+
59from ..binding import BindingError , BindingMixin
610from .exception import ExecutionError
711
@@ -21,17 +25,17 @@ class Driver(BindingMixin):
2125 - deactivate
2226 """
2327
24- def __attrs_post_init__ (self ):
28+ def __attrs_post_init__ (self ) -> None :
2529 super ().__attrs_post_init__ ()
2630 if self .target is None :
27- raise BindingError ("Drivers can only be created on a valid target" )
31+ raise BindingError ("Drivers can only be created on a valid target" ) # ty: ignore[too-many-positional-arguments]
2832
2933 logger_name = f"{ self .__class__ .__name__ } ({ self .target .name } )"
3034 if self .name :
3135 logger_name += f":{ self .name } "
3236 self .logger = logging .getLogger (logger_name )
3337
34- def get_priority (self , protocol ):
38+ def get_priority (self , protocol ) -> int :
3539 """Retrieve the priority for a given protocol
3640
3741 Arguments:
@@ -41,17 +45,17 @@ def get_priority(self, protocol):
4145 Int: value of the priority if it is found, 0 otherwise.
4246 """
4347 for cls in self .__class__ .__mro__ :
44- prios = getattr (cls , ' priorities' , {})
48+ prios = getattr (cls , " priorities" , {})
4549 # we found a matching parent priorities attribute with the matching protocol
4650 if prios and protocol in prios :
47- return prios . get ( protocol )
51+ return cast ( int , prios [ protocol ] )
4852 # If we find the parent protocol, set the priority to 0
4953 if cls .__name__ == protocol .__name__ :
5054 return 0
5155
5256 return 0
5357
54- def get_export_name (self ):
58+ def get_export_name (self ) -> str :
5559 """Get the name to be used for exported variables.
5660
5761 Falls back to the class name if the driver has no name.
@@ -60,29 +64,32 @@ def get_export_name(self):
6064 return self .name
6165 return self .__class__ .__name__
6266
63- def get_export_vars (self ):
67+ def get_export_vars (self ) -> dict [ str , str ] :
6468 """Get a dictionary of variables to be exported."""
6569 return {}
6670
6771 @property
68- def skip_deactivate_on_export (self ):
72+ def skip_deactivate_on_export (self ) -> bool :
6973 """Drivers are deactivated on export by default.
7074
7175 If the driver can handle external accesses even while active, it can
7276 return True here.
7377 """
7478 return False
7579
76- def get_bound_resources (self ):
80+ def get_bound_resources (self ) -> set [ Resource ] :
7781 """Return the bound resources for a driver
7882
7983 This recursively calls all suppliers and combines the sets of returned resources.
8084 """
81- res = set ()
85+ res : set [ Resource ] = set ()
8286 for supplier in self .suppliers :
8387 res |= supplier .get_bound_resources ()
8488 return res
8589
86- def check_file (filename , * , command_prefix = []):
87- if subprocess .call (command_prefix + ['test' , '-r' , filename ]) != 0 :
88- raise ExecutionError (f"File { filename } is not readable" )
90+
91+ def check_file (filename , * , command_prefix = None ) -> None :
92+ if command_prefix is None :
93+ command_prefix : list [str ] = []
94+ if subprocess .call (command_prefix + ["test" , "-r" , filename ]) != 0 :
95+ raise ExecutionError (f"File { filename } is not readable" ) # ty: ignore[too-many-positional-arguments]
0 commit comments