Coverage for src / ethereum_types / bytes.py: 100%
58 statements
« prev ^ index » next coverage.py v7.14.0, created at 2026-06-08 11:02 -0400
« prev ^ index » next coverage.py v7.14.0, created at 2026-06-08 11:02 -0400
1"""
2Sequences of 256-bit values.
3"""
5from typing import TYPE_CHECKING, Any, ClassVar, Type, TypeVar
7from mypy_extensions import mypyc_attr
9if TYPE_CHECKING:
10 from .numeric import Uint
12B = TypeVar("B", bound="FixedBytes")
15@mypyc_attr(native_class=False)
16class FixedBytes(bytes):
17 """
18 Superclass for fixed sized byte arrays. Not intended to be used directly,
19 but should be subclassed.
20 """
22 LENGTH: ClassVar[int]
23 """
24 Number of bytes in each instance of this class.
25 """
27 __slots__ = ()
29 def __new__(cls: Type[B], *args: Any, **kwargs: Any) -> B:
30 """
31 Create a new instance, ensuring the result has the correct length.
32 """
33 result = bytes.__new__(cls, *args, **kwargs)
34 if len(result) != cls.LENGTH:
35 message = f"expected {cls.LENGTH} bytes but got {len(result)}"
36 raise ValueError(message)
37 return result
39 def zero_bytes(self) -> "Uint":
40 """
41 Count and return the number of zero bytes in the byte array.
42 """
43 from .numeric import Uint
45 return Uint(self.count(b"\0"))
48@mypyc_attr(native_class=False)
49class Bytes0(FixedBytes):
50 """
51 Byte array of exactly zero elements.
52 """
54 LENGTH = 0
55 """
56 Number of bytes in each instance of this class.
57 """
60@mypyc_attr(native_class=False)
61class Bytes1(FixedBytes):
62 """
63 Byte array of exactly one elements.
64 """
66 LENGTH = 1
67 """
68 Number of bytes in each instance of this class.
69 """
72@mypyc_attr(native_class=False)
73class Bytes4(FixedBytes):
74 """
75 Byte array of exactly four elements.
76 """
78 LENGTH = 4
79 """
80 Number of bytes in each instance of this class.
81 """
84@mypyc_attr(native_class=False)
85class Bytes8(FixedBytes):
86 """
87 Byte array of exactly eight elements.
88 """
90 LENGTH = 8
91 """
92 Number of bytes in each instance of this class.
93 """
96@mypyc_attr(native_class=False)
97class Bytes20(FixedBytes):
98 """
99 Byte array of exactly 20 elements.
100 """
102 LENGTH = 20
103 """
104 Number of bytes in each instance of this class.
105 """
108@mypyc_attr(native_class=False)
109class Bytes32(FixedBytes):
110 """
111 Byte array of exactly 32 elements.
112 """
114 LENGTH = 32
115 """
116 Number of bytes in each instance of this class.
117 """
120@mypyc_attr(native_class=False)
121class Bytes48(FixedBytes):
122 """
123 Byte array of exactly 48 elements.
124 """
126 LENGTH = 48
129@mypyc_attr(native_class=False)
130class Bytes64(FixedBytes):
131 """
132 Byte array of exactly 64 elements.
133 """
135 LENGTH = 64
136 """
137 Number of bytes in each instance of this class.
138 """
141@mypyc_attr(native_class=False)
142class Bytes96(FixedBytes):
143 """
144 Byte array of exactly 96 elements.
145 """
147 LENGTH = 96
148 """
149 Number of bytes in each instance of this class.
150 """
153@mypyc_attr(native_class=False)
154class Bytes256(FixedBytes):
155 """
156 Byte array of exactly 256 elements.
157 """
159 LENGTH = 256
160 """
161 Number of bytes in each instance of this class.
162 """
165Bytes = bytes
166"""
167Sequence of bytes (octets) of arbitrary length.
168"""