Skip to content

Commit 39f4453

Browse files
authored
Add black to web3's public utils (#2513)
* Add black to web3's public utils * Add newsfragment
1 parent 350126e commit 39f4453

File tree

4 files changed

+71
-61
lines changed

4 files changed

+71
-61
lines changed

newsfragments/2513.misc.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add black checks to ``web3/utils`` directory

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ basepython=python
6464
extras=linter
6565
commands=
6666
flake8 {toxinidir}/web3 {toxinidir}/ens {toxinidir}/ethpm {toxinidir}/tests --exclude {toxinidir}/ethpm/ethpm-spec
67-
black {toxinidir}/ethpm --exclude {toxinidir}/ethpm/ethpm-spec --check
67+
black {toxinidir}/ethpm {toxinidir}/web3/utils --exclude {toxinidir}/ethpm/ethpm-spec --check
6868
isort --recursive --check-only --diff {toxinidir}/web3/ {toxinidir}/ens/ {toxinidir}/ethpm/ {toxinidir}/tests/
6969
mypy -p web3 -p ethpm -p ens --config-file {toxinidir}/mypy.ini
7070

web3/utils/async_exception_handling.py

+33-30
Original file line numberDiff line numberDiff line change
@@ -32,61 +32,64 @@ async def async_handle_offchain_lookup(
3232
offchain_lookup_payload: Dict[str, Any],
3333
transaction: TxParams,
3434
) -> bytes:
35-
formatted_sender = to_hex_if_bytes(offchain_lookup_payload['sender']).lower()
36-
formatted_data = to_hex_if_bytes(offchain_lookup_payload['callData']).lower()
35+
formatted_sender = to_hex_if_bytes(offchain_lookup_payload["sender"]).lower()
36+
formatted_data = to_hex_if_bytes(offchain_lookup_payload["callData"]).lower()
3737

38-
if formatted_sender != to_hex_if_bytes(transaction['to']).lower():
38+
if formatted_sender != to_hex_if_bytes(transaction["to"]).lower():
3939
raise ValidationError(
40-
'Cannot handle OffchainLookup raised inside nested call. Returned `sender` value does '
41-
'not equal `to` address in transaction.'
40+
"Cannot handle OffchainLookup raised inside nested call. Returned `sender` value does "
41+
"not equal `to` address in transaction."
4242
)
4343

44-
for url in offchain_lookup_payload['urls']:
44+
for url in offchain_lookup_payload["urls"]:
4545
formatted_url = URI(
4646
str(url)
47-
.replace('{sender}', str(formatted_sender))
48-
.replace('{data}', str(formatted_data))
47+
.replace("{sender}", str(formatted_sender))
48+
.replace("{data}", str(formatted_data))
4949
)
5050

5151
try:
52-
if '{data}' in url and '{sender}' in url:
52+
if "{data}" in url and "{sender}" in url:
5353
response = await async_get_response_from_get_request(formatted_url)
54-
elif '{sender}' in url:
55-
response = await async_get_response_from_post_request(formatted_url, data={
56-
"data": formatted_data,
57-
"sender": formatted_sender
58-
})
54+
elif "{sender}" in url:
55+
response = await async_get_response_from_post_request(
56+
formatted_url,
57+
data={"data": formatted_data, "sender": formatted_sender},
58+
)
5959
else:
60-
raise ValidationError('url not formatted properly.')
60+
raise ValidationError("url not formatted properly.")
6161
except Exception:
6262
continue # try next url if timeout or issues making the request
6363

64-
if 400 <= response.status <= 499: # if request returns 400 error, raise exception
64+
if (
65+
400 <= response.status <= 499
66+
): # if request returns 400 error, raise exception
6567
response.raise_for_status()
6668
if not 200 <= response.status <= 299: # if not 400 error, try next url
6769
continue
6870

6971
result = await async_get_json_from_client_response(response)
7072

71-
if 'data' not in result.keys():
73+
if "data" not in result.keys():
7274
raise ValidationError(
7375
"Improperly formatted response for offchain lookup HTTP request - missing 'data' "
7476
"field."
7577
)
7678

77-
encoded_data_with_function_selector = b''.join([
78-
# 4-byte callback function selector
79-
to_bytes_if_hex(offchain_lookup_payload['callbackFunction']),
80-
81-
# encode the `data` from the result and the `extraData` as bytes
82-
encode_abi(
83-
['bytes', 'bytes'],
84-
[
85-
to_bytes_if_hex(result['data']),
86-
to_bytes_if_hex(offchain_lookup_payload['extraData']),
87-
]
88-
)
89-
])
79+
encoded_data_with_function_selector = b"".join(
80+
[
81+
# 4-byte callback function selector
82+
to_bytes_if_hex(offchain_lookup_payload["callbackFunction"]),
83+
# encode the `data` from the result and the `extraData` as bytes
84+
encode_abi(
85+
["bytes", "bytes"],
86+
[
87+
to_bytes_if_hex(result["data"]),
88+
to_bytes_if_hex(offchain_lookup_payload["extraData"]),
89+
],
90+
),
91+
]
92+
)
9093

9194
return encoded_data_with_function_selector
9295
raise MultipleFailedRequests("Offchain lookup failed for supplied urls.")

web3/utils/exception_handling.py

+36-30
Original file line numberDiff line numberDiff line change
@@ -31,61 +31,67 @@ def handle_offchain_lookup(
3131
offchain_lookup_payload: Dict[str, Any],
3232
transaction: TxParams,
3333
) -> bytes:
34-
formatted_sender = to_hex_if_bytes(offchain_lookup_payload['sender']).lower()
35-
formatted_data = to_hex_if_bytes(offchain_lookup_payload['callData']).lower()
34+
formatted_sender = to_hex_if_bytes(offchain_lookup_payload["sender"]).lower()
35+
formatted_data = to_hex_if_bytes(offchain_lookup_payload["callData"]).lower()
3636

37-
if formatted_sender != to_hex_if_bytes(transaction['to']).lower():
37+
if formatted_sender != to_hex_if_bytes(transaction["to"]).lower():
3838
raise ValidationError(
39-
'Cannot handle OffchainLookup raised inside nested call. Returned `sender` value does '
40-
'not equal `to` address in transaction.'
39+
"Cannot handle OffchainLookup raised inside nested call. Returned `sender` value does "
40+
"not equal `to` address in transaction."
4141
)
4242

43-
for url in offchain_lookup_payload['urls']:
43+
for url in offchain_lookup_payload["urls"]:
4444
formatted_url = URI(
4545
str(url)
46-
.replace('{sender}', str(formatted_sender))
47-
.replace('{data}', str(formatted_data))
46+
.replace("{sender}", str(formatted_sender))
47+
.replace("{data}", str(formatted_data))
4848
)
4949

5050
try:
51-
if '{data}' in url and '{sender}' in url:
51+
if "{data}" in url and "{sender}" in url:
5252
response = get_response_from_get_request(formatted_url)
53-
elif '{sender}' in url:
54-
response = get_response_from_post_request(formatted_url, data={
55-
"data": formatted_data,
56-
"sender": formatted_sender,
57-
})
53+
elif "{sender}" in url:
54+
response = get_response_from_post_request(
55+
formatted_url,
56+
data={
57+
"data": formatted_data,
58+
"sender": formatted_sender,
59+
},
60+
)
5861
else:
59-
raise ValidationError('url not formatted properly.')
62+
raise ValidationError("url not formatted properly.")
6063
except Exception:
6164
continue # try next url if timeout or issues making the request
6265

63-
if 400 <= response.status_code <= 499: # if request returns 400 error, raise exception
66+
if (
67+
400 <= response.status_code <= 499
68+
): # if request returns 400 error, raise exception
6469
response.raise_for_status()
6570
if not 200 <= response.status_code <= 299: # if not 400 error, try next url
6671
continue
6772

6873
result = response.json()
6974

70-
if 'data' not in result.keys():
75+
if "data" not in result.keys():
7176
raise ValidationError(
7277
"Improperly formatted response for offchain lookup HTTP request - missing 'data' "
7378
"field."
7479
)
7580

76-
encoded_data_with_function_selector = b''.join([
77-
# 4-byte callback function selector
78-
to_bytes_if_hex(offchain_lookup_payload['callbackFunction']),
79-
80-
# encode the `data` from the result and the `extraData` as bytes
81-
encode_abi(
82-
['bytes', 'bytes'],
83-
[
84-
to_bytes_if_hex(result['data']),
85-
to_bytes_if_hex(offchain_lookup_payload['extraData']),
86-
]
87-
)
88-
])
81+
encoded_data_with_function_selector = b"".join(
82+
[
83+
# 4-byte callback function selector
84+
to_bytes_if_hex(offchain_lookup_payload["callbackFunction"]),
85+
# encode the `data` from the result and the `extraData` as bytes
86+
encode_abi(
87+
["bytes", "bytes"],
88+
[
89+
to_bytes_if_hex(result["data"]),
90+
to_bytes_if_hex(offchain_lookup_payload["extraData"]),
91+
],
92+
),
93+
]
94+
)
8995

9096
return encoded_data_with_function_selector
9197
raise MultipleFailedRequests("Offchain lookup failed for supplied urls.")

0 commit comments

Comments
 (0)