Skip to content

Commit 7ffd261

Browse files
committed
Clean up async_net as a separate mypy type from net
- Also minor cleanup that came along with these changes: e.g. expect a more specific error, ``NameNotFound`` for ens test since the async version started to wrongly xpass.
1 parent 2f99280 commit 7ffd261

File tree

7 files changed

+34
-25
lines changed

7 files changed

+34
-25
lines changed

tests/core/contracts/test_contract_call_interface.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
FallbackNotFound,
3030
InvalidAddress,
3131
MismatchedABI,
32+
NameNotFound,
3233
NoABIFound,
3334
NoABIFunctionsFound,
3435
ValidationError,
@@ -490,9 +491,9 @@ def test_call_address_reflector_name_array(address_reflector_contract, call):
490491
assert addresses == result
491492

492493

493-
def test_call_reject_invalid_ens_name(address_reflector_contract, call):
494+
def test_call_rejects_invalid_ens_name(address_reflector_contract, call):
494495
with contract_ens_addresses(address_reflector_contract, []):
495-
with pytest.raises(ValueError):
496+
with pytest.raises(NameNotFound):
496497
call(
497498
contract=address_reflector_contract,
498499
contract_function="reflect",
@@ -1363,11 +1364,11 @@ async def test_async_call_address_reflector_name_array(
13631364

13641365
@pytest.mark.xfail
13651366
@pytest.mark.asyncio
1366-
async def test_async_call_reject_invalid_ens_name(
1367+
async def test_async_call_rejects_invalid_ens_name(
13671368
async_address_reflector_contract, async_call
13681369
):
13691370
with contract_ens_addresses(async_address_reflector_contract, []):
1370-
with pytest.raises(ValueError):
1371+
with pytest.raises(NameNotFound):
13711372
await async_call(
13721373
contract=async_address_reflector_contract,
13731374
contract_function="reflect",

tests/core/providers/test_async_http_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_web3_with_async_http_provider_has_default_middlewares_and_modules() ->
5252

5353
assert isinstance(async_w3.eth, AsyncEth)
5454
assert isinstance(async_w3.geth, Geth)
55-
assert isinstance(async_w3.async_net, AsyncNet)
55+
assert isinstance(async_w3.net, AsyncNet)
5656
assert isinstance(async_w3.geth.admin, AsyncGethAdmin)
5757
assert isinstance(async_w3.geth.personal, AsyncGethPersonal)
5858
assert isinstance(async_w3.geth.txpool, AsyncGethTxPool)

tests/integration/go_ethereum/test_goethereum_http.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,22 @@ class TestGoEthereumAsyncAdminModuleTest(GoEthereumAsyncAdminModuleTest):
129129
@pytest.mark.xfail(
130130
reason="running geth with the --nodiscover flag doesn't allow peer addition"
131131
)
132-
async def test_admin_peers(self, w3: "Web3") -> None:
133-
await super().test_admin_peers(w3)
132+
async def test_admin_peers(self, async_w3: "Web3") -> None:
133+
await super().test_admin_peers(async_w3)
134134

135135
@pytest.mark.asyncio
136-
async def test_admin_start_stop_http(self, w3: "Web3") -> None:
136+
async def test_admin_start_stop_http(self, async_w3: "Web3") -> None:
137137
# This test causes all tests after it to fail on CI if it's allowed to run
138138
pytest.xfail(
139139
reason="Only one HTTP endpoint is allowed to be active at any time"
140140
)
141-
await super().test_admin_start_stop_http(w3)
141+
await super().test_admin_start_stop_http(async_w3)
142142

143143
@pytest.mark.asyncio
144-
async def test_admin_start_stop_ws(self, w3: "Web3") -> None:
144+
async def test_admin_start_stop_ws(self, async_w3: "Web3") -> None:
145145
# This test causes all tests after it to fail on CI if it's allowed to run
146146
pytest.xfail(reason="Only one WS endpoint is allowed to be active at any time")
147-
await super().test_admin_start_stop_ws(w3)
147+
await super().test_admin_start_stop_ws(async_w3)
148148

149149

150150
class TestGoEthereumAsyncNetModuleTest(GoEthereumAsyncNetModuleTest):

web3/_utils/contracts.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,13 @@ def encode_abi(
208208
)
209209

210210
normalizers = [
211-
abi_ens_resolver(w3),
212211
abi_address_to_hex,
213212
abi_bytes_to_bytes,
214213
abi_string_to_text,
215214
]
215+
if not w3.eth.is_async:
216+
normalizers.append(abi_ens_resolver(w3))
217+
216218
normalized_arguments = map_abi_data(
217219
normalizers,
218220
argument_types,

web3/_utils/module_testing/net_module.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from typing import (
33
TYPE_CHECKING,
4+
cast,
45
)
56

67
from eth_utils import (
@@ -15,7 +16,7 @@
1516

1617
class NetModuleTest:
1718
def test_net_version(self, w3: "Web3") -> None:
18-
version = w3.net.version
19+
version = cast(str, w3.net.version)
1920

2021
assert is_string(version)
2122
assert version.isdigit()
@@ -34,19 +35,19 @@ def test_net_peer_count(self, w3: "Web3") -> None:
3435
class AsyncNetModuleTest:
3536
@pytest.mark.asyncio
3637
async def test_net_version(self, async_w3: "Web3") -> None:
37-
version = await async_w3.async_net.version
38+
version = await async_w3.net.version # type: ignore
3839

3940
assert is_string(version)
4041
assert version.isdigit()
4142

4243
@pytest.mark.asyncio
4344
async def test_net_listening(self, async_w3: "Web3") -> None:
44-
listening = await async_w3.async_net.listening
45+
listening = await async_w3.net.listening # type: ignore
4546

4647
assert is_boolean(listening)
4748

4849
@pytest.mark.asyncio
4950
async def test_net_peer_count(self, async_w3: "Web3") -> None:
50-
peer_count = await async_w3.async_net.peer_count
51+
peer_count = await async_w3.net.peer_count # type: ignore
5152

5253
assert is_integer(peer_count)

web3/_utils/normalizers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@ def abi_address_to_hex(
205205

206206

207207
@curry
208-
def abi_ens_resolver(w3: "Web3", type_str: TypeStr, val: Any) -> Tuple[TypeStr, Any]:
208+
def abi_ens_resolver(
209+
w3: "Web3",
210+
type_str: TypeStr,
211+
val: Any,
212+
) -> Tuple[TypeStr, Any]:
209213
if type_str == "address" and is_ens_name(val):
210214
if w3 is None:
211215
raise InvalidAddress(
@@ -214,11 +218,12 @@ def abi_ens_resolver(w3: "Web3", type_str: TypeStr, val: Any) -> Tuple[TypeStr,
214218
)
215219

216220
_ens = cast(ENS, w3.ens)
221+
net_version = int(cast(str, w3.net.version)) if hasattr(w3, "net") else None
217222
if _ens is None:
218223
raise InvalidAddress(
219224
f"Could not look up name {val!r} because ENS is" " set to None"
220225
)
221-
elif int(w3.net.version) != 1 and not isinstance(_ens, StaticENS):
226+
elif net_version != 1 and not isinstance(_ens, StaticENS):
222227
raise InvalidAddress(
223228
f"Could not look up name {val!r} because web3 is"
224229
" not connected to mainnet"

web3/main.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
def get_async_default_modules() -> Dict[str, Union[Type[Module], Sequence[Any]]]:
134134
return {
135135
"eth": AsyncEth,
136-
"async_net": AsyncNet,
136+
"net": AsyncNet,
137137
"geth": (
138138
Geth,
139139
{
@@ -237,8 +237,7 @@ def to_checksum_address(value: Union[AnyAddress, str, bytes]) -> ChecksumAddress
237237
# mypy Types
238238
eth: Eth
239239
geth: Geth
240-
net: Net
241-
async_net: AsyncNet
240+
net: Union[Net, AsyncNet]
242241

243242
def __init__(
244243
self,
@@ -256,10 +255,11 @@ def __init__(
256255
self.codec = ABICodec(build_default_registry())
257256

258257
if modules is None:
259-
if provider and provider.is_async:
260-
modules = get_async_default_modules()
261-
else:
262-
modules = get_default_modules()
258+
modules = (
259+
get_async_default_modules()
260+
if provider and provider.is_async
261+
else get_default_modules()
262+
)
263263

264264
self.attach_modules(modules)
265265

0 commit comments

Comments
 (0)