Skip to content

Commit 92e31ec

Browse files
committed
Add type annotations to runfiles library
1 parent f400682 commit 92e31ec

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

python/runfiles/runfiles.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,20 @@
6868
import os
6969
import posixpath
7070

71+
from typing import Callable, Dict, Optional, Tuple, Union
7172

7273
def CreateManifestBased(manifest_path):
74+
# type: (str) -> _Runfiles
7375
return _Runfiles(_ManifestBased(manifest_path))
7476

7577

7678
def CreateDirectoryBased(runfiles_dir_path):
79+
# type: (str) -> _Runfiles
7780
return _Runfiles(_DirectoryBased(runfiles_dir_path))
7881

7982

8083
def Create(env=None):
84+
# type: (Optional[Dict[str, str]]) -> Optional[_Runfiles]
8185
"""Returns a new `Runfiles` instance.
8286
8387
The returned object is either:
@@ -120,9 +124,11 @@ class _Runfiles(object):
120124
"""
121125

122126
def __init__(self, strategy):
127+
# type: (Union[_ManifestBased, _DirectoryBased]) -> None
123128
self._strategy = strategy
124129

125130
def Rlocation(self, path):
131+
# type: (str) -> Optional[str]
126132
"""Returns the runtime path of a runfile.
127133
128134
Runfiles are data-dependencies of Bazel-built binaries and tests.
@@ -162,6 +168,7 @@ def Rlocation(self, path):
162168
return self._strategy.RlocationChecked(path)
163169

164170
def EnvVars(self):
171+
# type: () -> Dict[str, str]
165172
"""Returns environment variables for subprocesses.
166173
167174
The caller should set the returned key-value pairs in the environment of
@@ -179,6 +186,7 @@ class _ManifestBased(object):
179186
"""`Runfiles` strategy that parses a runfiles-manifest to look up runfiles."""
180187

181188
def __init__(self, path):
189+
# type: (str) -> None
182190
if not path:
183191
raise ValueError()
184192
if not isinstance(path, str):
@@ -187,10 +195,12 @@ def __init__(self, path):
187195
self._runfiles = _ManifestBased._LoadRunfiles(path)
188196

189197
def RlocationChecked(self, path):
198+
# type: (str) -> Optional[str]
190199
return self._runfiles.get(path)
191200

192201
@staticmethod
193202
def _LoadRunfiles(path):
203+
# type: (str) -> Dict[str, str]
194204
"""Loads the runfiles manifest."""
195205
result = {}
196206
with open(path, "r") as f:
@@ -205,6 +215,7 @@ def _LoadRunfiles(path):
205215
return result
206216

207217
def _GetRunfilesDir(self):
218+
# type: () -> str
208219
if self._path.endswith("/MANIFEST") or self._path.endswith("\\MANIFEST"):
209220
return self._path[: -len("/MANIFEST")]
210221
elif self._path.endswith(".runfiles_manifest"):
@@ -213,6 +224,7 @@ def _GetRunfilesDir(self):
213224
return ""
214225

215226
def EnvVars(self):
227+
# type: () -> Dict[str, str]
216228
directory = self._GetRunfilesDir()
217229
return {
218230
"RUNFILES_MANIFEST_FILE": self._path,
@@ -227,19 +239,23 @@ class _DirectoryBased(object):
227239
"""`Runfiles` strategy that appends runfiles paths to the runfiles root."""
228240

229241
def __init__(self, path):
242+
# type: (str) -> None
230243
if not path:
231244
raise ValueError()
232245
if not isinstance(path, str):
233246
raise TypeError()
234247
self._runfiles_root = path
235248

236249
def RlocationChecked(self, path):
250+
# type: (str) -> str
251+
237252
# Use posixpath instead of os.path, because Bazel only creates a runfiles
238253
# tree on Unix platforms, so `Create()` will only create a directory-based
239254
# runfiles strategy on those platforms.
240255
return posixpath.join(self._runfiles_root, path)
241256

242257
def EnvVars(self):
258+
# type: () -> Dict[str, str]
243259
return {
244260
"RUNFILES_DIR": self._runfiles_root,
245261
# TODO(laszlocsomor): remove JAVA_RUNFILES once the Java launcher can
@@ -251,6 +267,7 @@ def EnvVars(self):
251267
def _PathsFrom(
252268
argv0, runfiles_mf, runfiles_dir, is_runfiles_manifest, is_runfiles_directory
253269
):
270+
# type: (str, str, str, Callable[[str], bool], Callable[[str], bool]) -> Tuple[str, str]
254271
"""Discover runfiles manifest and runfiles directory paths.
255272
256273
Args:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy