faster-eth-utils 5.3.8__cp313-cp313-win_amd64.whl → 5.3.23__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. faster_eth_utils/abi.cp313-win_amd64.pyd +0 -0
  2. faster_eth_utils/abi.py +95 -70
  3. faster_eth_utils/address.cp313-win_amd64.pyd +0 -0
  4. faster_eth_utils/address.py +7 -14
  5. faster_eth_utils/applicators.cp313-win_amd64.pyd +0 -0
  6. faster_eth_utils/applicators.py +73 -56
  7. faster_eth_utils/conversions.cp313-win_amd64.pyd +0 -0
  8. faster_eth_utils/conversions.py +18 -20
  9. faster_eth_utils/crypto.cp313-win_amd64.pyd +0 -0
  10. faster_eth_utils/crypto.py +3 -8
  11. faster_eth_utils/currency.cp313-win_amd64.pyd +0 -0
  12. faster_eth_utils/currency.py +11 -8
  13. faster_eth_utils/curried/__init__.py +62 -65
  14. faster_eth_utils/debug.cp313-win_amd64.pyd +0 -0
  15. faster_eth_utils/decorators.cp313-win_amd64.pyd +0 -0
  16. faster_eth_utils/decorators.py +65 -29
  17. faster_eth_utils/encoding.cp313-win_amd64.pyd +0 -0
  18. faster_eth_utils/encoding.py +1 -1
  19. faster_eth_utils/exceptions.cp313-win_amd64.pyd +0 -0
  20. faster_eth_utils/exceptions.py +8 -1
  21. faster_eth_utils/functional.cp313-win_amd64.pyd +0 -0
  22. faster_eth_utils/functional.py +10 -12
  23. faster_eth_utils/hexadecimal.cp313-win_amd64.pyd +0 -0
  24. faster_eth_utils/hexadecimal.py +8 -12
  25. faster_eth_utils/humanize.cp313-win_amd64.pyd +0 -0
  26. faster_eth_utils/humanize.py +16 -16
  27. faster_eth_utils/logging.py +51 -44
  28. faster_eth_utils/module_loading.cp313-win_amd64.pyd +0 -0
  29. faster_eth_utils/network.cp313-win_amd64.pyd +0 -0
  30. faster_eth_utils/network.py +2 -3
  31. faster_eth_utils/numeric.cp313-win_amd64.pyd +0 -0
  32. faster_eth_utils/pydantic.py +5 -7
  33. faster_eth_utils/toolz.cp313-win_amd64.pyd +0 -0
  34. faster_eth_utils/types.cp313-win_amd64.pyd +0 -0
  35. faster_eth_utils/types.py +7 -10
  36. faster_eth_utils/units.cp313-win_amd64.pyd +0 -0
  37. {faster_eth_utils-5.3.8.dist-info → faster_eth_utils-5.3.23.dist-info}/METADATA +39 -19
  38. faster_eth_utils-5.3.23.dist-info/RECORD +53 -0
  39. faster_eth_utils-5.3.23.dist-info/top_level.txt +3 -0
  40. faster_eth_utils__mypyc.cp313-win_amd64.pyd +0 -0
  41. 99c07adba6ff961eaf3e__mypyc.cp313-win_amd64.pyd +0 -0
  42. faster_eth_utils-5.3.8.dist-info/RECORD +0 -53
  43. faster_eth_utils-5.3.8.dist-info/top_level.txt +0 -3
  44. {faster_eth_utils-5.3.8.dist-info → faster_eth_utils-5.3.23.dist-info}/WHEEL +0 -0
  45. {faster_eth_utils-5.3.8.dist-info → faster_eth_utils-5.3.23.dist-info}/licenses/LICENSE +0 -0
@@ -1,9 +1,8 @@
1
1
  from typing import (
2
- Callable,
3
- Optional,
4
2
  TypeVar,
5
3
  Union,
6
4
  )
5
+ from collections.abc import Callable
7
6
 
8
7
  from eth_typing import (
9
8
  HexStr,
@@ -31,9 +30,9 @@ BytesLike = Union[Primitives, bytearray, memoryview]
31
30
 
32
31
 
33
32
  def to_hex(
34
- primitive: Optional[BytesLike] = None,
35
- hexstr: Optional[HexStr] = None,
36
- text: Optional[str] = None,
33
+ primitive: BytesLike | None = None,
34
+ hexstr: HexStr | None = None,
35
+ text: str | None = None,
37
36
  ) -> HexStr:
38
37
  """
39
38
  Auto converts any supported value into its hex representation.
@@ -71,9 +70,9 @@ def to_hex(
71
70
 
72
71
 
73
72
  def to_int(
74
- primitive: Optional[BytesLike] = None,
75
- hexstr: Optional[HexStr] = None,
76
- text: Optional[str] = None,
73
+ primitive: BytesLike | None = None,
74
+ hexstr: HexStr | None = None,
75
+ text: str | None = None,
77
76
  ) -> int:
78
77
  """
79
78
  Converts value to its integer representation.
@@ -107,9 +106,9 @@ def to_int(
107
106
 
108
107
 
109
108
  def to_bytes(
110
- primitive: Optional[BytesLike] = None,
111
- hexstr: Optional[HexStr] = None,
112
- text: Optional[str] = None,
109
+ primitive: BytesLike | None = None,
110
+ hexstr: HexStr | None = None,
111
+ text: str | None = None,
113
112
  ) -> bytes:
114
113
  if isinstance(primitive, bool):
115
114
  return b"\x01" if primitive else b"\x00"
@@ -118,10 +117,10 @@ def to_bytes(
118
117
  elif isinstance(primitive, bytes):
119
118
  return primitive
120
119
  elif isinstance(primitive, int):
121
- return to_bytes(hexstr=to_hex(primitive))
120
+ return int_to_big_endian(primitive)
122
121
  elif hexstr is not None:
123
122
  if len(hexstr) % 2:
124
- hexstr = "0x0" + remove_0x_prefix(hexstr) # type: ignore [assignment]
123
+ hexstr = HexStr(f"0x0{remove_0x_prefix(hexstr)}")
125
124
  return decode_hex(hexstr)
126
125
  elif text is not None:
127
126
  return text.encode("utf-8")
@@ -132,9 +131,9 @@ def to_bytes(
132
131
 
133
132
 
134
133
  def to_text(
135
- primitive: Optional[BytesLike] = None,
136
- hexstr: Optional[HexStr] = None,
137
- text: Optional[str] = None,
134
+ primitive: BytesLike | None = None,
135
+ hexstr: HexStr | None = None,
136
+ text: str | None = None,
138
137
  ) -> str:
139
138
  if hexstr is not None:
140
139
  return to_bytes(hexstr=hexstr).decode("utf-8")
@@ -147,13 +146,12 @@ def to_text(
147
146
  elif isinstance(primitive, memoryview):
148
147
  return bytes(primitive).decode("utf-8")
149
148
  elif isinstance(primitive, int):
150
- byte_encoding = int_to_big_endian(primitive)
151
- return to_text(byte_encoding)
149
+ return int_to_big_endian(primitive).decode("utf-8")
152
150
  raise TypeError("Expected an int, bytes, bytearray or hexstr.")
153
151
 
154
152
 
155
153
  def text_if_str(
156
- to_type: Callable[..., T], text_or_primitive: Union[bytes, int, str]
154
+ to_type: Callable[..., T], text_or_primitive: bytes | int | str
157
155
  ) -> T:
158
156
  """
159
157
  Convert to a type, assuming that strings can be only unicode text (not a hexstr).
@@ -169,7 +167,7 @@ def text_if_str(
169
167
 
170
168
 
171
169
  def hexstr_if_str(
172
- to_type: Callable[..., T], hexstr_or_primitive: Union[bytes, int, str]
170
+ to_type: Callable[..., T], hexstr_or_primitive: bytes | int | str
173
171
  ) -> T:
174
172
  """
175
173
  Convert to a type, assuming that strings can be only hexstr (not unicode text).
Binary file
@@ -1,8 +1,3 @@
1
- from typing import (
2
- Optional,
3
- Union,
4
- )
5
-
6
1
  from eth_hash.auto import (
7
2
  keccak as keccak_256,
8
3
  )
@@ -14,8 +9,8 @@ from .conversions import (
14
9
 
15
10
 
16
11
  def keccak(
17
- primitive: Optional[Union[bytes, int, bool]] = None,
18
- hexstr: Optional[HexStr] = None,
19
- text: Optional[str] = None,
12
+ primitive: bytes | int | bool | None = None,
13
+ hexstr: HexStr | None = None,
14
+ text: str | None = None,
20
15
  ) -> bytes:
21
16
  return bytes(keccak_256(to_bytes(primitive, hexstr, text)))
@@ -46,11 +46,12 @@ class denoms:
46
46
 
47
47
  MIN_WEI: Final = 0
48
48
  MAX_WEI: Final = 2**256 - 1
49
+ DECIMAL_ZERO: Final = decimal.Decimal(0)
49
50
 
50
51
  _NumberType = Union[int, float, str, decimal.Decimal]
51
52
 
52
53
 
53
- def _from_wei(number: int, unit_value: decimal.Decimal) -> Union[int, decimal.Decimal]:
54
+ def _from_wei(number: int, unit_value: decimal.Decimal) -> int | decimal.Decimal:
54
55
  if number == 0:
55
56
  return 0
56
57
 
@@ -75,7 +76,7 @@ def _to_wei(number: _NumberType, unit_value: decimal.Decimal) -> int:
75
76
  else:
76
77
  raise TypeError("Unsupported type. Must be one of integer, float, or string")
77
78
 
78
- if d_number == decimal.Decimal(0):
79
+ if d_number == DECIMAL_ZERO:
79
80
  return 0
80
81
 
81
82
  s_number = str(number)
@@ -97,14 +98,15 @@ def _to_wei(number: _NumberType, unit_value: decimal.Decimal) -> int:
97
98
  return int(result_value)
98
99
 
99
100
 
100
- def from_wei(number: int, unit: str) -> Union[int, decimal.Decimal]:
101
+ def from_wei(number: int, unit: str) -> int | decimal.Decimal:
101
102
  """
102
103
  Takes a number of wei and converts it to any other ether unit.
103
104
  """
104
- if unit.lower() not in units:
105
+ unit_key = unit.lower()
106
+ if unit_key not in units:
105
107
  raise ValueError(f"Unknown unit. Must be one of {'/'.join(units.keys())}")
106
108
 
107
- unit_value = units[unit.lower()]
109
+ unit_value = units[unit_key]
108
110
 
109
111
  return _from_wei(number, unit_value)
110
112
 
@@ -113,15 +115,16 @@ def to_wei(number: _NumberType, unit: str) -> int:
113
115
  """
114
116
  Takes a number of a unit and converts it to wei.
115
117
  """
116
- if unit.lower() not in units:
118
+ unit_key = unit.lower()
119
+ if unit_key not in units:
117
120
  raise ValueError(f"Unknown unit. Must be one of {'/'.join(units.keys())}")
118
121
 
119
- unit_value = units[unit.lower()]
122
+ unit_value = units[unit_key]
120
123
 
121
124
  return _to_wei(number, unit_value)
122
125
 
123
126
 
124
- def from_wei_decimals(number: int, decimals: int) -> Union[int, decimal.Decimal]:
127
+ def from_wei_decimals(number: int, decimals: int) -> int | decimal.Decimal:
125
128
  """
126
129
  Takes a number of wei and converts it to a decimal with the specified
127
130
  number of decimals.
@@ -1,11 +1,10 @@
1
+ import sys
2
+ from collections.abc import Callable, Generator, Sequence
1
3
  from typing import (
4
+ TYPE_CHECKING,
2
5
  Any,
3
- Callable,
4
- Dict,
5
- Generator,
6
6
  Optional,
7
- Sequence,
8
- Tuple,
7
+ TypeGuard,
9
8
  TypeVar,
10
9
  Union,
11
10
  overload,
@@ -118,138 +117,137 @@ from faster_eth_utils.toolz import (
118
117
  curry,
119
118
  )
120
119
 
120
+ if TYPE_CHECKING:
121
+ from _typeshed import SupportsBool
122
+ # We have to sacrifice a little bit of specificity on dinosaur Python3.8
123
+
124
+
125
+ TArg = TypeVar("TArg")
126
+ TOther = TypeVar("TOther")
121
127
  TReturn = TypeVar("TReturn")
122
128
  TValue = TypeVar("TValue")
123
129
 
124
130
 
125
131
  @overload
126
132
  def apply_formatter_if(
127
- condition: Callable[..., bool],
128
- ) -> Callable[[Callable[..., TReturn]], Callable[[TValue], Union[TReturn, TValue]]]:
129
- pass
130
-
133
+ condition: Callable[[TArg], TypeGuard[TOther]],
134
+ ) -> Callable[[Callable[[TOther], TReturn]], Callable[[TArg], TReturn | TArg]]:
135
+ ...
131
136
 
132
137
  @overload
133
138
  def apply_formatter_if(
134
- condition: Callable[..., bool], formatter: Callable[..., TReturn]
135
- ) -> Callable[[TValue], Union[TReturn, TValue]]:
136
- pass
137
-
139
+ condition: Callable[[TArg], TypeGuard[TOther]], formatter: Callable[[TOther], TReturn]
140
+ ) -> Callable[[TArg], TReturn | TArg]:
141
+ ...
138
142
 
139
143
  @overload
140
144
  def apply_formatter_if(
141
- condition: Callable[..., bool], formatter: Callable[..., TReturn], value: TValue
142
- ) -> Union[TReturn, TValue]:
143
- pass
145
+ condition: Callable[[TArg], TypeGuard[TOther]], formatter: Callable[[TOther], TReturn], value: TArg
146
+ ) -> TReturn | TArg:
147
+ ...
144
148
 
149
+ @overload
150
+ def apply_formatter_if(
151
+ condition: Callable[[TArg], bool], formatter: Callable[[TArg], TReturn], value: TArg
152
+ ) -> TReturn | TArg:
153
+ ...
145
154
 
146
- # This is just a stub to appease mypy, it gets overwritten later
147
155
  def apply_formatter_if( # type: ignore
148
- condition: Callable[..., bool],
149
- formatter: Optional[Callable[..., TReturn]] = None,
150
- value: Optional[TValue] = None,
151
- ) -> Union[
152
- Callable[[Callable[..., TReturn]], Callable[[TValue], Union[TReturn, TValue]]],
153
- Callable[[TValue], Union[TReturn, TValue]],
154
- TReturn,
155
- TValue,
156
- ]:
156
+ condition: Callable[[TArg], TypeGuard[TOther]] | Callable[[TArg], bool],
157
+ formatter: Callable[[TOther], TReturn] | Callable[[TArg], TReturn] | None = None,
158
+ value: TArg | None = None,
159
+ ) -> (
160
+ Callable[[Callable[[TOther], TReturn]], Callable[[TArg], TReturn | TArg]] |
161
+ Callable[[TArg], TReturn | TArg] |
162
+ TReturn |
163
+ TArg
164
+ ):
157
165
  pass
158
166
 
159
167
 
160
168
  @overload
161
169
  def apply_one_of_formatters(
162
170
  formatter_condition_pairs: Sequence[
163
- Tuple[Callable[..., bool], Callable[..., TReturn]]
171
+ tuple[Callable[[TArg], "SupportsBool"], Callable[[TArg], TReturn]]
164
172
  ],
165
- ) -> Callable[[TValue], TReturn]:
166
- ...
173
+ ) -> Callable[[TArg], TReturn]: ...
167
174
 
168
175
 
169
176
  @overload
170
177
  def apply_one_of_formatters(
171
178
  formatter_condition_pairs: Sequence[
172
- Tuple[Callable[..., bool], Callable[..., TReturn]]
179
+ tuple[Callable[[TArg], "SupportsBool"], Callable[[TArg], TReturn]]
173
180
  ],
174
- value: TValue,
175
- ) -> TReturn:
176
- ...
181
+ value: TArg,
182
+ ) -> TReturn: ...
177
183
 
178
184
 
179
185
  # This is just a stub to appease mypy, it gets overwritten later
180
- def apply_one_of_formatters( # type: ignore
186
+ def apply_one_of_formatters( # type: ignore[empty-body]
181
187
  formatter_condition_pairs: Sequence[
182
- Tuple[Callable[..., bool], Callable[..., TReturn]]
188
+ tuple[Callable[[TArg], "SupportsBool"], Callable[[TArg], TReturn]]
183
189
  ],
184
- value: Optional[TValue] = None,
185
- ) -> TReturn:
186
- ...
190
+ value: TArg | None = None,
191
+ ) -> Callable[[TArg], TReturn] | TReturn: ...
187
192
 
188
193
 
189
194
  @overload
190
195
  def hexstr_if_str(
191
196
  to_type: Callable[..., TReturn],
192
- ) -> Callable[[Union[bytes, int, str]], TReturn]:
193
- ...
197
+ ) -> Callable[[bytes | int | str], TReturn]: ...
194
198
 
195
199
 
196
200
  @overload
197
201
  def hexstr_if_str(
198
- to_type: Callable[..., TReturn], to_format: Union[bytes, int, str]
199
- ) -> TReturn:
200
- ...
202
+ to_type: Callable[..., TReturn], to_format: bytes | int | str
203
+ ) -> TReturn: ...
201
204
 
202
205
 
203
206
  # This is just a stub to appease mypy, it gets overwritten later
204
207
  def hexstr_if_str( # type: ignore
205
- to_type: Callable[..., TReturn], to_format: Optional[Union[bytes, int, str]] = None
206
- ) -> TReturn:
207
- ...
208
+ to_type: Callable[..., TReturn], to_format: bytes | int | str | None = None
209
+ ) -> TReturn: ...
208
210
 
209
211
 
210
212
  @overload
211
213
  def text_if_str(
212
214
  to_type: Callable[..., TReturn],
213
- ) -> Callable[[Union[bytes, int, str]], TReturn]:
214
- ...
215
+ ) -> Callable[[bytes | int | str], TReturn]: ...
215
216
 
216
217
 
217
218
  @overload
218
219
  def text_if_str(
219
- to_type: Callable[..., TReturn], text_or_primitive: Union[bytes, int, str]
220
- ) -> TReturn:
221
- ...
220
+ to_type: Callable[..., TReturn], text_or_primitive: bytes | int | str
221
+ ) -> TReturn: ...
222
222
 
223
223
 
224
224
  # This is just a stub to appease mypy, it gets overwritten later
225
225
  def text_if_str( # type: ignore
226
226
  to_type: Callable[..., TReturn],
227
- text_or_primitive: Optional[Union[bytes, int, str]] = None,
228
- ) -> TReturn:
229
- ...
227
+ text_or_primitive: bytes | int | str | None = None,
228
+ ) -> TReturn: ...
230
229
 
231
230
 
232
231
  @overload
233
232
  def apply_formatters_to_dict(
234
- formatters: Dict[Any, Any], unaliased: bool = False
235
- ) -> Callable[[Dict[Any, Any]], TReturn]:
233
+ formatters: dict[Any, Any], unaliased: bool = False
234
+ ) -> Callable[[dict[Any, Any] | CamelModel], dict[Any, Any]]:
236
235
  ...
237
236
 
238
237
 
239
238
  @overload
240
239
  def apply_formatters_to_dict(
241
- formatters: Dict[Any, Any], value: Union[Dict[Any, Any], CamelModel]
242
- ) -> Dict[Any, Any]:
240
+ formatters: dict[Any, Any], value: dict[Any, Any] | CamelModel, unaliased: bool = False
241
+ ) -> dict[Any, Any]:
243
242
  ...
244
243
 
245
244
 
246
245
  # This is just a stub to appease mypy, it gets overwritten later
247
- def apply_formatters_to_dict( # type: ignore
248
- formatters: Dict[Any, Any],
249
- value: Optional[Union[Dict[Any, Any], CamelModel]] = None,
246
+ def apply_formatters_to_dict(
247
+ formatters: dict[Any, Any],
248
+ value: dict[Any, Any] | CamelModel | None = None,
250
249
  unaliased: bool = False,
251
- ) -> Dict[Any, Any]:
252
- ...
250
+ ) -> dict[Any, Any]: ...
253
251
 
254
252
 
255
253
  apply_formatter_at_index = curry(apply_formatter_at_index)
@@ -282,13 +280,12 @@ clamp = curry(clamp)
282
280
  # `from eth_utils.curried import *`
283
281
  del Any
284
282
  del Callable
285
- del Dict
286
283
  del Generator
287
284
  del Optional
288
285
  del Sequence
289
286
  del TReturn
290
287
  del TValue
291
- del Tuple
288
+ del TypeGuard
292
289
  del TypeVar
293
290
  del Union
294
291
  del curry
Binary file
@@ -1,69 +1,103 @@
1
1
  import functools
2
- import itertools
3
- import os
2
+ from collections.abc import Callable
4
3
  from typing import (
5
4
  Any,
6
- Callable,
7
- Dict,
8
- Optional,
9
- Type,
5
+ Concatenate,
6
+ Final,
7
+ Generic,
10
8
  TypeVar,
11
9
  final,
12
10
  )
13
11
 
14
- from .types import (
15
- is_text,
16
- )
12
+ from typing_extensions import ParamSpec
13
+
14
+ P = ParamSpec("P")
17
15
 
18
16
  T = TypeVar("T")
19
17
 
18
+ TInstance = TypeVar("TInstance", bound=object)
19
+ """A TypeVar representing an instance that a method can bind to."""
20
+
20
21
 
21
22
  @final
22
- class combomethod:
23
- def __init__(self, method: Callable[..., Any]) -> None:
24
- self.method = method
23
+ class combomethod(Generic[TInstance, P, T]):
24
+ def __init__(
25
+ self, method: Callable[Concatenate[TInstance | type[TInstance], P], T]
26
+ ) -> None:
27
+ self.method: Final = method
28
+
29
+ def __repr__(self) -> str:
30
+ return f"combomethod({self.method})"
25
31
 
26
32
  def __get__(
27
- self, obj: Optional[T] = None, objtype: Optional[Type[T]] = None
28
- ) -> Callable[..., Any]:
29
- @functools.wraps(self.method)
30
- def _wrapper(*args: Any, **kwargs: Any) -> Any:
31
- if obj is not None:
32
- return self.method(obj, *args, **kwargs)
33
- else:
34
- return self.method(objtype, *args, **kwargs)
33
+ self,
34
+ obj: TInstance | None,
35
+ objtype: type[TInstance],
36
+ ) -> Callable[P, T]:
37
+
38
+ method = self.method
39
+ bound_arg = objtype if obj is None else obj
40
+
41
+ @functools.wraps(method)
42
+ def _wrapper(*args: P.args, **kwargs: P.kwargs) -> T:
43
+ return method(bound_arg, *args, **kwargs)
35
44
 
36
45
  return _wrapper
37
46
 
38
47
 
39
- def return_arg_type(at_position: int) -> Callable[..., Callable[..., T]]:
48
+ _return_arg_type_deco_cache: Final[
49
+ dict[int, Callable[[Callable[P, T]], Callable[P, Any]]]
50
+ ] = {}
51
+ # No need to hold so many unique instances in memory
52
+
53
+
54
+ def return_arg_type(at_position: int) -> Callable[[Callable[P, T]], Callable[P, Any]]:
40
55
  """
41
56
  Wrap the return value with the result of `type(args[at_position])`.
42
57
  """
58
+ if deco := _return_arg_type_deco_cache.get(at_position):
59
+ return deco
43
60
 
44
- def decorator(to_wrap: Callable[..., Any]) -> Callable[..., T]:
61
+ def decorator(to_wrap: Callable[P, Any]) -> Callable[P, Any]:
45
62
  @functools.wraps(to_wrap)
46
- def wrapper(*args: Any, **kwargs: Any) -> T: # type: ignore
63
+ def wrapper(*args: P.args, **kwargs: P.kwargs) -> Any:
47
64
  result = to_wrap(*args, **kwargs)
48
65
  ReturnType = type(args[at_position])
49
- return ReturnType(result) # type: ignore
66
+ return ReturnType(result) # type: ignore [call-arg]
50
67
 
51
68
  return wrapper
52
69
 
70
+ _return_arg_type_deco_cache[at_position] = decorator
71
+
53
72
  return decorator
54
73
 
55
74
 
75
+ ExcType = type[BaseException]
76
+
77
+ ReplaceExceptionsCache = dict[
78
+ tuple[tuple[ExcType, ExcType], ...],
79
+ Callable[[Callable[P, T]], Callable[P, T]],
80
+ ]
81
+
82
+ _replace_exceptions_deco_cache: Final[ReplaceExceptionsCache[..., Any]] = {}
83
+ # No need to hold so many unique instances in memory
84
+
85
+
56
86
  def replace_exceptions(
57
- old_to_new_exceptions: Dict[Type[BaseException], Type[BaseException]]
58
- ) -> Callable[[Callable[..., T]], Callable[..., T]]:
87
+ old_to_new_exceptions: dict[ExcType, ExcType],
88
+ ) -> Callable[[Callable[P, T]], Callable[P, T]]:
59
89
  """
60
90
  Replaces old exceptions with new exceptions to be raised in their place.
61
91
  """
62
- old_exceptions = tuple(old_to_new_exceptions.keys())
92
+ cache_key = tuple(old_to_new_exceptions.items())
93
+ if deco := _replace_exceptions_deco_cache.get(cache_key):
94
+ return deco
95
+
96
+ old_exceptions = tuple(old_to_new_exceptions)
63
97
 
64
- def decorator(to_wrap: Callable[..., T]) -> Callable[..., T]:
98
+ def decorator(to_wrap: Callable[P, T]) -> Callable[P, T]:
65
99
  @functools.wraps(to_wrap)
66
- def wrapped(*args: Any, **kwargs: Any) -> T:
100
+ def wrapped(*args: P.args, **kwargs: P.kwargs) -> T:
67
101
  try:
68
102
  return to_wrap(*args, **kwargs)
69
103
  except old_exceptions as err:
@@ -76,4 +110,6 @@ def replace_exceptions(
76
110
 
77
111
  return wrapped
78
112
 
113
+ _replace_exceptions_deco_cache[cache_key] = decorator
114
+
79
115
  return decorator
@@ -2,5 +2,5 @@ def int_to_big_endian(value: int) -> bytes:
2
2
  return value.to_bytes((value.bit_length() + 7) // 8 or 1, "big")
3
3
 
4
4
 
5
- def big_endian_to_int(value: bytes) -> int:
5
+ def big_endian_to_int(value: bytes | bytearray) -> int:
6
6
  return int.from_bytes(value, "big")
@@ -1,4 +1,11 @@
1
- class ValidationError(Exception):
1
+ """
2
+ faster-eth-utils exceptions always inherit from eth-utils exceptions, so porting to faster-eth-utils
3
+ does not require any change to your existing exception handlers. They will continue to work.
4
+ """
5
+
6
+ import eth_utils.exceptions
7
+
8
+ class ValidationError(eth_utils.exceptions.ValidationError): # type: ignore[misc]
2
9
  """
3
10
  Raised when something does not pass a validation check.
4
11
  """
@@ -1,12 +1,10 @@
1
1
  import collections
2
2
  import functools
3
3
  import itertools
4
+ from collections.abc import Callable, Iterable, Mapping
4
5
  from typing import ( # noqa: F401
5
- Callable,
6
6
  Dict,
7
- Iterable,
8
7
  List,
9
- Mapping,
10
8
  Set,
11
9
  Tuple,
12
10
  TypeVar,
@@ -57,25 +55,25 @@ def apply_to_return_value(
57
55
  TVal = TypeVar("TVal")
58
56
  TKey = TypeVar("TKey")
59
57
 
60
- def to_tuple(fn: Callable[P, Iterable[TVal]]) -> Callable[P, Tuple[TVal, ...]]:
61
- def to_tuple_wrap(*args: P.args, **kwargs: P.kwargs) -> Tuple[TVal, ...]:
58
+ def to_tuple(fn: Callable[P, Iterable[TVal]]) -> Callable[P, tuple[TVal, ...]]:
59
+ def to_tuple_wrap(*args: P.args, **kwargs: P.kwargs) -> tuple[TVal, ...]:
62
60
  return tuple(fn(*args, **kwargs))
63
61
  return to_tuple_wrap
64
62
 
65
- def to_list(fn: Callable[P, Iterable[TVal]]) -> Callable[P, List[TVal]]:
66
- def to_list_wrap(*args: P.args, **kwargs: P.kwargs) -> List[TVal]:
63
+ def to_list(fn: Callable[P, Iterable[TVal]]) -> Callable[P, list[TVal]]:
64
+ def to_list_wrap(*args: P.args, **kwargs: P.kwargs) -> list[TVal]:
67
65
  return list(fn(*args, **kwargs))
68
66
  return to_list_wrap
69
67
 
70
- def to_set(fn: Callable[P, Iterable[TVal]]) -> Callable[P, Set[TVal]]:
71
- def to_set_wrap(*args: P.args, **kwargs: P.kwargs) -> Set[TVal]:
68
+ def to_set(fn: Callable[P, Iterable[TVal]]) -> Callable[P, set[TVal]]:
69
+ def to_set_wrap(*args: P.args, **kwargs: P.kwargs) -> set[TVal]:
72
70
  return set(fn(*args, **kwargs))
73
71
  return to_set_wrap
74
72
 
75
73
  def to_dict(
76
- fn: Callable[P, Union[Mapping[TKey, TVal], Iterable[Tuple[TKey, TVal]]]]
77
- ) -> Callable[P, Dict[TKey, TVal]]:
78
- def to_dict_wrap(*args: P.args, **kwargs: P.kwargs) -> Dict[TKey, TVal]:
74
+ fn: Callable[P, Mapping[TKey, TVal] | Iterable[tuple[TKey, TVal]]]
75
+ ) -> Callable[P, dict[TKey, TVal]]:
76
+ def to_dict_wrap(*args: P.args, **kwargs: P.kwargs) -> dict[TKey, TVal]:
79
77
  return dict(fn(*args, **kwargs))
80
78
  return to_dict_wrap
81
79