Skip to content

Commit f0c395f

Browse files
committed
Lint and typecheck.
1 parent 498da4e commit f0c395f

3 files changed

Lines changed: 20 additions & 17 deletions

File tree

src/agent.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from __future__ import annotations
2+
from collections.abc import Callable
13
import os
2-
from typing import Callable, Optional, Union
4+
from typing import Optional, Union
35
from pathlib import Path
46

57
import openai
@@ -67,7 +69,7 @@ def read_api_key(self, filename: Optional[Path]=None) -> str:
6769
settings: Settings = Settings()
6870
if settings.contains('openai.api_key'):
6971
if key := settings.get_string('openai.api_key'):
70-
return key
72+
return str(key)
7173

7274
# If the settings don't exist, contain the key, or the key is empty,
7375
# check the environment variable.
@@ -106,7 +108,7 @@ def get_model(self) -> str:
106108
if model := settings.get_string('openai.model'):
107109
# Check that is a valid model by querying the OpenAI API.
108110
if self.is_valid_model(model):
109-
return model
111+
return str(model)
110112
# Return a valid, default model.
111113
assert self.is_valid_model('text-davinci-003')
112114
return 'text-davinci-003'
@@ -119,7 +121,7 @@ def get_token_count(self) -> int:
119121
if settings.contains('openai.max_tokens'):
120122
# Check that the value is not None.
121123
if (max_tokens := settings.get_integer('openai.max_tokens')) is not None:
122-
return max_tokens
124+
return int(max_tokens)
123125
return 1_024
124126

125127
def instruction_list(self, function: Union[LowLevelILFunction,
@@ -161,7 +163,7 @@ def generate_query(self, function: Union[Function,
161163
return prompt
162164

163165
def generate_rename_variable_query(self,
164-
instruction: HighLevelILInstruction) -> Optional[str]:
166+
instruction: HighLevelILInstruction) -> str:
165167
'''Generates a query string given a BNIL instruction. Returns the query
166168
as a string.
167169
'''

src/entry.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
from pathlib import Path
22
from binaryninja import BinaryView, Function
3-
from binaryninja.highlevelil import HighLevelILInstruction, HighLevelILVarInit, \
4-
HighLevelILFunction
3+
from binaryninja.highlevelil import HighLevelILInstruction, HighLevelILVarInit
54
from binaryninja.log import log_error
65
from . agent import Agent
76

87
API_KEY_PATH = Path.home() / Path('.openai/api_key.txt')
98

10-
def check_function(bv: BinaryView, func: Function) -> bool:
9+
def check_function(bv: BinaryView, func: Function) -> None:
1110
agent: Agent = Agent(
1211
bv=bv,
1312
path_to_api_key=API_KEY_PATH
1413
)
1514
query: str = agent.generate_query(func)
1615
agent.send_query(query)
1716

18-
def rename_variable(bv: BinaryView, instruction: HighLevelILInstruction) -> bool:
17+
def rename_variable(bv: BinaryView, instruction: HighLevelILInstruction) -> None:
1918

2019
if not isinstance(instruction, HighLevelILVarInit):
2120
log_error(f'Instruction must be of type HighLevelILVarInit, got type: ' \
2221
f'{type(instruction)}')
23-
return False
22+
return
2423

2524
agent: Agent = Agent(
2625
bv=bv,

src/query.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
from __future__ import annotations
2+
from collections.abc import Callable
3+
from typing import Optional
14
import openai
2-
from typing import Callable
35
from binaryninja.plugin import BackgroundTaskThread
4-
from binaryninja.log import log_debug
6+
from binaryninja.log import log_debug, log_info
57

68
class Query(BackgroundTaskThread):
79

810
def __init__(self, query_string: str, model: str,
9-
max_token_count: int, callback_function: Callable=None) -> None:
11+
max_token_count: int, callback_function: Optional[Callable]=None) -> None:
1012
BackgroundTaskThread.__init__(self,
1113
initial_progress_text="",
1214
can_cancel=False)
@@ -20,16 +22,16 @@ def run(self) -> None:
2022

2123
log_debug(f'Sending query: {self.query_string}')
2224

23-
response: str = openai.Completion.create(
25+
response = openai.Completion.create(
2426
model=self.model,
2527
prompt=self.query_string,
2628
max_tokens=self.max_token_count,
2729
)
2830
# Get the response text.
29-
response: str = response.choices[0].text
31+
result: str = response.choices[0].text
3032
# If there is a callback, do something with it.
3133
if self.callback:
32-
self.callback(response)
34+
self.callback(result)
3335
# Otherwise, assume we just want to log it.
3436
else:
35-
log_info(response)
37+
log_info(result)

0 commit comments

Comments
 (0)