Skip to content

Commit 1d6fc15

Browse files
authored
Auto Provider batch requests (#3607)
* Get active provider from AutoProvider * Add docs * Add newsfragment
1 parent aefeb7e commit 1d6fc15

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

docs/web3.main.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ Batch Requests
7272
responses = batch.execute()
7373
assert len(responses) == 3
7474
75+
.. note::
76+
77+
Within the batching context above, calls are suspended until
78+
``batch.execute()`` is called. Calling a method without
79+
passing it to ``batch.add`` might result in unexpected behavior.
80+
7581
Using the batch object directly:
7682

7783
.. code-block:: python

newsfragments/3607.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Batching can now be used with the AutoProvider

tests/integration/go_ethereum/test_goethereum_http.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ def w3(geth_process, endpoint_uri):
7676
return Web3(Web3.HTTPProvider(endpoint_uri, request_kwargs={"timeout": 10}))
7777

7878

79+
@pytest.fixture(scope="module")
80+
def auto_w3(geth_process, endpoint_uri):
81+
wait_for_http(endpoint_uri)
82+
83+
from web3.auto import (
84+
w3,
85+
)
86+
87+
return w3
88+
89+
7990
class TestGoEthereumWeb3ModuleTest(GoEthereumWeb3ModuleTest):
8091
pass
8192

@@ -105,7 +116,15 @@ class TestGoEthereumDebugModuleTest(GoEthereumDebugModuleTest):
105116

106117

107118
class TestGoEthereumEthModuleTest(GoEthereumEthModuleTest):
108-
pass
119+
def test_auto_provider_batching(
120+
self,
121+
auto_w3: "Web3",
122+
monkeypatch,
123+
endpoint_uri,
124+
) -> None:
125+
monkeypatch.setenv("WEB3_PROVIDER_URI", endpoint_uri)
126+
# test that batch_requests doesn't error out when using the auto provider
127+
auto_w3.batch_requests()
109128

110129

111130
class TestGoEthereumNetModuleTest(GoEthereumNetModuleTest):

tests/integration/go_ethereum/test_goethereum_ipc.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ def geth_ipc_path(datadir):
5353
os.remove(_geth_ipc_path)
5454

5555

56+
@pytest.fixture(scope="module")
57+
def auto_w3(geth_process, geth_ipc_path):
58+
wait_for_socket(geth_ipc_path)
59+
60+
from web3.auto import (
61+
w3,
62+
)
63+
64+
return w3
65+
66+
5667
@pytest.fixture(scope="module")
5768
def w3(geth_process, geth_ipc_path):
5869
wait_for_socket(geth_ipc_path)
@@ -68,7 +79,15 @@ class TestGoEthereumDebugModuleTest(GoEthereumDebugModuleTest):
6879

6980

7081
class TestGoEthereumEthModuleTest(GoEthereumEthModuleTest):
71-
pass
82+
def test_auto_provider_batching(
83+
self,
84+
auto_w3: "Web3",
85+
monkeypatch,
86+
geth_ipc_path,
87+
) -> None:
88+
monkeypatch.setenv("WEB3_PROVIDER_URI", f"file:///{geth_ipc_path}")
89+
# test that batch_requests doesn't error out when using the auto provider
90+
auto_w3.batch_requests()
7291

7392

7493
class TestGoEthereumNetModuleTest(GoEthereumNetModuleTest):

web3/manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ def _batch_requests(self) -> RequestBatcher[Method[Callable[..., Any]]]:
447447
"""
448448
Context manager for making batch requests
449449
"""
450+
if isinstance(self.provider, AutoProvider):
451+
self.provider = self.provider._get_active_provider(use_cache=True)
450452
if not isinstance(self.provider, (AsyncJSONBaseProvider, JSONBaseProvider)):
451453
raise Web3TypeError("Batch requests are not supported by this provider.")
452454
return RequestBatcher(self.w3)

0 commit comments

Comments
 (0)