Skip to content

Commit fa390b8

Browse files
adambaloghbalogh.adam@icloud.com
andauthored
Fix mypy issues (#192)
* fix mypy issues * revert --------- Co-authored-by: balogh.adam@icloud.com <adambalogh@mac.mynetworksettings.com>
1 parent ad9a21f commit fa390b8

13 files changed

Lines changed: 41 additions & 35 deletions

examples/alpha/create_workflow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import opengradient as og
44

5-
alpha = og.Alpha(private_key=os.environ.get("OG_PRIVATE_KEY"))
5+
alpha = og.Alpha(private_key=os.environ["OG_PRIVATE_KEY"])
66

77
# Define model input
88
input_query = og.HistoricalInputQuery(

examples/alpha/run_embeddings_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import opengradient as og
44

5-
alpha = og.Alpha(private_key=os.environ.get("OG_PRIVATE_KEY"))
5+
alpha = og.Alpha(private_key=os.environ["OG_PRIVATE_KEY"])
66

77
queries = [
88
"how much protein should a female eat",

examples/alpha/run_inference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import opengradient as og
44

5-
alpha = og.Alpha(private_key=os.environ.get("OG_PRIVATE_KEY"))
5+
alpha = og.Alpha(private_key=os.environ["OG_PRIVATE_KEY"])
66

77
inference_result = alpha.infer(
88
model_cid="hJD2Ja3akZFt1A2LT-D_1oxOCz_OtuGYw4V9eE1m39M",

examples/alpha/use_workflow.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import opengradient as og
44

5-
alpha = og.Alpha(private_key=os.environ.get("OG_PRIVATE_KEY"))
5+
alpha = og.Alpha(private_key=os.environ["OG_PRIVATE_KEY"])
66

77
model_output = alpha.read_workflow_result(
88
# This is the workflow contract address that you previously deployed

examples/langchain_react_agent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import opengradient as og
1717

18-
private_key = os.environ.get("OG_PRIVATE_KEY")
18+
private_key = os.environ["OG_PRIVATE_KEY"]
1919

2020
# One-time Permit2 approval for OPG spending (idempotent)
2121
llm_client = og.LLM(private_key=private_key)

examples/twins_chat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import opengradient as og
77

8-
twins = og.Twins(api_key=os.environ.get("TWINS_API_KEY"))
8+
twins = og.Twins(api_key=os.environ["TWINS_API_KEY"])
99

1010
# Chat with Elon Musk
1111
print("--------------------------------")
@@ -18,7 +18,7 @@
1818
messages=[{"role": "user", "content": "What do you think about AI?"}],
1919
max_tokens=1000,
2020
)
21-
print(f"Elon: {elon.chat_output['content']}")
21+
print(f"Elon: {elon.chat_output['content'] if elon.chat_output else None}")
2222

2323
# Chat with Donald Trump
2424
print("--------------------------------")
@@ -31,4 +31,4 @@
3131
messages=[{"role": "user", "content": "What's your plan for America?"}],
3232
max_tokens=1000,
3333
)
34-
print(f"Trump: {trump.chat_output['content']}")
34+
print(f"Trump: {trump.chat_output['content'] if trump.chat_output else None}")

src/opengradient/client/_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ def get_abi(abi_name: str) -> dict:
2020
"""Returns the ABI for the requested contract."""
2121
abi_path = _ABI_DIR / abi_name
2222
with open(abi_path, "r") as f:
23-
return json.load(f)
23+
result: dict = json.load(f)
24+
return result
2425

2526

2627
def get_bin(bin_name: str) -> str:

src/opengradient/client/alpha.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from web3.logs import DISCARD
1919

2020
from ..types import HistoricalInputQuery, InferenceMode, InferenceResult, ModelOutput, SchedulerParams
21-
from ._conversions import convert_array_to_model_output, convert_to_model_input, convert_to_model_output
21+
from ._conversions import convert_array_to_model_output, convert_to_model_input, convert_to_model_output # type: ignore[attr-defined]
2222
from ._utils import get_abi, get_bin, run_with_retry
2323

2424
DEFAULT_RPC_URL = "https://ogevmdevnet.opengradient.ai"
@@ -57,8 +57,8 @@ def __init__(
5757
self._wallet_account: LocalAccount = self._blockchain.eth.account.from_key(private_key)
5858
self._inference_hub_contract_address = inference_contract_address
5959
self._api_url = api_url
60-
self._inference_abi = None
61-
self._precompile_abi = None
60+
self._inference_abi: Optional[dict] = None
61+
self._precompile_abi: Optional[dict] = None
6262

6363
@property
6464
def inference_abi(self) -> dict:
@@ -98,8 +98,12 @@ def infer(
9898
"""
9999

100100
def execute_transaction():
101-
contract = self._blockchain.eth.contract(address=self._inference_hub_contract_address, abi=self.inference_abi)
102-
precompile_contract = self._blockchain.eth.contract(address=PRECOMPILE_CONTRACT_ADDRESS, abi=self.precompile_abi)
101+
contract = self._blockchain.eth.contract(
102+
address=Web3.to_checksum_address(self._inference_hub_contract_address), abi=self.inference_abi
103+
)
104+
precompile_contract = self._blockchain.eth.contract(
105+
address=Web3.to_checksum_address(PRECOMPILE_CONTRACT_ADDRESS), abi=self.precompile_abi
106+
)
103107

104108
inference_mode_uint8 = inference_mode.value
105109
converted_model_input = convert_to_model_input(model_input)
@@ -122,7 +126,8 @@ def execute_transaction():
122126

123127
return InferenceResult(tx_hash.hex(), model_output)
124128

125-
return run_with_retry(execute_transaction, max_retries)
129+
result: InferenceResult = run_with_retry(execute_transaction, max_retries)
130+
return result
126131

127132
def _send_tx_with_revert_handling(self, run_function):
128133
"""
@@ -161,7 +166,7 @@ def _send_tx_with_revert_handling(self, run_function):
161166
}
162167
)
163168

164-
signed_tx = self._wallet_account.sign_transaction(transaction)
169+
signed_tx = self._wallet_account.sign_transaction(transaction) # type: ignore[arg-type]
165170
tx_hash = self._blockchain.eth.send_raw_transaction(signed_tx.raw_transaction)
166171
tx_receipt = self._blockchain.eth.wait_for_transaction_receipt(tx_hash, timeout=INFERENCE_TX_TIMEOUT)
167172

@@ -176,7 +181,7 @@ def _send_tx_with_revert_handling(self, run_function):
176181

177182
return tx_hash, tx_receipt
178183

179-
def _get_inference_result_from_node(self, inference_id: str, inference_mode: InferenceMode) -> Dict:
184+
def _get_inference_result_from_node(self, inference_id: str, inference_mode: InferenceMode) -> Optional[Dict]:
180185
"""
181186
Get the inference result from node.
182187
@@ -317,7 +322,7 @@ def deploy_transaction():
317322

318323
return tx_receipt.contractAddress
319324

320-
contract_address = run_with_retry(deploy_transaction)
325+
contract_address: str = run_with_retry(deploy_transaction)
321326

322327
if scheduler_params:
323328
self._register_with_scheduler(contract_address, scheduler_params)
@@ -343,7 +348,7 @@ def _register_with_scheduler(self, contract_address: str, scheduler_params: Sche
343348

344349
# Scheduler contract address
345350
scheduler_address = DEFAULT_SCHEDULER_ADDRESS
346-
scheduler_contract = self._blockchain.eth.contract(address=scheduler_address, abi=scheduler_abi)
351+
scheduler_contract = self._blockchain.eth.contract(address=Web3.to_checksum_address(scheduler_address), abi=scheduler_abi)
347352

348353
try:
349354
# Register the workflow with the scheduler
@@ -359,7 +364,7 @@ def _register_with_scheduler(self, contract_address: str, scheduler_params: Sche
359364
}
360365
)
361366

362-
signed_scheduler_tx = self._wallet_account.sign_transaction(scheduler_tx)
367+
signed_scheduler_tx = self._wallet_account.sign_transaction(scheduler_tx) # type: ignore[arg-type]
363368
scheduler_tx_hash = self._blockchain.eth.send_raw_transaction(signed_scheduler_tx.raw_transaction)
364369
self._blockchain.eth.wait_for_transaction_receipt(scheduler_tx_hash, timeout=REGULAR_TX_TIMEOUT)
365370
except Exception as e:
@@ -388,7 +393,8 @@ def read_workflow_result(self, contract_address: str) -> ModelOutput:
388393
# Get the result
389394
result = contract.functions.getInferenceResult().call()
390395

391-
return convert_array_to_model_output(result)
396+
output: ModelOutput = convert_array_to_model_output(result)
397+
return output
392398

393399
def run_workflow(self, contract_address: str) -> ModelOutput:
394400
"""
@@ -423,17 +429,18 @@ def run_workflow(self, contract_address: str) -> ModelOutput:
423429
}
424430
)
425431

426-
signed_txn = self._wallet_account.sign_transaction(transaction)
432+
signed_txn = self._wallet_account.sign_transaction(transaction) # type: ignore[arg-type]
427433
tx_hash = self._blockchain.eth.send_raw_transaction(signed_txn.raw_transaction)
428434
tx_receipt = self._blockchain.eth.wait_for_transaction_receipt(tx_hash, timeout=INFERENCE_TX_TIMEOUT)
429435

430-
if tx_receipt.status == 0:
436+
if tx_receipt["status"] == 0:
431437
raise ContractLogicError(f"Run transaction failed. Receipt: {tx_receipt}")
432438

433439
# Get the inference result from the contract
434440
result = contract.functions.getInferenceResult().call()
435441

436-
return convert_array_to_model_output(result)
442+
run_output: ModelOutput = convert_array_to_model_output(result)
443+
return run_output
437444

438445
def read_workflow_history(self, contract_address: str, num_results: int) -> List[ModelOutput]:
439446
"""

src/opengradient/client/model_hub.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ def upload(self, model_path: str, model_name: str, version: str) -> FileUploadRe
165165
else:
166166
raise RuntimeError("Empty or null response content received")
167167
elif response.status_code == 500:
168-
raise RuntimeError("Internal server error occurred", status_code=500)
168+
raise RuntimeError(f"Internal server error occurred (status_code=500)")
169169
else:
170170
error_message = response.json().get("detail", "Unknown error occurred")
171-
raise RuntimeError(f"Upload failed: {error_message}", status_code=response.status_code)
171+
raise RuntimeError(f"Upload failed: {error_message} (status_code={response.status_code})")
172172

173173
except requests.RequestException as e:
174174
raise RuntimeError(f"Upload failed: {str(e)}")
@@ -200,7 +200,8 @@ def list_files(self, model_name: str, version: str) -> List[Dict]:
200200
try:
201201
response = requests.get(url, headers=headers)
202202
response.raise_for_status()
203-
return response.json()
203+
result: list[dict] = response.json()
204+
return result
204205

205206
except requests.RequestException as e:
206207
raise RuntimeError(f"File listing failed: {str(e)}")

src/opengradient/client/opg_token.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ def ensure_opg_approval(wallet_account: LocalAccount, opg_amount: float) -> Perm
100100
}
101101
)
102102

103-
signed = wallet_account.sign_transaction(tx)
103+
signed = wallet_account.sign_transaction(tx) # type: ignore[arg-type]
104104
tx_hash = w3.eth.send_raw_transaction(signed.raw_transaction)
105105
receipt = w3.eth.wait_for_transaction_receipt(tx_hash, timeout=120)
106106

107-
if receipt.status != 1:
107+
if receipt.status != 1: # type: ignore[attr-defined]
108108
raise RuntimeError(f"Permit2 approval transaction reverted: {tx_hash.hex()}")
109109

110110
allowance_after = token.functions.allowance(owner, spender).call()

0 commit comments

Comments
 (0)