Skip to content

Commit 9139169

Browse files
committed
Python 3.11 refactors
- refactored assert_type import from typing_extensions to typing - refactored Union to use primitive type offered by 3.10
1 parent 0fa85f7 commit 9139169

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+325
-378
lines changed

src/ethereum/arrow_glacier/blocks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
chain.
1010
"""
1111
from dataclasses import dataclass
12-
from typing import Tuple, Union
12+
from typing import Tuple
1313

1414
from ethereum_rlp import rlp
1515
from ethereum_types.bytes import Bytes, Bytes8, Bytes32
@@ -210,7 +210,7 @@ class Block:
210210
[headers]: ref:ethereum.arrow_glacier.blocks.Header
211211
"""
212212

213-
transactions: Tuple[Union[Bytes, LegacyTransaction], ...]
213+
transactions: Tuple[Bytes | LegacyTransaction, ...]
214214
"""
215215
A tuple of transactions included in this block. Each transaction can be
216216
any of a legacy transaction, an access list transaction, or a fee market
@@ -289,7 +289,7 @@ class Receipt:
289289
"""
290290

291291

292-
def encode_receipt(tx: Transaction, receipt: Receipt) -> Union[Bytes, Receipt]:
292+
def encode_receipt(tx: Transaction, receipt: Receipt) -> Bytes | Receipt:
293293
r"""
294294
Encodes a transaction receipt based on the transaction type.
295295
@@ -306,7 +306,7 @@ def encode_receipt(tx: Transaction, receipt: Receipt) -> Union[Bytes, Receipt]:
306306
return receipt
307307

308308

309-
def decode_receipt(receipt: Union[Bytes, Receipt]) -> Receipt:
309+
def decode_receipt(receipt: Bytes | Receipt) -> Receipt:
310310
r"""
311311
Decodes a receipt from its serialized form.
312312

src/ethereum/arrow_glacier/fork.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"""
1414

1515
from dataclasses import dataclass
16-
from typing import List, Optional, Set, Tuple, Union
16+
from typing import List, Optional, Set, Tuple
1717

1818
from ethereum_rlp import rlp
1919
from ethereum_types.bytes import Bytes
@@ -507,7 +507,7 @@ def make_receipt(
507507
error: Optional[EthereumException],
508508
cumulative_gas_used: Uint,
509509
logs: Tuple[Log, ...],
510-
) -> Union[Bytes, Receipt]:
510+
) -> Bytes | Receipt:
511511
"""
512512
Make the receipt for a transaction that was executed.
513513
@@ -540,7 +540,7 @@ def make_receipt(
540540

541541
def apply_body(
542542
block_env: vm.BlockEnvironment,
543-
transactions: Tuple[Union[LegacyTransaction, Bytes], ...],
543+
transactions: Tuple[LegacyTransaction | Bytes, ...],
544544
ommers: Tuple[Header, ...],
545545
) -> vm.BlockOutput:
546546
"""

src/ethereum/arrow_glacier/transactions.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
transactions are the events that move between states.
55
"""
66
from dataclasses import dataclass
7-
from typing import Tuple, Union
7+
from typing import Tuple
88

99
from ethereum_rlp import rlp
1010
from ethereum_types.bytes import Bytes, Bytes0, Bytes32
@@ -76,7 +76,7 @@ class LegacyTransaction:
7676
The maximum amount of gas that can be used by this transaction.
7777
"""
7878

79-
to: Union[Bytes0, Address]
79+
to: Bytes0 | Address
8080
"""
8181
The address of the recipient. If empty, the transaction is a contract
8282
creation.
@@ -161,7 +161,7 @@ class AccessListTransaction:
161161
The maximum amount of gas that can be used by this transaction.
162162
"""
163163

164-
to: Union[Bytes0, Address]
164+
to: Bytes0 | Address
165165
"""
166166
The address of the recipient. If empty, the transaction is a contract
167167
creation.
@@ -238,7 +238,7 @@ class FeeMarketTransaction:
238238
The maximum amount of gas that can be used by this transaction.
239239
"""
240240

241-
to: Union[Bytes0, Address]
241+
to: Bytes0 | Address
242242
"""
243243
The address of the recipient. If empty, the transaction is a contract
244244
creation.
@@ -277,15 +277,13 @@ class FeeMarketTransaction:
277277
"""
278278

279279

280-
Transaction = Union[
281-
LegacyTransaction, AccessListTransaction, FeeMarketTransaction
282-
]
280+
Transaction = LegacyTransaction | AccessListTransaction | FeeMarketTransaction
283281
"""
284282
Union type representing any valid transaction type.
285283
"""
286284

287285

288-
def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
286+
def encode_transaction(tx: Transaction) -> LegacyTransaction | Bytes:
289287
"""
290288
Encode a transaction into its RLP or typed transaction format.
291289
Needed because non-legacy transactions aren't RLP.
@@ -303,7 +301,7 @@ def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
303301
raise Exception(f"Unable to encode transaction of type {type(tx)}")
304302

305303

306-
def decode_transaction(tx: Union[LegacyTransaction, Bytes]) -> Transaction:
304+
def decode_transaction(tx: LegacyTransaction | Bytes) -> Transaction:
307305
"""
308306
Decode a transaction from its RLP or typed transaction format.
309307
Needed because non-legacy transactions aren't RLP.
@@ -552,7 +550,7 @@ def signing_hash_1559(tx: FeeMarketTransaction) -> Hash32:
552550
)
553551

554552

555-
def get_transaction_hash(tx: Union[Bytes, LegacyTransaction]) -> Hash32:
553+
def get_transaction_hash(tx: Bytes | LegacyTransaction) -> Hash32:
556554
"""
557555
Compute the hash of a transaction.
558556

src/ethereum/arrow_glacier/trie.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@
2626
Sequence,
2727
Tuple,
2828
TypeVar,
29-
Union,
29+
assert_type,
3030
cast,
3131
)
3232

3333
from ethereum_rlp import Extended, rlp
3434
from ethereum_types.bytes import Bytes
3535
from ethereum_types.frozen import slotted_freezable
3636
from ethereum_types.numeric import U256, Uint
37-
from typing_extensions import assert_type
3837

3938
from ethereum.crypto.hash import keccak256
4039
from ethereum.london import trie as previous_trie
@@ -63,15 +62,15 @@
6362
)
6463
)
6564

66-
Node = Union[Account, Bytes, LegacyTransaction, Receipt, Uint, U256, None]
65+
Node = Account | Bytes | LegacyTransaction | Receipt | Uint | U256 | None
6766
K = TypeVar("K", bound=Bytes)
6867
V = TypeVar(
6968
"V",
7069
Optional[Account],
7170
Optional[Bytes],
7271
Bytes,
73-
Optional[Union[LegacyTransaction, Bytes]],
74-
Optional[Union[Receipt, Bytes]],
72+
Optional[LegacyTransaction | Bytes],
73+
Optional[Receipt | Bytes],
7574
Uint,
7675
U256,
7776
)
@@ -124,7 +123,7 @@ class BranchNode:
124123
value: Extended
125124

126125

127-
InternalNode = Union[LeafNode, ExtensionNode, BranchNode]
126+
InternalNode = LeafNode | ExtensionNode | BranchNode
128127

129128

130129
def encode_internal_node(node: Optional[InternalNode]) -> Extended:

src/ethereum/arrow_glacier/utils/address.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
Address specific functions used in this arrow_glacier version of
1313
specification.
1414
"""
15-
from typing import Union
16-
1715
from ethereum_rlp import rlp
1816
from ethereum_types.bytes import Bytes32
1917
from ethereum_types.numeric import U256, Uint
@@ -24,7 +22,7 @@
2422
from ..fork_types import Address
2523

2624

27-
def to_address(data: Union[Uint, U256]) -> Address:
25+
def to_address(data: Uint | U256) -> Address:
2826
"""
2927
Convert a Uint or U256 value to a valid address (20 bytes).
3028

src/ethereum/arrow_glacier/vm/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""
1515

1616
from dataclasses import dataclass, field
17-
from typing import List, Optional, Set, Tuple, Union
17+
from typing import List, Optional, Set, Tuple
1818

1919
from ethereum_types.bytes import Bytes, Bytes0, Bytes32
2020
from ethereum_types.numeric import U64, U256, Uint
@@ -71,9 +71,9 @@ class BlockOutput:
7171

7272
block_gas_used: Uint = Uint(0)
7373
transactions_trie: Trie[
74-
Bytes, Optional[Union[Bytes, LegacyTransaction]]
74+
Bytes, Optional[Bytes | LegacyTransaction]
7575
] = field(default_factory=lambda: Trie(secured=False, default=None))
76-
receipts_trie: Trie[Bytes, Optional[Union[Bytes, Receipt]]] = field(
76+
receipts_trie: Trie[Bytes, Optional[Bytes | Receipt]] = field(
7777
default_factory=lambda: Trie(secured=False, default=None)
7878
)
7979
receipt_keys: Tuple[Bytes, ...] = field(default_factory=tuple)
@@ -105,7 +105,7 @@ class Message:
105105
block_env: BlockEnvironment
106106
tx_env: TransactionEnvironment
107107
caller: Address
108-
target: Union[Bytes0, Address]
108+
target: Bytes0 | Address
109109
current_target: Address
110110
gas: Uint
111111
value: U256

src/ethereum/berlin/blocks.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
chain.
1010
"""
1111
from dataclasses import dataclass
12-
from typing import Tuple, Union
12+
from typing import Tuple
1313

1414
from ethereum_rlp import rlp
1515
from ethereum_types.bytes import Bytes, Bytes8, Bytes32
@@ -187,7 +187,7 @@ class Block:
187187
[headers]: ref:ethereum.berlin.blocks.Header
188188
"""
189189

190-
transactions: Tuple[Union[Bytes, LegacyTransaction], ...]
190+
transactions: Tuple[Bytes | LegacyTransaction, ...]
191191
"""
192192
A tuple of transactions included in this block, which can be either legacy
193193
transactions, or access list transactions.
@@ -265,7 +265,7 @@ class Receipt:
265265
"""
266266

267267

268-
def encode_receipt(tx: Transaction, receipt: Receipt) -> Union[Bytes, Receipt]:
268+
def encode_receipt(tx: Transaction, receipt: Receipt) -> Bytes | Receipt:
269269
r"""
270270
Encodes a transaction receipt based on the transaction type.
271271
@@ -279,7 +279,7 @@ def encode_receipt(tx: Transaction, receipt: Receipt) -> Union[Bytes, Receipt]:
279279
return receipt
280280

281281

282-
def decode_receipt(receipt: Union[Bytes, Receipt]) -> Receipt:
282+
def decode_receipt(receipt: Bytes | Receipt) -> Receipt:
283283
r"""
284284
Decodes a receipt from its serialized form.
285285

src/ethereum/berlin/fork.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
Entry point for the Ethereum specification.
1313
"""
1414
from dataclasses import dataclass
15-
from typing import List, Optional, Set, Tuple, Union
15+
from typing import List, Optional, Set, Tuple
1616

1717
from ethereum_rlp import rlp
1818
from ethereum_types.bytes import Bytes
@@ -400,7 +400,7 @@ def make_receipt(
400400
error: Optional[EthereumException],
401401
cumulative_gas_used: Uint,
402402
logs: Tuple[Log, ...],
403-
) -> Union[Bytes, Receipt]:
403+
) -> Bytes | Receipt:
404404
"""
405405
Make the receipt for a transaction that was executed.
406406
@@ -433,7 +433,7 @@ def make_receipt(
433433

434434
def apply_body(
435435
block_env: vm.BlockEnvironment,
436-
transactions: Tuple[Union[LegacyTransaction, Bytes], ...],
436+
transactions: Tuple[LegacyTransaction | Bytes, ...],
437437
ommers: Tuple[Header, ...],
438438
) -> vm.BlockOutput:
439439
"""

src/ethereum/berlin/transactions.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
transactions are the events that move between states.
55
"""
66
from dataclasses import dataclass
7-
from typing import Tuple, Union
7+
from typing import Tuple
88

99
from ethereum_rlp import rlp
1010
from ethereum_types.bytes import Bytes, Bytes0, Bytes32
@@ -75,7 +75,7 @@ class LegacyTransaction:
7575
The maximum amount of gas that can be used by this transaction.
7676
"""
7777

78-
to: Union[Bytes0, Address]
78+
to: Bytes0 | Address
7979
"""
8080
The address of the recipient. If empty, the transaction is a contract
8181
creation.
@@ -160,7 +160,7 @@ class AccessListTransaction:
160160
The maximum amount of gas that can be used by this transaction.
161161
"""
162162

163-
to: Union[Bytes0, Address]
163+
to: Bytes0 | Address
164164
"""
165165
The address of the recipient. If empty, the transaction is a contract
166166
creation.
@@ -199,13 +199,13 @@ class AccessListTransaction:
199199
"""
200200

201201

202-
Transaction = Union[LegacyTransaction, AccessListTransaction]
202+
Transaction = LegacyTransaction | AccessListTransaction
203203
"""
204204
Union type representing any valid transaction type.
205205
"""
206206

207207

208-
def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
208+
def encode_transaction(tx: Transaction) -> LegacyTransaction | Bytes:
209209
"""
210210
Encode a transaction into its RLP or typed transaction format.
211211
Needed because non-legacy transactions aren't RLP.
@@ -221,7 +221,7 @@ def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
221221
raise Exception(f"Unable to encode transaction of type {type(tx)}")
222222

223223

224-
def decode_transaction(tx: Union[LegacyTransaction, Bytes]) -> Transaction:
224+
def decode_transaction(tx: LegacyTransaction | Bytes) -> Transaction:
225225
"""
226226
Decode a transaction from its RLP or typed transaction format.
227227
Needed because non-legacy transactions aren't RLP.
@@ -434,7 +434,7 @@ def signing_hash_2930(tx: AccessListTransaction) -> Hash32:
434434
)
435435

436436

437-
def get_transaction_hash(tx: Union[Bytes, LegacyTransaction]) -> Hash32:
437+
def get_transaction_hash(tx: Bytes | LegacyTransaction) -> Hash32:
438438
"""
439439
Compute the hash of a transaction.
440440

src/ethereum/berlin/trie.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@
2626
Sequence,
2727
Tuple,
2828
TypeVar,
29-
Union,
29+
assert_type,
3030
cast,
3131
)
3232

3333
from ethereum_rlp import Extended, rlp
3434
from ethereum_types.bytes import Bytes
3535
from ethereum_types.frozen import slotted_freezable
3636
from ethereum_types.numeric import U256, Uint
37-
from typing_extensions import assert_type
3837

3938
from ethereum.crypto.hash import keccak256
4039
from ethereum.muir_glacier import trie as previous_trie
@@ -63,15 +62,15 @@
6362
)
6463
)
6564

66-
Node = Union[Account, Bytes, LegacyTransaction, Receipt, Uint, U256, None]
65+
Node = Account | Bytes | LegacyTransaction | Receipt | Uint | U256 | None
6766
K = TypeVar("K", bound=Bytes)
6867
V = TypeVar(
6968
"V",
7069
Optional[Account],
7170
Optional[Bytes],
7271
Bytes,
73-
Optional[Union[LegacyTransaction, Bytes]],
74-
Optional[Union[Receipt, Bytes]],
72+
Optional[LegacyTransaction | Bytes],
73+
Optional[Receipt | Bytes],
7574
Uint,
7675
U256,
7776
)
@@ -124,7 +123,7 @@ class BranchNode:
124123
value: Extended
125124

126125

127-
InternalNode = Union[LeafNode, ExtensionNode, BranchNode]
126+
InternalNode = LeafNode | ExtensionNode | BranchNode
128127

129128

130129
def encode_internal_node(node: Optional[InternalNode]) -> Extended:

0 commit comments

Comments
 (0)