Skip to content

Commit

Permalink
Add black to web3/middleware (ethereum#2521)
Browse files Browse the repository at this point in the history
* Add black to web3/middleware

* Add newsfragment and SHA to ignore

* Revert changes to .git-blame-ignore-revs
  • Loading branch information
kclowes authored Jun 17, 2022
1 parent 10edeb1 commit ca1dbbd
Show file tree
Hide file tree
Showing 17 changed files with 463 additions and 379 deletions.
1 change: 1 addition & 0 deletions newsfragments/2521.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add black linting to ``web3/middleware`` directory
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ basepython=python
extras=linter
commands=
flake8 {toxinidir}/web3 {toxinidir}/ens {toxinidir}/ethpm {toxinidir}/tests --exclude {toxinidir}/ethpm/ethpm-spec
black {toxinidir}/ethpm {toxinidir}/web3/auto {toxinidir}/web3/utils {toxinidir}/web3/_utils {toxinidir}/web3/beacon {toxinidir}/web3/gas_strategies {toxinidir}/web3/scripts {toxinidir}/web3/providers --exclude {toxinidir}/ethpm/ethpm-spec --check
black {toxinidir}/ethpm {toxinidir}/web3/auto {toxinidir}/web3/utils {toxinidir}/web3/_utils {toxinidir}/web3/beacon {toxinidir}/web3/gas_strategies {toxinidir}/web3/scripts {toxinidir}/web3/providers {toxinidir}/web3/middleware --exclude {toxinidir}/ethpm/ethpm-spec --check
isort --recursive --check-only --diff {toxinidir}/web3/ {toxinidir}/ens/ {toxinidir}/ethpm/ {toxinidir}/tests/
mypy -p web3 -p ethpm -p ens --config-file {toxinidir}/mypy.ini

Expand Down
12 changes: 5 additions & 7 deletions web3/middleware/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@

def combine_middlewares(
middlewares: Sequence[Middleware],
w3: 'Web3',
provider_request_fn: Callable[[RPCEndpoint, Any], Any]
w3: "Web3",
provider_request_fn: Callable[[RPCEndpoint, Any], Any],
) -> Callable[..., RPCResponse]:
"""
Returns a callable function which will call the provider.provider_request
Expand All @@ -96,8 +96,8 @@ def combine_middlewares(

async def async_combine_middlewares(
middlewares: Sequence[Middleware],
w3: 'Web3',
provider_request_fn: Callable[[RPCEndpoint, Any], Any]
w3: "Web3",
provider_request_fn: Callable[[RPCEndpoint, Any], Any],
) -> Callable[..., RPCResponse]:
"""
Returns a callable function which will call the provider.provider_request
Expand All @@ -110,8 +110,6 @@ async def async_combine_middlewares(


async def construct_middleware(
middleware: Middleware,
fn: Callable[..., RPCResponse],
w3: 'Web3'
middleware: Middleware, fn: Callable[..., RPCResponse], w3: "Web3"
) -> Callable[[RPCEndpoint, Any], Any]:
return await middleware(fn, w3)
8 changes: 5 additions & 3 deletions web3/middleware/attrdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ def attrdict_middleware(
"""
Converts any result which is a dictionary into an a
"""

def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
response = make_request(method, params)

if 'result' in response:
result = response['result']
if "result" in response:
result = response["result"]
if is_dict(result) and not isinstance(result, AttributeDict):
return assoc(response, 'result', AttributeDict.recursive(result))
return assoc(response, "result", AttributeDict.recursive(result))
else:
return response
else:
return response

return middleware
18 changes: 8 additions & 10 deletions web3/middleware/buffered_gas_estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,30 @@ def buffered_gas_estimate_middleware(
make_request: Callable[[RPCEndpoint, Any], Any], w3: "Web3"
) -> Callable[[RPCEndpoint, Any], RPCResponse]:
def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
if method == 'eth_sendTransaction':
if method == "eth_sendTransaction":
transaction = params[0]
if 'gas' not in transaction:
if "gas" not in transaction:
transaction = assoc(
transaction,
'gas',
"gas",
hex(get_buffered_gas_estimate(w3, transaction)),
)
return make_request(method, [transaction])
return make_request(method, params)

return middleware


async def async_buffered_gas_estimate_middleware(
make_request: Callable[[RPCEndpoint, Any], Any], w3: "Web3"
) -> AsyncMiddleware:
async def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
if method == 'eth_sendTransaction':
if method == "eth_sendTransaction":
transaction = params[0]
if 'gas' not in transaction:
if "gas" not in transaction:
gas_estimate = await async_get_buffered_gas_estimate(w3, transaction)
transaction = assoc(
transaction,
'gas',
hex(gas_estimate)
)
transaction = assoc(transaction, "gas", hex(gas_estimate))
return await make_request(method, [transaction])
return await make_request(method, params)

return middleware
Loading

0 comments on commit ca1dbbd

Please sign in to comment.