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

1""" 

2Sequences of 256-bit values. 

3""" 

4 

5from typing import TYPE_CHECKING, Any, ClassVar, Type, TypeVar 

6 

7from mypy_extensions import mypyc_attr 

8 

9if TYPE_CHECKING: 

10 from .numeric import Uint 

11 

12B = TypeVar("B", bound="FixedBytes") 

13 

14 

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 """ 

21 

22 LENGTH: ClassVar[int] 

23 """ 

24 Number of bytes in each instance of this class. 

25 """ 

26 

27 __slots__ = () 

28 

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 

38 

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 

44 

45 return Uint(self.count(b"\0")) 

46 

47 

48@mypyc_attr(native_class=False) 

49class Bytes0(FixedBytes): 

50 """ 

51 Byte array of exactly zero elements. 

52 """ 

53 

54 LENGTH = 0 

55 """ 

56 Number of bytes in each instance of this class. 

57 """ 

58 

59 

60@mypyc_attr(native_class=False) 

61class Bytes1(FixedBytes): 

62 """ 

63 Byte array of exactly one elements. 

64 """ 

65 

66 LENGTH = 1 

67 """ 

68 Number of bytes in each instance of this class. 

69 """ 

70 

71 

72@mypyc_attr(native_class=False) 

73class Bytes4(FixedBytes): 

74 """ 

75 Byte array of exactly four elements. 

76 """ 

77 

78 LENGTH = 4 

79 """ 

80 Number of bytes in each instance of this class. 

81 """ 

82 

83 

84@mypyc_attr(native_class=False) 

85class Bytes8(FixedBytes): 

86 """ 

87 Byte array of exactly eight elements. 

88 """ 

89 

90 LENGTH = 8 

91 """ 

92 Number of bytes in each instance of this class. 

93 """ 

94 

95 

96@mypyc_attr(native_class=False) 

97class Bytes20(FixedBytes): 

98 """ 

99 Byte array of exactly 20 elements. 

100 """ 

101 

102 LENGTH = 20 

103 """ 

104 Number of bytes in each instance of this class. 

105 """ 

106 

107 

108@mypyc_attr(native_class=False) 

109class Bytes32(FixedBytes): 

110 """ 

111 Byte array of exactly 32 elements. 

112 """ 

113 

114 LENGTH = 32 

115 """ 

116 Number of bytes in each instance of this class. 

117 """ 

118 

119 

120@mypyc_attr(native_class=False) 

121class Bytes48(FixedBytes): 

122 """ 

123 Byte array of exactly 48 elements. 

124 """ 

125 

126 LENGTH = 48 

127 

128 

129@mypyc_attr(native_class=False) 

130class Bytes64(FixedBytes): 

131 """ 

132 Byte array of exactly 64 elements. 

133 """ 

134 

135 LENGTH = 64 

136 """ 

137 Number of bytes in each instance of this class. 

138 """ 

139 

140 

141@mypyc_attr(native_class=False) 

142class Bytes96(FixedBytes): 

143 """ 

144 Byte array of exactly 96 elements. 

145 """ 

146 

147 LENGTH = 96 

148 """ 

149 Number of bytes in each instance of this class. 

150 """ 

151 

152 

153@mypyc_attr(native_class=False) 

154class Bytes256(FixedBytes): 

155 """ 

156 Byte array of exactly 256 elements. 

157 """ 

158 

159 LENGTH = 256 

160 """ 

161 Number of bytes in each instance of this class. 

162 """ 

163 

164 

165Bytes = bytes 

166""" 

167Sequence of bytes (octets) of arbitrary length. 

168"""