Skip to content

Commit

Permalink
fix: mypy concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Jul 17, 2023
1 parent 7b53b79 commit e7b575d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import Any, Dict, Iterator, List, Optional, Union, cast

from eth_typing import BlockNumber, HexStr
from eth_utils import add_0x_prefix, is_0x_prefixed, to_hex
from eth_utils import add_0x_prefix, to_hex
from ethpm_types import HexBytes
from evm_trace import CallTreeNode as EvmCallTreeNode
from evm_trace import TraceFrame as EvmTraceFrame
Expand Down Expand Up @@ -68,6 +68,7 @@
raises_not_implemented,
run_until_complete,
spawn,
to_int,
)
from ape.utils.misc import DEFAULT_MAX_RETRIES_TX, _create_raises_not_implemented_error

Expand Down Expand Up @@ -856,7 +857,7 @@ def chain_id(self) -> int:
@property
def gas_price(self) -> int:
price = self.web3.eth.generate_gas_price() or 0
return int(price, 16) if isinstance(price, str) and is_0x_prefixed(price) else int(price)
return to_int(price)

@property
def priority_fee(self) -> int:
Expand Down
6 changes: 2 additions & 4 deletions src/ape/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import requests
import yaml
from eth_utils import is_0x_prefixed
from ethpm_types import HexBytes
from importlib_metadata import PackageNotFoundError, distributions, packages_distributions
from importlib_metadata import version as version_metadata
Expand Down Expand Up @@ -271,10 +272,7 @@ def to_int(value) -> int:
if isinstance(value, int):
return value
elif isinstance(value, str):
if value.startswith("0x"):
return int(value, 16)
else:
return int(value)
return int(value, 16) if is_0x_prefixed(value) else int(value)
elif isinstance(value, bytes):
return int.from_bytes(value, "big")

Expand Down
8 changes: 8 additions & 0 deletions tests/functional/utils/test_misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
from ethpm_types import HexBytes
from packaging.version import Version
from web3.types import Wei

from ape.exceptions import APINotImplementedError
from ape.utils.misc import (
Expand All @@ -11,6 +13,7 @@
is_zero_hex,
raises_not_implemented,
run_until_complete,
to_int,
)


Expand Down Expand Up @@ -99,3 +102,8 @@ def test_is_not_zero_address(owner):
assert not is_zero_hex(owner)
assert not is_zero_hex("MyContract")
assert not is_zero_hex("0x01")


@pytest.mark.parametrize("val", (5, "0x5", "0x05", "0x0005", HexBytes(5), Wei(5)))
def test_to_int(val):
assert to_int(val) == 5

0 comments on commit e7b575d

Please sign in to comment.