Skip to content
This repository was archived by the owner on Mar 8, 2020. It is now read-only.

Commit e8483d9

Browse files
authored
Merge pull request #135 from warenlg/master
Add type hints
2 parents 3139a87 + d91270d commit e8483d9

File tree

4 files changed

+17
-23
lines changed

4 files changed

+17
-23
lines changed

bblfsh/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RoleSearchException(Exception):
1515
pass
1616

1717

18-
def role_id(role_name):
18+
def role_id(role_name: str) -> int:
1919
try:
2020
name = DESCRIPTOR.enum_types_by_name["Role"].values_by_name[role_name].number
2121
except KeyError:
@@ -24,7 +24,7 @@ def role_id(role_name):
2424
return name
2525

2626

27-
def role_name(role_id):
27+
def role_name(role_id: int) -> str:
2828
try:
2929
id_ = DESCRIPTOR.enum_types_by_name["Role"].values_by_number[role_id].name
3030
except KeyError:

bblfsh/__main__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
import sys
33

4+
import bblfsh
45
from bblfsh.pyuast import filter
56

67
from bblfsh.client import BblfshClient
@@ -28,7 +29,7 @@ def setup():
2829
args = parser.parse_args()
2930
return args
3031

31-
def run_query(root, query, mapn, as_array):
32+
def run_query(root: bblfsh.Node, query: str, mapn: str, as_array: bool) -> None:
3233
result = list(filter(root, query))
3334

3435
if not result:

bblfsh/client.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
import grpc
55

6-
from bblfsh.aliases import (ParseRequest, NativeParseRequest, VersionRequest,
7-
ProtocolServiceStub, SupportedLanguagesRequest, SupportedLanguagesResponse)
6+
from bblfsh.aliases import (ParseRequest, ParseResponse, NativeParseRequest, NativeParseResponse,
7+
VersionRequest, ProtocolServiceStub, SupportedLanguagesRequest, SupportedLanguagesResponse)
88
from bblfsh.sdkversion import VERSION
99

1010
# The following two insertions fix the broken pb import paths
@@ -22,33 +22,33 @@ class BblfshClient(object):
2222
Babelfish gRPC client. Currently it is only capable of fetching UASTs.
2323
"""
2424

25-
def __init__(self, endpoint):
25+
def __init__(self, endpoint: str):
2626
"""
2727
Initializes a new instance of BblfshClient.
2828
2929
:param endpoint: The address of the Babelfish server, \
3030
for example "0.0.0.0:9432"
31-
:type endpoint: str
3231
"""
3332
self._channel = grpc.insecure_channel(endpoint)
3433
self._stub = ProtocolServiceStub(self._channel)
3534

3635
@staticmethod
37-
def _check_utf8(text):
36+
def _check_utf8(text: str) -> None:
3837
try:
3938
text.decode("utf-8")
4039
except UnicodeDecodeError:
4140
raise NonUTF8ContentException("Content must be UTF-8, ASCII or Base64 encoded")
4241

4342
@staticmethod
44-
def _get_contents(contents, filename):
43+
def _get_contents(contents: str, filename: str) -> str:
4544
if contents is None:
4645
with open(filename, "rb") as fin:
4746
contents = fin.read()
4847
BblfshClient._check_utf8(contents)
4948
return contents
5049

51-
def parse(self, filename, language=None, contents=None, timeout=None):
50+
def parse(self, filename: str, language: str=None, contents: str=None,
51+
timeout: float=None) -> ParseResponse:
5252
"""
5353
Queries the Babelfish server and receives the UAST response for the specified
5454
file.
@@ -61,10 +61,6 @@ def parse(self, filename, language=None, contents=None, timeout=None):
6161
:param contents: The contents of the file. IF None, it is read from \
6262
filename.
6363
:param timeout: The request timeout in seconds.
64-
:type filename: str
65-
:type language: str
66-
:type contents: str
67-
:type timeout: float
6864
:return: UAST object.
6965
"""
7066

@@ -74,7 +70,8 @@ def parse(self, filename, language=None, contents=None, timeout=None):
7470
language=self._scramble_language(language))
7571
return self._stub.Parse(request, timeout=timeout)
7672

77-
def native_parse(self, filename, language=None, contents=None, timeout=None):
73+
def native_parse(self, filename: str, language: str=None, contents: str=None,
74+
timeout: float=None) -> NativeParseResponse:
7875
"""
7976
Queries the Babelfish server and receives the native AST response for the specified
8077
file.
@@ -87,17 +84,13 @@ def native_parse(self, filename, language=None, contents=None, timeout=None):
8784
:param contents: The contents of the file. IF None, it is read from \
8885
filename.
8986
:param timeout: The request timeout in seconds.
90-
:type filename: str
91-
:type language: str
92-
:type contents: str
93-
:type timeout: float
9487
:return: Native AST object.
9588
"""
9689

9790
contents = self._get_contents(contents, filename)
9891
request = NativeParseRequest(filename=os.path.basename(filename),
99-
content=contents,
100-
language=self._scramble_language(language))
92+
content=contents,
93+
language=self._scramble_language(language))
10194
return self._stub.NativeParse(request, timeout=timeout)
10295

10396
def supported_languages(self):
@@ -114,7 +107,7 @@ def version(self):
114107
return self._stub.Version(VersionRequest())
115108

116109
@staticmethod
117-
def _scramble_language(lang):
110+
def _scramble_language(lang: str) -> str:
118111
if lang is None:
119112
return None
120113
lang = lang.lower()

bblfsh/launcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import docker
66

77

8-
def ensure_bblfsh_is_running():
8+
def ensure_bblfsh_is_running() -> bool:
99
log = logging.getLogger("bblfsh")
1010
try:
1111
client = docker.from_env(version="auto")

0 commit comments

Comments
 (0)