Skip to content

Commit fc0f0cd

Browse files
committed
Address comments on PR #2786
1 parent b416e3f commit fc0f0cd

10 files changed

+87
-33
lines changed

docs/abi_types.rst

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ All addresses must be supplied in one of three ways:
2828
Disabling Strict Bytes Type Checking
2929
------------------------------------
3030

31-
There is a method on web3 that will disable strict bytes type checking.
32-
This allows bytes values of Python strings and allows bytestrings less
33-
than the specified byte size. To disable stricter checks, set the
34-
``w3.strict_bytes_type_checking`` flag to ``False``. This will no longer cause the web3
35-
instance to raise an error if a Python string is passed in without a "0x"
36-
prefix. It will also render valid byte strings or hex strings that are below
37-
the exact number of bytes specified by the ABI type. See the
38-
:ref:`disable-strict-byte-check` section for an example and more details.
31+
There is a boolean flag on the web3 instance that will disable strict bytes type checking.
32+
This allows bytes values of Python strings and allows byte strings less
33+
than the specified byte size, appropriately padding values that need padding. To
34+
disable stricter checks, set the ``w3.strict_bytes_type_checking`` flag to ``False``.
35+
This will no longer cause the web3 instance to raise an error if a Python string is
36+
passed in without a "0x" prefix. It will also render valid byte strings or hex strings
37+
that are below the exact number of bytes specified by the ABI type by padding the value
38+
appropriately, according to the ABI type. See the :ref:`disable-strict-byte-check`
39+
section for an example on using the flag and more details. For more details on the ABI
40+
specification, refer to the
41+
`Solidity ABI Spec <https://docs.soliditylang.org/en/latest/abi-spec.html>`_.
3942

4043
Types by Example
4144
----------------

docs/web3.contract.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,8 @@ size, web3 will invalidate the value. For example, if an abi specifies a type of
480480

481481
However, you may want to be less strict with acceptable values for bytes types.
482482
This may prove useful if you trust that values coming through are what they are
483-
meant to be with respects to the respective ABI. In this case, the automatic padding
484-
might be convenient for inferred types. For this, you can set the
483+
meant to be with respect to the ABI. In this case, the automatic padding might be
484+
convenient for inferred types. For this, you can set the
485485
:meth:`w3.strict_bytes_type_checking` flag to ``False``, which is available on the
486486
Web3 instance. A Web3 instance which has this flag set to ``False`` will have a less
487487
strict set of rules on which values are accepted. A ``bytes`` type will allow values as

docs/web3.main.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ Check Encodability
356356
RPC API Modules
357357
~~~~~~~~~~~~~~~
358358

359-
Each ``Web3`` instance also exposes these name-spaced API modules.
359+
Each ``Web3`` instance also exposes these namespaced API modules.
360360

361361

362362
.. py:attribute:: Web3.eth

ens/base_ens.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ class BaseENS:
3838
_resolver_contract: Union[Type["Contract"], Type["AsyncContract"]] = None
3939
_reverse_resolver_contract: Union[Type["Contract"], Type["AsyncContract"]] = None
4040

41+
@property
42+
def strict_bytes_type_checking(self) -> bool:
43+
return self.w3.strict_bytes_type_checking
44+
45+
@strict_bytes_type_checking.setter
46+
def strict_bytes_type_checking(self, strict_bytes_type_check: bool) -> None:
47+
self.w3.strict_bytes_type_checking = strict_bytes_type_check
48+
4149
@staticmethod
4250
@wraps(label_to_hash)
4351
def labelhash(label: str) -> HexBytes:

ens/ens.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __init__(
110110
@classmethod
111111
def from_web3(cls, w3: "Web3", addr: ChecksumAddress = None) -> "ENS":
112112
"""
113-
Generate an ENS instance with web3
113+
Generate an ENS instance from a Web3 instance
114114
115115
:param `web3.Web3` w3: to infer connection, middleware, and codec information
116116
:param hex-string addr: the address of the ENS registry on-chain. If not
@@ -120,9 +120,8 @@ def from_web3(cls, w3: "Web3", addr: ChecksumAddress = None) -> "ENS":
120120
middlewares = w3.middleware_onion.middlewares
121121
ns = cls(cast("BaseProvider", provider), addr=addr, middlewares=middlewares)
122122

123-
# set codec and strict bytes type check flag from w3
124-
ns.w3.strict_bytes_type_checking = w3.strict_bytes_type_checking
125-
ns.w3.codec = w3.codec
123+
# inherit strict bytes checking from w3 instance
124+
ns.strict_bytes_type_checking = w3.strict_bytes_type_checking
126125

127126
return ns
128127

tests/core/contracts/test_contract_call_interface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def nested_tuple_contract(w3, NestedTupleContract, address_conversion_func):
109109
def test_deploy_raises_due_to_strict_byte_checking_by_default(
110110
w3, Bytes32Contract, address_conversion_func
111111
):
112-
with pytest.raises(TypeError, match=""):
112+
with pytest.raises(TypeError):
113113
deploy(
114114
w3,
115115
Bytes32Contract,

tests/core/contracts/test_contract_transact_interface.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ def test_transacting_with_contract_with_bytes32_array_argument(
166166
assert final_value == new_bytes32_array
167167

168168

169-
# TODO: strict by default
170-
def test_transacting_with_contract_with_byte_array_argument_strict(
169+
def test_transacting_with_contract_with_byte_array_argument(
171170
w3, arrays_contract, transact, call
172171
):
173172
new_byte_array = [b"\x03", b"\x03", b"\x03", b"\x03", b"\x03", b"\x03"]
@@ -462,7 +461,6 @@ async def test_async_transacting_with_contract_with_bytes32_array_argument(
462461
assert final_value == new_bytes32_array
463462

464463

465-
# TODO: strict by default
466464
@pytest.mark.asyncio
467465
async def test_async_transacting_with_contract_with_byte_array_argument(
468466
async_w3,

tests/core/filtering/test_utils_functions.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,14 @@ def test_match_fn_with_various_data_types(w3, data, expected, match_data_and_abi
185185
),
186186
),
187187
)
188-
def test_match_fn_with_various_data_types_strict(
189-
w3, data, expected, match_data_and_abi
188+
def test_match_fn_with_various_data_types_non_strict(
189+
w3_non_strict_abi, data, expected, match_data_and_abi
190190
):
191191
abi_types, match_data = zip(*match_data_and_abi)
192-
encoded_data = w3.codec.encode(abi_types, data)
193-
assert match_fn(w3.codec, match_data_and_abi, encoded_data) == expected
192+
encoded_data = w3_non_strict_abi.codec.encode(abi_types, data)
193+
assert (
194+
match_fn(w3_non_strict_abi.codec, match_data_and_abi, encoded_data) == expected
195+
)
194196

195197

196198
@pytest.mark.parametrize(

tests/core/utilities/test_construct_event_topics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ def test_construct_event_topics(w3, arguments, expected):
133133
),
134134
),
135135
)
136-
def test_construct_event_topics_strict(w3, arguments, expected):
137-
actual = construct_event_topic_set(EVENT_1_ABI, w3.codec, arguments)
136+
def test_construct_event_topics_non_strict(w3_non_strict_abi, arguments, expected):
137+
actual = construct_event_topic_set(EVENT_1_ABI, w3_non_strict_abi.codec, arguments)
138138
assert actual == expected
139139

140140

tests/ens/test_ens.py

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,38 @@ def test_w3_ens_setter_sets_w3_object_reference_on_ens(w3):
5454
assert w3 == w3.ens.w3
5555

5656
assert w3.strict_bytes_type_checking
57+
assert w3.ens.strict_bytes_type_checking
5758
assert w3.ens.w3.strict_bytes_type_checking
5859

5960
w3.strict_bytes_type_checking = False
6061
assert not w3.strict_bytes_type_checking
62+
assert not w3.ens.strict_bytes_type_checking
6163
assert not w3.ens.w3.strict_bytes_type_checking
6264

6365

66+
def test_ens_strict_bytes_type_checking_is_distinct_from_w3_instance(w3):
67+
ns = ENS.from_web3(w3)
68+
assert ns.w3 != w3 # assert unique instance for ns
69+
assert ns.w3 == ns._resolver_contract.w3 # assert same instance used under the hood
70+
71+
assert w3.strict_bytes_type_checking
72+
assert ns.strict_bytes_type_checking
73+
74+
w3.strict_bytes_type_checking = False
75+
assert not w3.strict_bytes_type_checking
76+
assert ns.strict_bytes_type_checking
77+
assert ns.w3.strict_bytes_type_checking
78+
assert ns._resolver_contract.w3.strict_bytes_type_checking
79+
80+
w3.strict_bytes_type_checking = True
81+
assert w3.strict_bytes_type_checking
82+
83+
ns.strict_bytes_type_checking = False
84+
assert not ns.strict_bytes_type_checking
85+
assert not ns.w3.strict_bytes_type_checking
86+
assert not ns._resolver_contract.w3.strict_bytes_type_checking
87+
88+
6489
# -- async -- #
6590

6691

@@ -69,17 +94,15 @@ def async_w3():
6994
return Web3(AsyncEthereumTesterProvider())
7095

7196

72-
@pytest.mark.asyncio
73-
async def test_async_from_web3_inherits_web3_middlewares(async_w3):
97+
def test_async_from_web3_inherits_web3_middlewares(async_w3):
7498
test_middleware = async_validation_middleware
7599
async_w3.middleware_onion.add(test_middleware, "test_middleware")
76100

77101
ns = AsyncENS.from_web3(async_w3)
78102
assert ns.w3.middleware_onion.get("test_middleware") == test_middleware
79103

80104

81-
@pytest.mark.asyncio
82-
async def test_async_from_web3_does_not_set_w3_object_reference(async_w3):
105+
def test_async_from_web3_does_not_set_w3_object_reference(async_w3):
83106
assert async_w3.strict_bytes_type_checking
84107

85108
ns = AsyncENS.from_web3(async_w3)
@@ -93,8 +116,7 @@ async def test_async_from_web3_does_not_set_w3_object_reference(async_w3):
93116
assert ns.w3.strict_bytes_type_checking # assert unchanged instance property
94117

95118

96-
@pytest.mark.asyncio
97-
async def test_async_w3_ens_for_empty_ens_sets_w3_object_reference(async_w3):
119+
def test_async_w3_ens_for_empty_ens_sets_w3_object_reference(async_w3):
98120
assert async_w3.strict_bytes_type_checking
99121
assert async_w3.ens.w3.strict_bytes_type_checking
100122

@@ -105,8 +127,7 @@ async def test_async_w3_ens_for_empty_ens_sets_w3_object_reference(async_w3):
105127
assert not async_w3.ens.w3.strict_bytes_type_checking
106128

107129

108-
@pytest.mark.asyncio
109-
async def test_async_w3_ens_setter_sets_w3_object_reference_on_ens(async_w3):
130+
def test_async_w3_ens_setter_sets_w3_object_reference_on_ens(async_w3):
110131
ns = AsyncENS.from_web3(async_w3)
111132
async_w3.ens = ns
112133
assert ns == async_w3.ens
@@ -118,3 +139,26 @@ async def test_async_w3_ens_setter_sets_w3_object_reference_on_ens(async_w3):
118139
async_w3.strict_bytes_type_checking = False
119140
assert not async_w3.strict_bytes_type_checking
120141
assert not async_w3.ens.w3.strict_bytes_type_checking
142+
143+
144+
def test_async_ens_strict_bytes_type_checking_is_distinct_from_w3_instance(async_w3):
145+
ns = AsyncENS.from_web3(async_w3)
146+
assert ns.w3 != async_w3 # assert unique instance for ns
147+
assert ns.w3 == ns._resolver_contract.w3 # assert same instance used under the hood
148+
149+
assert async_w3.strict_bytes_type_checking
150+
assert ns.strict_bytes_type_checking
151+
152+
async_w3.strict_bytes_type_checking = False
153+
assert not async_w3.strict_bytes_type_checking
154+
assert ns.strict_bytes_type_checking
155+
assert ns.w3.strict_bytes_type_checking
156+
assert ns._resolver_contract.w3.strict_bytes_type_checking
157+
158+
async_w3.strict_bytes_type_checking = True
159+
assert async_w3.strict_bytes_type_checking
160+
161+
ns.strict_bytes_type_checking = False
162+
assert not ns.strict_bytes_type_checking
163+
assert not ns.w3.strict_bytes_type_checking
164+
assert not ns._resolver_contract.w3.strict_bytes_type_checking

0 commit comments

Comments
 (0)