6868import os
6969import posixpath
7070
71+ if False :
72+ # Mypy needs these symbols imported, but since they only exist in python 3.5+,
73+ # this import may fail at runtime. Luckily mypy can follow this conditional import.
74+ from typing import Callable , Dict , Optional , Tuple , Union
7175
7276def CreateManifestBased (manifest_path ):
77+ # type: (str) -> _Runfiles
7378 return _Runfiles (_ManifestBased (manifest_path ))
7479
7580
7681def CreateDirectoryBased (runfiles_dir_path ):
82+ # type: (str) -> _Runfiles
7783 return _Runfiles (_DirectoryBased (runfiles_dir_path ))
7884
7985
8086def Create (env = None ):
87+ # type: (Optional[Dict[str, str]]) -> Optional[_Runfiles]
8188 """Returns a new `Runfiles` instance.
8289
8390 The returned object is either:
@@ -120,9 +127,11 @@ class _Runfiles(object):
120127 """
121128
122129 def __init__ (self , strategy ):
130+ # type: (Union[_ManifestBased, _DirectoryBased]) -> None
123131 self ._strategy = strategy
124132
125133 def Rlocation (self , path ):
134+ # type: (str) -> Optional[str]
126135 """Returns the runtime path of a runfile.
127136
128137 Runfiles are data-dependencies of Bazel-built binaries and tests.
@@ -162,6 +171,7 @@ def Rlocation(self, path):
162171 return self ._strategy .RlocationChecked (path )
163172
164173 def EnvVars (self ):
174+ # type: () -> Dict[str, str]
165175 """Returns environment variables for subprocesses.
166176
167177 The caller should set the returned key-value pairs in the environment of
@@ -179,6 +189,7 @@ class _ManifestBased(object):
179189 """`Runfiles` strategy that parses a runfiles-manifest to look up runfiles."""
180190
181191 def __init__ (self , path ):
192+ # type: (str) -> None
182193 if not path :
183194 raise ValueError ()
184195 if not isinstance (path , str ):
@@ -187,10 +198,12 @@ def __init__(self, path):
187198 self ._runfiles = _ManifestBased ._LoadRunfiles (path )
188199
189200 def RlocationChecked (self , path ):
201+ # type: (str) -> Optional[str]
190202 return self ._runfiles .get (path )
191203
192204 @staticmethod
193205 def _LoadRunfiles (path ):
206+ # type: (str) -> Dict[str, str]
194207 """Loads the runfiles manifest."""
195208 result = {}
196209 with open (path , "r" ) as f :
@@ -205,6 +218,7 @@ def _LoadRunfiles(path):
205218 return result
206219
207220 def _GetRunfilesDir (self ):
221+ # type: () -> str
208222 if self ._path .endswith ("/MANIFEST" ) or self ._path .endswith ("\\ MANIFEST" ):
209223 return self ._path [: - len ("/MANIFEST" )]
210224 elif self ._path .endswith (".runfiles_manifest" ):
@@ -213,6 +227,7 @@ def _GetRunfilesDir(self):
213227 return ""
214228
215229 def EnvVars (self ):
230+ # type: () -> Dict[str, str]
216231 directory = self ._GetRunfilesDir ()
217232 return {
218233 "RUNFILES_MANIFEST_FILE" : self ._path ,
@@ -227,19 +242,23 @@ class _DirectoryBased(object):
227242 """`Runfiles` strategy that appends runfiles paths to the runfiles root."""
228243
229244 def __init__ (self , path ):
245+ # type: (str) -> None
230246 if not path :
231247 raise ValueError ()
232248 if not isinstance (path , str ):
233249 raise TypeError ()
234250 self ._runfiles_root = path
235251
236252 def RlocationChecked (self , path ):
253+ # type: (str) -> str
254+
237255 # Use posixpath instead of os.path, because Bazel only creates a runfiles
238256 # tree on Unix platforms, so `Create()` will only create a directory-based
239257 # runfiles strategy on those platforms.
240258 return posixpath .join (self ._runfiles_root , path )
241259
242260 def EnvVars (self ):
261+ # type: () -> Dict[str, str]
243262 return {
244263 "RUNFILES_DIR" : self ._runfiles_root ,
245264 # TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can
@@ -251,6 +270,7 @@ def EnvVars(self):
251270def _PathsFrom (
252271 argv0 , runfiles_mf , runfiles_dir , is_runfiles_manifest , is_runfiles_directory
253272):
273+ # type: (str, str, str, Callable[[str], bool], Callable[[str], bool]) -> Tuple[str, str]
254274 """Discover runfiles manifest and runfiles directory paths.
255275
256276 Args:
0 commit comments