Skip to content

Commit 21b4b24

Browse files
committed
Add documentation on ENS strict bytes check flag
1 parent 8d5871a commit 21b4b24

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

docs/abi_types.rst

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

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
31+
There is a boolean flag on the ``Web3`` class and the ``ENS`` class that will disable
32+
strict bytes type checking. This allows bytes values of Python strings and allows byte
33+
strings less than the specified byte size, appropriately padding values that need
34+
padding. To disable stricter checks, set the ``w3.strict_bytes_type_checking``
35+
(or ``ns.strict_bytes_type_checking``) flag to ``False``. This will no longer cause
36+
the ``Web3`` / ``ENS`` instance to raise an error if a Python string is passed in
37+
without a "0x" prefix. It will also render valid byte strings or hex strings
3738
that are below the exact number of bytes specified by the ABI type by padding the value
3839
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+
section for an example on using the flag and more details.
41+
42+
.. note::
43+
If an ``ENS`` class is instantiated from a ``Web3`` instance, i.e.
44+
``ns = ENS.from_web3(w3)``, it will inherit the value of the
45+
``w3.strict_bytes_type_checking`` flag from the ``Web3`` instance at the time of
46+
instantiation.
47+
48+
For more details on the ABI
4049
specification, refer to the
4150
`Solidity ABI Spec <https://docs.soliditylang.org/en/latest/abi-spec.html>`_.
4251

52+
4353
Types by Example
4454
----------------
4555

docs/ens_overview.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,77 @@ The first time it's used, Web3.py will create the ``ens`` instance using
6262
w3.ens.address('ethereum.eth')
6363
6464
65+
.. py:attribute:: ens.strict_bytes_type_checking
66+
67+
The ``ENS`` instance has a ``strict_bytes_type_checking`` flag that toggles the flag
68+
with the same name on the ``Web3`` instance attached to the ``ENS`` instance.
69+
You may disable the stricter bytes type checking that is loaded by default using
70+
this flag. For more examples, see :ref:`disable-strict-byte-check`
71+
72+
If instantiating a standalone ENS instance using ``ENS.from_web3()``, the ENS
73+
instance will inherit the value of the flag on the Web3 instance at time of
74+
instantiation.
75+
76+
.. doctest::
77+
78+
>>> from web3 import Web3, EthereumTesterProvider
79+
>>> from ens import ENS
80+
>>> w3 = Web3(EthereumTesterProvider())
81+
82+
>>> assert w3.strict_bytes_type_checking # assert strict by default
83+
>>> w3.is_encodable('bytes2', b'1')
84+
False
85+
86+
>>> w3.strict_bytes_type_checking = False
87+
>>> w3.is_encodable('bytes2', b'1') # zero-padded, so encoded to: b'1\x00'
88+
True
89+
90+
>>> ns = ENS.from_web3(w3)
91+
>>> # assert inherited from w3 at time of instantiation via ENS.from_web3()
92+
>>> assert ns.strict_bytes_type_checking is False
93+
>>> ns.w3.is_encodable('bytes2', b'1')
94+
True
95+
96+
>>> # assert these are now separate instances
97+
>>> ns.strict_bytes_type_checking = True
98+
>>> ns.w3.is_encodable('bytes2', b'1')
99+
False
100+
101+
>>> # assert w3 flag value remains
102+
>>> assert w3.strict_bytes_type_checking is False
103+
>>> w3.is_encodable('bytes2', b'1')
104+
True
105+
106+
However, if accessing the ``ENS`` class via the ``Web3`` instance as a module
107+
(``w3.ens``), since all modules use the same ``Web3`` object reference
108+
under the the hood (the parent ``w3`` object), changing the
109+
``strict_bytes_type_checking`` flag value on ``w3`` also changes the flag state
110+
for ``w3.ens.w3`` and all modules.
111+
112+
.. doctest::
113+
114+
>>> from web3 import Web3, EthereumTesterProvider
115+
>>> w3 = Web3(EthereumTesterProvider())
116+
117+
>>> assert w3.strict_bytes_type_checking # assert strict by default
118+
>>> w3.is_encodable('bytes2', b'1')
119+
False
120+
121+
>>> w3.strict_bytes_type_checking = False
122+
>>> w3.is_encodable('bytes2', b'1') # zero-padded, so encoded to: b'1\x00'
123+
True
124+
125+
>>> assert w3 == w3.ens.w3 # assert same object
126+
>>> assert not w3.ens.w3.strict_bytes_type_checking
127+
>>> w3.ens.w3.is_encodable('bytes2', b'1')
128+
True
129+
130+
>>> # sanity check on eth module as well
131+
>>> assert not w3.eth.w3.strict_bytes_type_checking
132+
>>> w3.eth.w3.is_encodable('bytes2', b'1')
133+
True
134+
135+
65136
Usage
66137
-----
67138

newsfragments/2788.breaking.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Strict bytes type checking is now default for ``web3.py``. This change also adds a boolean flag for turning this feature on and off.
1+
Strict bytes type checking is now default for ``web3.py``. This change also adds a boolean flag on the ``Web3`` class for turning this feature on and off, as well as a flag on the ``ENS`` class for control over a standalone ``ENS`` instance.

0 commit comments

Comments
 (0)