Skip to content

Commit bf3789c

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 69b1c58 commit bf3789c

Some content is hidden

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

91 files changed

+733
-393
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
@@ -59,7 +59,7 @@ class Block:
5959
"""
6060

6161
header: Header
62-
transactions: Tuple[Union[Bytes, LegacyTransaction], ...]
62+
transactions: Tuple[Bytes | LegacyTransaction, ...]
6363
ommers: Tuple[Header, ...]
6464

6565

@@ -88,7 +88,7 @@ class Receipt:
8888
logs: Tuple[Log, ...]
8989

9090

91-
def encode_receipt(tx: Transaction, receipt: Receipt) -> Union[Bytes, Receipt]:
91+
def encode_receipt(tx: Transaction, receipt: Receipt) -> Bytes | Receipt:
9292
"""
9393
Encodes a receipt.
9494
"""
@@ -100,7 +100,7 @@ def encode_receipt(tx: Transaction, receipt: Receipt) -> Union[Bytes, Receipt]:
100100
return receipt
101101

102102

103-
def decode_receipt(receipt: Union[Bytes, Receipt]) -> Receipt:
103+
def decode_receipt(receipt: Bytes | Receipt) -> Receipt:
104104
"""
105105
Decodes a receipt.
106106
"""

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
@@ -482,7 +482,7 @@ def make_receipt(
482482
error: Optional[EthereumException],
483483
cumulative_gas_used: Uint,
484484
logs: Tuple[Log, ...],
485-
) -> Union[Bytes, Receipt]:
485+
) -> Bytes | Receipt:
486486
"""
487487
Make the receipt for a transaction that was executed.
488488
@@ -515,7 +515,7 @@ def make_receipt(
515515

516516
def apply_body(
517517
block_env: vm.BlockEnvironment,
518-
transactions: Tuple[Union[LegacyTransaction, Bytes], ...],
518+
transactions: Tuple[LegacyTransaction | Bytes, ...],
519519
ommers: Tuple[Header, ...],
520520
) -> vm.BlockOutput:
521521
"""

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
@@ -36,7 +36,7 @@ class LegacyTransaction:
3636
nonce: U256
3737
gas_price: Uint
3838
gas: Uint
39-
to: Union[Bytes0, Address]
39+
to: Bytes0 | Address
4040
value: U256
4141
data: Bytes
4242
v: U256
@@ -67,7 +67,7 @@ class AccessListTransaction:
6767
nonce: U256
6868
gas_price: Uint
6969
gas: Uint
70-
to: Union[Bytes0, Address]
70+
to: Bytes0 | Address
7171
value: U256
7272
data: Bytes
7373
access_list: Tuple[Access, ...]
@@ -88,7 +88,7 @@ class FeeMarketTransaction:
8888
max_priority_fee_per_gas: Uint
8989
max_fee_per_gas: Uint
9090
gas: Uint
91-
to: Union[Bytes0, Address]
91+
to: Bytes0 | Address
9292
value: U256
9393
data: Bytes
9494
access_list: Tuple[Access, ...]
@@ -97,12 +97,10 @@ class FeeMarketTransaction:
9797
s: U256
9898

9999

100-
Transaction = Union[
101-
LegacyTransaction, AccessListTransaction, FeeMarketTransaction
102-
]
100+
Transaction = LegacyTransaction | AccessListTransaction | FeeMarketTransaction
103101

104102

105-
def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
103+
def encode_transaction(tx: Transaction) -> LegacyTransaction | Bytes:
106104
"""
107105
Encode a transaction. Needed because non-legacy transactions aren't RLP.
108106
"""
@@ -116,7 +114,7 @@ def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
116114
raise Exception(f"Unable to encode transaction of type {type(tx)}")
117115

118116

119-
def decode_transaction(tx: Union[LegacyTransaction, Bytes]) -> Transaction:
117+
def decode_transaction(tx: LegacyTransaction | Bytes) -> Transaction:
120118
"""
121119
Decode a transaction. Needed because non-legacy transactions aren't RLP.
122120
"""
@@ -400,7 +398,7 @@ def signing_hash_1559(tx: FeeMarketTransaction) -> Hash32:
400398
)
401399

402400

403-
def get_transaction_hash(tx: Union[Bytes, LegacyTransaction]) -> Hash32:
401+
def get_transaction_hash(tx: Bytes | LegacyTransaction) -> Hash32:
404402
"""
405403
Parameters
406404
----------

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
@@ -53,7 +53,7 @@ class Block:
5353
"""
5454

5555
header: Header
56-
transactions: Tuple[Union[Bytes, LegacyTransaction], ...]
56+
transactions: Tuple[Bytes | LegacyTransaction, ...]
5757
ommers: Tuple[Header, ...]
5858

5959

@@ -82,7 +82,7 @@ class Receipt:
8282
logs: Tuple[Log, ...]
8383

8484

85-
def encode_receipt(tx: Transaction, receipt: Receipt) -> Union[Bytes, Receipt]:
85+
def encode_receipt(tx: Transaction, receipt: Receipt) -> Bytes | Receipt:
8686
"""
8787
Encodes a receipt.
8888
"""
@@ -92,7 +92,7 @@ def encode_receipt(tx: Transaction, receipt: Receipt) -> Union[Bytes, Receipt]:
9292
return receipt
9393

9494

95-
def decode_receipt(receipt: Union[Bytes, Receipt]) -> Receipt:
95+
def decode_receipt(receipt: Bytes | Receipt) -> Receipt:
9696
"""
9797
Decodes a receipt.
9898
"""

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
@@ -389,7 +389,7 @@ def make_receipt(
389389
error: Optional[EthereumException],
390390
cumulative_gas_used: Uint,
391391
logs: Tuple[Log, ...],
392-
) -> Union[Bytes, Receipt]:
392+
) -> Bytes | Receipt:
393393
"""
394394
Make the receipt for a transaction that was executed.
395395
@@ -422,7 +422,7 @@ def make_receipt(
422422

423423
def apply_body(
424424
block_env: vm.BlockEnvironment,
425-
transactions: Tuple[Union[LegacyTransaction, Bytes], ...],
425+
transactions: Tuple[LegacyTransaction | Bytes, ...],
426426
ommers: Tuple[Header, ...],
427427
) -> vm.BlockOutput:
428428
"""

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
@@ -36,7 +36,7 @@ class LegacyTransaction:
3636
nonce: U256
3737
gas_price: Uint
3838
gas: Uint
39-
to: Union[Bytes0, Address]
39+
to: Bytes0 | Address
4040
value: U256
4141
data: Bytes
4242
v: U256
@@ -67,7 +67,7 @@ class AccessListTransaction:
6767
nonce: U256
6868
gas_price: Uint
6969
gas: Uint
70-
to: Union[Bytes0, Address]
70+
to: Bytes0 | Address
7171
value: U256
7272
data: Bytes
7373
access_list: Tuple[Access, ...]
@@ -76,10 +76,10 @@ class AccessListTransaction:
7676
s: U256
7777

7878

79-
Transaction = Union[LegacyTransaction, AccessListTransaction]
79+
Transaction = LegacyTransaction | AccessListTransaction
8080

8181

82-
def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
82+
def encode_transaction(tx: Transaction) -> LegacyTransaction | Bytes:
8383
"""
8484
Encode a transaction. Needed because non-legacy transactions aren't RLP.
8585
"""
@@ -91,7 +91,7 @@ def encode_transaction(tx: Transaction) -> Union[LegacyTransaction, Bytes]:
9191
raise Exception(f"Unable to encode transaction of type {type(tx)}")
9292

9393

94-
def decode_transaction(tx: Union[LegacyTransaction, Bytes]) -> Transaction:
94+
def decode_transaction(tx: LegacyTransaction | Bytes) -> Transaction:
9595
"""
9696
Decode a transaction. Needed because non-legacy transactions aren't RLP.
9797
"""
@@ -334,7 +334,7 @@ def signing_hash_2930(tx: AccessListTransaction) -> Hash32:
334334
)
335335

336336

337-
def get_transaction_hash(tx: Union[Bytes, LegacyTransaction]) -> Hash32:
337+
def get_transaction_hash(tx: Bytes | LegacyTransaction) -> Hash32:
338338
"""
339339
Parameters
340340
----------

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)