Skip to content

Commit b0d5322

Browse files
committed
Address comments on PR #2786
1 parent 5073429 commit b0d5322

10 files changed

+60
-2472
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
@@ -490,8 +490,8 @@ size, web3 will invalidate the value. For example, if an abi specifies a type of
490490

491491
However, you may want to be less strict with acceptable values for bytes types.
492492
This may prove useful if you trust that values coming through are what they are
493-
meant to be with respects to the respective ABI. In this case, the automatic padding
494-
might be convenient for inferred types. For this, you can set the
493+
meant to be with respect to the ABI. In this case, the automatic padding might be
494+
convenient for inferred types. For this, you can set the
495495
:meth:`w3.strict_bytes_type_checking` flag to ``False``, which is available on the
496496
web3 instance. A web3 instance which has this flag set to ``False`` will have a less
497497
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/ens.py

Lines changed: 11 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,12 +120,19 @@ 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

128+
@property
129+
def strict_bytes_type_checking(self) -> bool:
130+
return self.w3.strict_bytes_type_checking
131+
132+
@strict_bytes_type_checking.setter
133+
def strict_bytes_type_checking(self, strict_bytes_type_check: bool) -> None:
134+
self.w3.strict_bytes_type_checking = strict_bytes_type_check
135+
129136
def address(self, name: str) -> Optional[ChecksumAddress]:
130137
"""
131138
Look up the Ethereum address that `name` currently points to.

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: 25 additions & 0 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

0 commit comments

Comments
 (0)