Skip to content

Commit bd27920

Browse files
committed
🔧 refactor: replace Union type hints with the new Python 3.10 syntax using '|'
🔧 refactor: add __future__ import annotations for forward references
1 parent 533901b commit bd27920

21 files changed

+104
-148
lines changed

‎convex_api/account.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
55
66
"""
7+
from __future__ import annotations
78

89
import re
9-
from typing import Union
1010
from convex_api.key_pair import KeyPair
1111

1212

1313
class Account:
1414

1515
@staticmethod
16-
def is_address(text: Union[int, str]) -> bool:
16+
def is_address(text: int | str) -> bool:
1717
"""
1818
Returns True if the text value is a valid address.
1919
@@ -25,7 +25,7 @@ def is_address(text: Union[int, str]) -> bool:
2525
return Account.to_address(text) >= 0
2626

2727
@staticmethod
28-
def to_address(value: Union['Account', int, str]) -> int:
28+
def to_address(value: Account | int | str) -> int:
2929
"""
3030
Convert address text with possible leading '#' to an integer address value.
3131
@@ -47,7 +47,7 @@ def to_address(value: Union['Account', int, str]) -> int:
4747
raise ValueError(f'Invalid address {value}')
4848
return address
4949

50-
def __init__(self, key_pair: KeyPair, address: Union['Account', int, str], name: Union[str, None] = None):
50+
def __init__(self, key_pair: KeyPair, address: Account | int | str, name: str | None = None):
5151
"""
5252
5353
Create a new account with a private key KeyPair.
@@ -133,7 +133,7 @@ def address(self) -> int:
133133
return self._address
134134

135135
@address.setter
136-
def address(self, value: Union['Account', int, str]) -> None:
136+
def address(self, value: Account | int | str) -> None:
137137
"""
138138
139139
Sets the network address of this account
@@ -154,7 +154,7 @@ def address(self, value: Union['Account', int, str]) -> None:
154154
self._address = Account.to_address(value)
155155

156156
@property
157-
def name(self) -> Union[str, None]:
157+
def name(self) -> str | None:
158158
return self._name
159159

160160
@name.setter

‎convex_api/api.py‎

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
Convex API
44
55
"""
6+
from __future__ import annotations
7+
8+
from typing import TYPE_CHECKING
9+
10+
if TYPE_CHECKING:
11+
from typing import Any, Dict
612

713
import logging
814
import re
915
import secrets
1016
import time
11-
from typing import (
12-
Any,
13-
Dict,
14-
Union,
15-
cast
16-
)
17+
from typing import cast
1718
from urllib.parse import urljoin
1819

1920
import requests
@@ -101,7 +102,7 @@ def create_account(self, key_pair: KeyPair, sequence_retry_count: int = 20) -> A
101102

102103
return account
103104

104-
def load_account(self, name: str, key_pair: KeyPair) -> Union[Account, None]:
105+
def load_account(self, name: str, key_pair: KeyPair) -> Account | None:
105106
"""
106107
107108
Load an account using the correct name. If successful return the :class:`.Account` object with the address set.
@@ -130,7 +131,7 @@ def load_account(self, name: str, key_pair: KeyPair) -> Union[Account, None]:
130131
new_account = Account(key_pair, address, name=name)
131132
return new_account
132133

133-
def setup_account(self, name: str, key_pair: KeyPair, register_account: Union[Account, None] = None) -> Union[Account, None]:
134+
def setup_account(self, name: str, key_pair: KeyPair, register_account: Account | None = None) -> Account | None:
134135
"""
135136
136137
Convenience method to create or load an account based on the account name.
@@ -174,8 +175,8 @@ def setup_account(self, name: str, key_pair: KeyPair, register_account: Union[Ac
174175
def register_account_name(
175176
self,
176177
name: str,
177-
address_account: Union[Account, int, str],
178-
account: Union[Account, None] = None
178+
address_account: Account | int | str,
179+
account: Account | None = None
179180
) -> Account:
180181
"""
181182
@@ -287,7 +288,7 @@ def send(self, transaction: str, account: Account, sequence_retry_count: int = 2
287288
def query(
288289
self,
289290
transaction: str,
290-
address_account: Union[int, str, Account]
291+
address_account: int | str | Account
291292
):
292293
"""
293294
@@ -398,7 +399,7 @@ def topup_account(
398399
retry_count -= 1
399400
return transfer_amount
400401

401-
def get_address(self, function_name: str, address_account: Union[Account, int, str]):
402+
def get_address(self, function_name: str, address_account: Account | int | str):
402403
"""
403404
404405
Query the network for a contract ( function ) address. The contract must have been deployed
@@ -425,7 +426,7 @@ def get_address(self, function_name: str, address_account: Union[Account, int, s
425426
result = self.query(line, address_account)
426427
return Account.to_address(result.value)
427428

428-
def get_balance(self, address_account: Union[Account, int, str], account_from: Union[Account, int, str, None] = None):
429+
def get_balance(self, address_account: Account | int | str, account_from: Account | int | str | None = None):
429430
"""
430431
431432
Get a balance of an account.
@@ -473,7 +474,7 @@ def get_balance(self, address_account: Union[Account, int, str], account_from: U
473474
value = cast(int, result.value)
474475
return value
475476

476-
def transfer(self, to_address_account: Union[Account, int, str], amount: Union[int, float], account: Account):
477+
def transfer(self, to_address_account: Account | int | str, amount: int | float, account: Account):
477478
"""
478479
479480
Transfer funds from on account to another.
@@ -513,7 +514,7 @@ def transfer(self, to_address_account: Union[Account, int, str], amount: Union[i
513514
return result.value
514515
return 0
515516

516-
def transfer_account(self, from_account: Union[Account, int, str], to_account: Union[Account, int, str]):
517+
def transfer_account(self, from_account: Account | int | str, to_account: Account | int | str):
517518
"""
518519
519520
**WARNING**
@@ -564,7 +565,7 @@ def transfer_account(self, from_account: Union[Account, int, str], to_account: U
564565
if result is not None and from_account.key_pair.is_equal(result.value):
565566
return Account(from_account.key_pair, to_account.address, to_account.name)
566567

567-
def get_account_info(self, address_account: Union[Account, int, str]) -> AccountDetailsResponse:
568+
def get_account_info(self, address_account: Account | int | str) -> AccountDetailsResponse:
568569
"""
569570
570571
Get account information. This will only work with an account that has a balance or has had some transactions
@@ -605,7 +606,7 @@ def get_account_info(self, address_account: Union[Account, int, str]) -> Account
605606

606607
return result
607608

608-
def resolve_account_name(self, name: str) -> Union[int, None]:
609+
def resolve_account_name(self, name: str) -> int | None:
609610
"""
610611
Resolves an account name to an address.
611612
:param string name Name of the account to resolve.
@@ -618,7 +619,7 @@ def resolve_account_name(self, name: str) -> Union[int, None]:
618619
"""
619620
return self._registry.resolve_address(f'account.{name}')
620621

621-
def resolve_name(self, name: str) -> Union[int, None]:
622+
def resolve_name(self, name: str) -> int | None:
622623
"""
623624
Resolves any Convex Name Services to an address.
624625
:param string name Name of the the CNS Service.
@@ -641,11 +642,11 @@ def load_contract(self, name: str):
641642
def _post(
642643
self,
643644
url: str,
644-
data: Union[CreateAccountRequest, FaucetRequest, QueryRequest, PrepareTransactionRequest, SubmitTransactionRequest],
645+
data: CreateAccountRequest | FaucetRequest | QueryRequest | PrepareTransactionRequest | SubmitTransactionRequest,
645646
sequence_retry_count: int = 20
646-
) -> Union[Dict[str, Any], None]:
647+
) -> Dict[str, Any] | None:
647648
max_sleep_time_seconds = 1
648-
result: Union[Dict[str, Any], None] = None
649+
result: Dict[str, Any] | None = None
649650
while sequence_retry_count >= 0:
650651
response = requests.post(url, data=data.model_dump_json())
651652
if response.status_code == 200:
@@ -668,8 +669,8 @@ def _post(
668669
def _transaction_prepare(
669670
self,
670671
transaction: str,
671-
address: Union[Account, int, str],
672-
sequence_number: Union[int, None] = None
672+
address: Account | int | str,
673+
sequence_number: int | None = None
673674
) -> PrepareTransactionResponse:
674675
"""
675676
A transaction requires its hash to be digitally signed by the executing account prior to submission.
@@ -703,15 +704,15 @@ def _transaction_prepare(
703704

704705
def _transaction_submit(
705706
self,
706-
address: Union[Account, int, str],
707+
address: Account | int | str,
707708
public_key: str,
708709
hash_data: str,
709710
signed_data: str
710711
) -> SubmitTransactionResponse:
711712
"""
712713
Submit a transaction to the Convex network.
713714
714-
:param Union[Account, int, str] address: :class:`.Account` object or address of the executing account.
715+
:param Account | int, str address: :class:`.Account` object or address of the executing account.
715716
:param str public_key: Public key of the executing account.
716717
:param str hash_data: Hash of the transaction to be submitted.
717718
:param str signed_data: Ed25519 signature of the transaction hash.
@@ -733,7 +734,7 @@ def _transaction_submit(
733734

734735
def _transaction_query(
735736
self,
736-
address: Union[Account, int, str],
737+
address: Account | int | str,
737738
transaction: str
738739
):
739740
"""

‎convex_api/contract.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"""
77

88
import re
9-
from typing import Union
109
from convex_api.account import Account
1110
from convex_api.api import API
1211

@@ -25,9 +24,9 @@ def __init__(self, convex: API):
2524

2625
def load(
2726
self,
28-
name: Union[str, None] = None,
29-
address: Union[Account, int, str, None] = None,
30-
owner_address: Union[Account, int, str, None] = None
27+
name: str | None = None,
28+
address: Account | int | str | None = None,
29+
owner_address: Account | int | str | None = None
3130
):
3231
"""
3332
@@ -62,10 +61,10 @@ def load(
6261
def deploy(
6362
self,
6463
account: Account,
65-
text: Union[str, None] = None,
66-
filename: Union[str, None] = None,
67-
name: Union[str, None] = None,
68-
owner_account: Union[Account, None] = None
64+
text: str | None = None,
65+
filename: str | None = None,
66+
name: str | None = None,
67+
owner_account: Account | None = None
6968
):
7069
"""
7170
@@ -141,7 +140,7 @@ def send(self, transaction: str, account: Account):
141140
raise ValueError(f'No contract address found for {self._name}')
142141
return self._convex.send(f'(call #{self._address} {transaction})', account)
143142

144-
def query(self, transaction: str, account_address: Union[Account, int, str, None] = None):
143+
def query(self, transaction: str, account_address: Account | int | str | None = None):
145144
"""
146145
147146
Sends a query to the contract.

‎convex_api/exceptions.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
"""
66

77

8-
from typing import Union
9-
10-
118
class ConvexBaseError(Exception):
12-
def __init__(self, source: str, code: Union[int, str], text: str):
9+
def __init__(self, source: str, code: int | str, text: str):
1310
self.source = source
1411
self.code = code
1512
self.text = text

‎convex_api/key_pair.py‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
55
66
"""
7+
from __future__ import annotations
78

89
import binascii
910
import re
10-
from typing import Union
1111

1212
from cryptography.hazmat.backends import default_backend
1313
from cryptography.hazmat.backends.openssl.backend import backend as openssl_backend
@@ -109,7 +109,7 @@ def hex_to_bytes(hexstr: str) -> bytes:
109109
raise ValueError('Invalid hex string')
110110

111111
@staticmethod
112-
def to_bytes(data: Union[int, bytes]) -> bytes:
112+
def to_bytes(data: int | bytes) -> bytes:
113113
"""
114114
115115
Convert data to bytes.
@@ -225,7 +225,7 @@ def is_public_key(public_key: str) -> bool:
225225
else:
226226
return False
227227

228-
def __init__(self, private_key: Union[Ed25519PrivateKey, None] = None):
228+
def __init__(self, private_key: Ed25519PrivateKey | None = None):
229229
"""
230230
231231
Create a new keypair object with a public and private key as a Ed25519PrivateKey. It is better to use
@@ -283,7 +283,7 @@ def sign(self, hash_text: str) -> str:
283283
signed_hash_bytes = self._private_key.sign(hash_data)
284284
return KeyPair.to_hex(signed_hash_bytes)
285285

286-
def export_to_text(self, password: Union[str, bytes]):
286+
def export_to_text(self, password: str | bytes):
287287
"""
288288
289289
Export the private key to an encrypted PEM string.
@@ -340,7 +340,7 @@ def export_to_mnemonic(self) -> str:
340340
encryption_algorithm=serialization.NoEncryption()
341341
))
342342

343-
def export_to_file(self, filename: str, password: Union[str, bytes]) -> None:
343+
def export_to_file(self, filename: str, password: str | bytes) -> None:
344344
"""
345345
346346
Export the private key to a file. This uses `export_to_text` to export as a string.
@@ -455,7 +455,7 @@ def public_key_checksum(self) -> str:
455455

456456
return KeyPair.to_public_key_checksum(self.public_key)
457457

458-
def is_equal(self, public_key_pair: Union['KeyPair', str]) -> bool:
458+
def is_equal(self, public_key_pair: KeyPair | str) -> bool:
459459
"""
460460
461461
Compare the value to see if it is the same as this key_pair
@@ -473,7 +473,7 @@ def is_equal(self, public_key_pair: Union['KeyPair', str]) -> bool:
473473
return KeyPair.remove_0x_prefix(self.public_key_checksum).lower() == KeyPair.remove_0x_prefix(public_key).lower()
474474

475475
@staticmethod
476-
def import_from_bytes(value: bytes) -> 'KeyPair':
476+
def import_from_bytes(value: bytes) -> KeyPair:
477477
"""
478478
479479
Import an keypair from a private key in bytes.
@@ -491,7 +491,7 @@ def import_from_bytes(value: bytes) -> 'KeyPair':
491491
return KeyPair(Ed25519PrivateKey.from_private_bytes(value))
492492

493493
@staticmethod
494-
def import_from_text(text: Union[str, bytes], password: Union[str, bytes]) -> 'KeyPair':
494+
def import_from_text(text: str | bytes, password: str | bytes) -> KeyPair:
495495
"""
496496
497497
Import a KeyPair from an encrypted PEM string.
@@ -526,7 +526,7 @@ def import_from_text(text: Union[str, bytes], password: Union[str, bytes]) -> 'K
526526
return KeyPair(private_key)
527527

528528
@staticmethod
529-
def import_from_mnemonic(words: str) -> 'KeyPair':
529+
def import_from_mnemonic(words: str) -> KeyPair:
530530
"""
531531
532532
Creates a new KeyPair object using a list of words. These words contain the private key and must be kept secret.
@@ -549,7 +549,7 @@ def import_from_mnemonic(words: str) -> 'KeyPair':
549549
return KeyPair(Ed25519PrivateKey.from_private_bytes(value))
550550

551551
@staticmethod
552-
def import_from_file(filename: str, password: Union[str, bytes]) -> 'KeyPair':
552+
def import_from_file(filename: str, password: str | bytes) -> KeyPair:
553553
"""
554554
555555
Load the encrypted private key from file. The file is saved in PEM format encrypted with a password

0 commit comments

Comments
 (0)