Skip to content

Commit 0f77497

Browse files
authored
Merge pull request #480 from multiversx/fix-mypy-issues
Fixed mypy errors
2 parents baf60bb + 622105f commit 0f77497

14 files changed

+79
-107
lines changed

multiversx_sdk_cli/cli_deps.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ def check(args: Any):
5757
if len(missing_dependencies):
5858
raise errors.DependenciesMissing(missing_dependencies)
5959
return
60-
else:
61-
module = dependencies.get_module_by_key(name)
62-
tag_to_check: str = config.get_dependency_tag(module.key)
63-
64-
is_installed = check_module_is_installed(module, tag_to_check)
65-
if is_installed and name != "rust":
66-
logger.info(f"[{module.key} {tag_to_check}] is installed.")
67-
return
68-
elif not is_installed:
69-
raise errors.DependencyMissing(module.key, tag_to_check)
60+
61+
module = dependencies.get_module_by_key(name)
62+
tag_to_check = config.get_dependency_tag(module.key)
63+
64+
is_installed = check_module_is_installed(module, tag_to_check)
65+
if is_installed:
66+
logger.info(f"[{module.key} {tag_to_check}] is installed.")
67+
return
68+
elif not is_installed:
69+
raise errors.DependencyMissing(module.key, tag_to_check)
7070

7171

7272
def check_module_is_installed(module: DependencyModule, tag_to_check: str) -> bool:

multiversx_sdk_cli/cli_shared.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -318,30 +318,28 @@ def prepare_account(args: Any):
318318
return Account.new_from_pem(file_path=Path(args.pem), index=args.pem_index, hrp=hrp)
319319
elif args.keyfile:
320320
password = load_password(args)
321-
account = Account.new_from_keystore(
321+
return Account.new_from_keystore(
322322
file_path=Path(args.keyfile),
323323
password=password,
324324
address_index=args.address_index,
325325
hrp=hrp,
326326
)
327327
elif args.ledger:
328-
account = LedgerAccount(address_index=args.ledger_address_index)
328+
return LedgerAccount(address_index=args.ledger_address_index)
329329
else:
330330
raise errors.NoWalletProvided()
331331

332-
return account
333-
334332

335333
def prepare_relayer_account(args: Any) -> IAccount:
336334
hrp = config.get_address_hrp()
337335

338336
if args.relayer_ledger:
339-
account = LedgerAccount(address_index=args.relayer_ledger_address_index)
337+
return LedgerAccount(address_index=args.relayer_ledger_address_index)
340338
if args.relayer_pem:
341-
account = Account.new_from_pem(file_path=Path(args.relayer_pem), index=args.relayer_pem_index, hrp=hrp)
339+
return Account.new_from_pem(file_path=Path(args.relayer_pem), index=args.relayer_pem_index, hrp=hrp)
342340
elif args.relayer_keyfile:
343341
password = load_password(args)
344-
account = Account.new_from_keystore(
342+
return Account.new_from_keystore(
345343
file_path=Path(args.relayer_keyfile),
346344
password=password,
347345
address_index=args.relayer_address_index,
@@ -350,29 +348,25 @@ def prepare_relayer_account(args: Any) -> IAccount:
350348
else:
351349
raise errors.NoWalletProvided()
352350

353-
return account
354-
355351

356352
def prepare_guardian_account(args: Any) -> IAccount:
357353
hrp = config.get_address_hrp()
358354

359355
if args.guardian_pem:
360-
account = Account.new_from_pem(file_path=Path(args.guardian_pem), index=args.guardian_pem_index, hrp=hrp)
356+
return Account.new_from_pem(file_path=Path(args.guardian_pem), index=args.guardian_pem_index, hrp=hrp)
361357
elif args.guardian_keyfile:
362358
password = load_guardian_password(args)
363-
account = Account.new_from_keystore(
359+
return Account.new_from_keystore(
364360
file_path=Path(args.guardian_keyfile),
365361
password=password,
366362
address_index=args.guardian_address_index,
367363
hrp=hrp,
368364
)
369365
elif args.guardian_ledger:
370-
account = LedgerAccount(address_index=args.relayer_ledger_address_index)
366+
return LedgerAccount(address_index=args.relayer_ledger_address_index)
371367
else:
372368
raise errors.NoWalletProvided()
373369

374-
return account
375-
376370

377371
def load_sender_account(args: Any) -> Union[IAccount, None]:
378372
hrp = config.get_address_hrp()
@@ -416,7 +410,7 @@ def get_guardian_address(guardian: Union[IAccount, None], args: Any) -> Union[Ad
416410
address_from_account = guardian.address if guardian else None
417411
address_from_args = Address.new_from_bech32(args.guardian) if hasattr(args, "guardian") and args.guardian else None
418412

419-
if address_from_account and address_from_args and address_from_account != address_from_args:
413+
if not _is_matching_address(address_from_account, address_from_args):
420414
raise IncorrectWalletError("Guardian wallet does not match the guardian's address set in the arguments.")
421415

422416
return address_from_account or address_from_args
@@ -426,12 +420,18 @@ def get_relayer_address(relayer: Union[IAccount, None], args: Any) -> Union[Addr
426420
address_from_account = relayer.address if relayer else None
427421
address_from_args = Address.new_from_bech32(args.relayer) if hasattr(args, "relayer") and args.relayer else None
428422

429-
if address_from_account and address_from_args and address_from_account != address_from_args:
423+
if not _is_matching_address(address_from_account, address_from_args):
430424
raise IncorrectWalletError("Relayer wallet does not match the relayer's address set in the arguments.")
431425

432426
return address_from_account or address_from_args
433427

434428

429+
def _is_matching_address(account_address: Union[Address, None], args_address: Union[Address, None]) -> bool:
430+
if account_address and args_address and account_address != args_address:
431+
return False
432+
return True
433+
434+
435435
def load_relayer_account(args: Any) -> Union[IAccount, None]:
436436
hrp = config.get_address_hrp()
437437

multiversx_sdk_cli/cli_transactions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def setup_parser(args: list[str], subparsers: Any) -> Any:
9090
f"Sign a previously saved transaction.{CLIOutputBuilder.describe()}",
9191
)
9292
# we add the wallet args, but don't make the args mandatory
93-
cli_shared.add_wallet_args(args, sub, True)
93+
cli_shared.add_wallet_args(args=args, sub=sub, skip_required_check=True)
9494
cli_shared.add_infile_arg(sub, what="a previously saved transaction")
9595
cli_shared.add_outfile_arg(sub, what="the signed transaction")
9696
cli_shared.add_broadcast_args(sub)

multiversx_sdk_cli/dns.py

+21-46
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
from typing import Any, List, Optional, Protocol, Union
1+
from typing import Any, List, Protocol
22

33
from Cryptodome.Hash import keccak
44
from multiversx_sdk import (
55
Address,
66
AddressComputer,
7-
AwaitingOptions,
8-
NetworkConfig,
9-
TransactionOnNetwork,
10-
TransactionsFactoryConfig,
7+
SmartContractQuery,
8+
SmartContractQueryResponse,
119
)
1210

1311
from multiversx_sdk_cli import cli_shared, utils
1412
from multiversx_sdk_cli.config import get_address_hrp
1513
from multiversx_sdk_cli.constants import ADDRESS_ZERO_HEX
16-
from multiversx_sdk_cli.contracts import SmartContract
1714
from multiversx_sdk_cli.transactions import do_prepare_transaction
1815

1916
MaxNumShards = 256
@@ -23,53 +20,39 @@
2320

2421
# fmt: off
2522
class INetworkProvider(Protocol):
26-
def query_contract(self, query: Any) -> Any:
27-
...
28-
29-
def get_network_config(self) -> NetworkConfig:
30-
...
31-
32-
def await_transaction_completed(
33-
self, transaction_hash: Union[bytes, str], options: Optional[AwaitingOptions] = None
34-
) -> TransactionOnNetwork:
23+
def query_contract(self, query: SmartContractQuery) -> SmartContractQueryResponse:
3524
...
3625
# fmt: on
3726

3827

3928
def resolve(name: str, proxy: INetworkProvider) -> Address:
40-
name_arg = "0x{}".format(str.encode(name).hex())
4129
dns_address = dns_address_for_name(name)
4230

43-
response = _query_contract(contract_address=dns_address, proxy=proxy, function="resolve", args=[name_arg])
31+
response = _query_contract(contract_address=dns_address, proxy=proxy, function="resolve", args=[name.encode()])
4432

45-
if len(response) == 0:
33+
if len(response.return_data_parts) == 0:
4634
return Address.new_from_hex(ADDRESS_ZERO_HEX, get_address_hrp())
4735

48-
result = response[0].get("returnDataParts")[0]
49-
return Address.new_from_hex(result, get_address_hrp())
36+
result = response.return_data_parts[0]
37+
return Address(result, get_address_hrp())
5038

5139

5240
def validate_name(name: str, shard_id: int, proxy: INetworkProvider):
53-
name_arg = "0x{}".format(str.encode(name).hex())
5441
dns_address = compute_dns_address_for_shard_id(shard_id)
5542

5643
response = _query_contract(
5744
contract_address=dns_address,
5845
proxy=proxy,
5946
function="validateName",
60-
args=[name_arg],
47+
args=[name.encode()],
6148
)
6249

63-
response = response[0]
64-
65-
return_code = response["returnCode"]
50+
return_code: str = response.return_code
6651
if return_code == "ok":
6752
print(f"name [{name}] is valid")
6853
else:
6954
print(f"name [{name}] is invalid")
7055

71-
print(response)
72-
7356

7457
def register(args: Any):
7558
args = utils.as_object(args)
@@ -107,22 +90,18 @@ def registration_cost(shard_id: int, proxy: INetworkProvider) -> int:
10790
args=[],
10891
)
10992

110-
response = response[0]
111-
112-
data = response["returnDataParts"][0]
93+
data = response.return_data_parts[0]
11394
if not data:
11495
return 0
11596
else:
116-
return int("0x{}".format(data))
97+
return int.from_bytes(data, byteorder="big", signed=False)
11798

11899

119100
def version(shard_id: int, proxy: INetworkProvider) -> str:
120101
dns_address = compute_dns_address_for_shard_id(shard_id)
121102

122103
response = _query_contract(contract_address=dns_address, proxy=proxy, function="version", args=[])
123-
124-
response = response[0]
125-
return bytearray.fromhex(response["returnDataParts"][0]).decode()
104+
return response.return_data_parts[0].decode()
126105

127106

128107
def dns_address_for_name(name: str) -> Address:
@@ -147,15 +126,11 @@ def dns_register_data(name: str) -> str:
147126
return "register@{}".format(name_enc.hex())
148127

149128

150-
def _query_contract(contract_address: Address, proxy: INetworkProvider, function: str, args: List[Any]) -> List[Any]:
151-
chain_id = proxy.get_network_config().chain_id
152-
config = TransactionsFactoryConfig(chain_id)
153-
contract = SmartContract(config)
154-
155-
return contract.query_contract(
156-
contract_address=contract_address,
157-
proxy=proxy,
158-
function=function,
159-
arguments=args,
160-
should_prepare_args=False,
161-
)
129+
def _query_contract(
130+
contract_address: Address,
131+
proxy: INetworkProvider,
132+
function: str,
133+
args: List[bytes],
134+
) -> SmartContractQueryResponse:
135+
query = SmartContractQuery(contract=contract_address, function=function, arguments=args)
136+
return proxy.query_contract(query)

multiversx_sdk_cli/localnet/config_root.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77

88
from multiversx_sdk_cli.localnet import config_default
99
from multiversx_sdk_cli.localnet.config_part import ConfigPart
10+
from multiversx_sdk_cli.localnet.config_sharding import Metashard, RegularShards
1011
from multiversx_sdk_cli.localnet.constants import METACHAIN_ID
1112
from multiversx_sdk_cli.localnet.node import Node
1213

1314
logger = logging.getLogger("localnet")
1415

1516

1617
class ConfigRoot(ConfigPart):
17-
def __init__(self):
18+
def __init__(self) -> None:
1819
self.general = config_default.general
1920
self.software = config_default.software
20-
self.metashard = config_default.metashard
21-
self.shards = config_default.shards
21+
self.metashard: Metashard = config_default.metashard
22+
self.shards: RegularShards = config_default.shards
2223
self.networking = config_default.networking
2324

2425
def get_name(self) -> str:

multiversx_sdk_cli/localnet/config_software.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ def _verify(self):
6666
f"In configuration section '{self.get_name()}', resolution is '{self.resolution.value}', but 'local_path' is not a directory: {self.local_path}"
6767
)
6868

69-
def get_archive_download_folder(self):
69+
def get_archive_download_folder(self) -> Path:
7070
return self.archive_download_folder.expanduser().resolve()
7171

72-
def get_archive_extraction_folder(self):
72+
def get_archive_extraction_folder(self) -> Path:
7373
return self.archive_extraction_folder.expanduser().resolve()
7474

7575
def get_local_path(self):

multiversx_sdk_cli/native_auth_client.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ def _get_current_block_hash_using_gateway(self) -> str:
5454
round = self._get_current_round()
5555
url = f"{self.config.gateway_url}/blocks/by-round/{round}"
5656
response = self._execute_request(url)
57-
blocks = response["data"]["blocks"]
58-
block = [b for b in blocks if b["shard"] == self.config.block_hash_shard][0]
57+
blocks: list[dict[str, Any]] = response["data"]["blocks"]
58+
block: dict[str, str] = [b for b in blocks if b["shard"] == self.config.block_hash_shard][0]
5959
return block["hash"]
6060

6161
def _get_current_round(self) -> int:
@@ -67,14 +67,14 @@ def _get_current_round(self) -> int:
6767

6868
url = f"{self.config.gateway_url}/network/status/{self.config.block_hash_shard}"
6969
response = self._execute_request(url)
70-
status = response["data"]["status"]
70+
status: dict[str, int] = response["data"]["status"]
7171

7272
return status["erd_current_round"]
7373

7474
def _get_current_block_hash_using_api(self) -> str:
7575
try:
7676
url = f"{self.config.api_url}/blocks/latest?ttl={self.config.expiry_seconds}&fields=hash"
77-
response = self._execute_request(url)
77+
response: dict[str, str] = self._execute_request(url)
7878
if response["hash"]:
7979
return response["hash"]
8080
except Exception:
@@ -88,7 +88,7 @@ def _get_current_block_hash_using_api_fallback(self) -> str:
8888
if self.config.block_hash_shard:
8989
url += f"&shard={self.config.block_hash_shard}"
9090

91-
response = self._execute_request(url)
91+
response: list[dict[str, str]] = self._execute_request(url)
9292
return response[0]["hash"]
9393

9494
def _encode_value(self, string: str) -> str:

multiversx_sdk_cli/tests/test_cli_contracts.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ def test_contract_query(capsys: Any):
617617

618618

619619
def _read_stdout(capsys: Any) -> str:
620-
return capsys.readouterr().out.strip()
620+
stdout: str = capsys.readouterr().out.strip()
621+
return stdout
621622

622623

623624
def get_contract_address(capsys: Any):
@@ -633,5 +634,5 @@ def get_query_response(capsys: Any):
633634

634635
def get_transaction_data(capsys: Any) -> str:
635636
out = _read_stdout(capsys)
636-
output = json.loads(out)
637+
output: dict[str, str] = json.loads(out)
637638
return output["emittedTransactionData"]

multiversx_sdk_cli/tests/test_cli_staking_provider.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,8 @@ def test_withdraw(capsys: Any):
742742

743743

744744
def _read_stdout(capsys: Any) -> str:
745-
return capsys.readouterr().out.strip()
745+
stdout: str = capsys.readouterr().out.strip()
746+
return stdout
746747

747748

748749
def get_transaction(capsys: Any):

multiversx_sdk_cli/tests/test_cli_transactions.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -394,4 +394,5 @@ def test_sign_transaction(capsys: Any):
394394

395395

396396
def _read_stdout(capsys: Any) -> str:
397-
return capsys.readouterr().out.strip()
397+
stdout: str = capsys.readouterr().out.strip()
398+
return stdout

multiversx_sdk_cli/tests/test_cli_wallet.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,7 @@ def test_wallet_convert_pem_to_pubkey(capsys: Any):
334334
def test_wallet_convert_pem_to_secret_key(capsys: Any):
335335
infile = testdata_path / "alice.pem"
336336

337-
main([
338-
"wallet", "convert", "--infile", str(infile), "--in-format", "pem", "--out-format", "secret-key"
339-
])
337+
main(["wallet", "convert", "--infile", str(infile), "--in-format", "pem", "--out-format", "secret-key"])
340338

341339
out = _read_stdout(capsys).strip("Output:\n\n")
342340
assert out == "413f42575f7f26fad3317a778771212fdb80245850981e48b58a4f25e344e8f9"
@@ -538,7 +536,8 @@ def _read_stdout_wallet_address(capsys: Any) -> str:
538536

539537

540538
def _read_stdout(capsys: Any) -> str:
541-
return capsys.readouterr().out.strip()
539+
stdout: str = capsys.readouterr().out.strip()
540+
return stdout
542541

543542

544543
def _mock_getpass(monkeypatch: Any, password: str):

0 commit comments

Comments
 (0)