55)
66import functools
77
8+ from typing import (
9+ Any ,
10+ AnyStr ,
11+ Callable ,
12+ Dict ,
13+ List ,
14+ Sequence ,
15+ Tuple ,
16+ )
17+
818from cytoolz import (
919 assoc_in ,
1020 compose ,
1525)
1626import cytoolz .curried
1727
28+ from eth_typing import (
29+ Address ,
30+ )
31+
1832from eth_utils import (
1933 apply_formatters_to_dict ,
2034 big_endian_to_int ,
4155 is_cleanly_mergable ,
4256)
4357
58+ from eth .typing import (
59+ AccountState ,
60+ GeneralState ,
61+ )
62+
4463
4564#
4665# Primitives
4766#
4867@functools .lru_cache (maxsize = 1024 )
49- def normalize_int (value ) :
68+ def normalize_int (value : Any ) -> int :
5069 """
5170 Robust to integer conversion, handling hex values, string representations,
5271 and special cases like `0x`.
@@ -66,7 +85,7 @@ def normalize_int(value):
6685 raise TypeError ("Unsupported type: Got `{0}`" .format (type (value )))
6786
6887
69- def normalize_bytes (value ) :
88+ def normalize_bytes (value : Any ) -> bytes :
7089 if is_bytes (value ):
7190 return value
7291 elif is_text (value ) and is_hex (value ):
@@ -78,7 +97,7 @@ def normalize_bytes(value):
7897
7998
8099@functools .lru_cache (maxsize = 1024 )
81- def to_int (value ) :
100+ def to_int (value : Any ) -> int :
82101 """
83102 Robust to integer conversion, handling hex values, string representations,
84103 and special cases like `0x`.
@@ -93,7 +112,7 @@ def to_int(value):
93112
94113
95114@functools .lru_cache (maxsize = 128 )
96- def normalize_to_address (value ) :
115+ def normalize_to_address (value : AnyStr ) -> Address :
97116 if value :
98117 return to_canonical_address (value )
99118 else :
@@ -106,21 +125,28 @@ def normalize_to_address(value):
106125#
107126# Containers
108127#
109- def dict_normalizer (formatters , required = None , optional = None ):
128+
129+ NormalizerType = Callable [[Dict [Any , Any ]], Iterable [Tuple [Any , Any ]]]
130+
131+
132+ def dict_normalizer (formatters : Dict [Any , Any ],
133+ required : Iterable [Any ]= None ,
134+ optional : Iterable [Any ]= None ) -> NormalizerType :
135+
110136 all_keys = set (formatters .keys ())
111137
112138 if required is None and optional is None :
113- required = all_keys
139+ required_set_form = all_keys
114140 elif required is not None :
115- required = set (required )
141+ required_set_form = set (required )
116142 elif optional is not None :
117- required = all_keys - set (optional )
143+ required_set_form = all_keys - set (optional )
118144 else :
119145 raise ValueError ("Both required and optional keys specified" )
120146
121- def normalizer (d ) :
147+ def normalizer (d : Dict [ Any , Any ]) -> Iterable [ Tuple [ Any , Any ]] :
122148 keys = set (d .keys ())
123- missing_keys = required - keys
149+ missing_keys = required_set_form - keys
124150 superfluous_keys = keys - all_keys
125151 if missing_keys :
126152 raise KeyError ("Missing required keys: {}" .format (", " .join (missing_keys )))
@@ -132,9 +158,9 @@ def normalizer(d):
132158 return normalizer
133159
134160
135- def dict_options_normalizer (normalizers ) :
161+ def dict_options_normalizer (normalizers : Iterable [ Callable [..., Any ]]) -> Callable [..., Any ] :
136162
137- def normalize (d ) :
163+ def normalize (d : Dict [ Any , Any ]) -> Callable [..., Any ] :
138164 first_exception = None
139165 for normalizer in normalizers :
140166 try :
@@ -153,7 +179,7 @@ def normalize(d):
153179#
154180# Composition
155181#
156- def state_definition_to_dict (state_definition ) :
182+ def state_definition_to_dict (state_definition : GeneralState ) -> AccountState :
157183 """Convert a state definition to the canonical dict form.
158184
159185 State can either be defined in the canonical form, or as a list of sub states that are then
@@ -305,7 +331,9 @@ def state_definition_to_dict(state_definition):
305331#
306332# Fixture Normalizers
307333#
308- def normalize_unsigned_transaction (transaction , indexes ):
334+ def normalize_unsigned_transaction (transaction : Dict [str , Any ],
335+ indexes : Dict [str , Any ]) -> Dict [str , Any ]:
336+
309337 normalized = normalize_transaction_group (transaction )
310338 return merge (normalized , {
311339 transaction_key : normalized [transaction_key ][indexes [index_key ]]
@@ -318,7 +346,7 @@ def normalize_unsigned_transaction(transaction, indexes):
318346 })
319347
320348
321- def normalize_account_state (account_state ) :
349+ def normalize_account_state (account_state : AccountState ) -> AccountState :
322350 return {
323351 to_canonical_address (address ): {
324352 'balance' : to_int (state ['balance' ]),
@@ -333,14 +361,17 @@ def normalize_account_state(account_state):
333361
334362
335363@to_dict
336- def normalize_post_state (post_state ) :
364+ def normalize_post_state (post_state : Dict [ str , Any ]) -> Iterable [ Tuple [ str , bytes ]] :
337365 yield 'hash' , decode_hex (post_state ['hash' ])
338366 if 'logs' in post_state :
339367 yield 'logs' , decode_hex (post_state ['logs' ])
340368
341369
342370@curry
343- def normalize_statetest_fixture (fixture , fork , post_state_index ):
371+ def normalize_statetest_fixture (fixture : Dict [str , Any ],
372+ fork : str ,
373+ post_state_index : int ) -> Dict [str , Any ]:
374+
344375 post_state = fixture ['post' ][fork ][post_state_index ]
345376
346377 normalized_fixture = {
@@ -356,7 +387,7 @@ def normalize_statetest_fixture(fixture, fork, post_state_index):
356387 return normalized_fixture
357388
358389
359- def normalize_exec (exec_params ) :
390+ def normalize_exec (exec_params : Dict [ str , Any ]) -> Dict [ str , Any ] :
360391 return {
361392 'origin' : to_canonical_address (exec_params ['origin' ]),
362393 'address' : to_canonical_address (exec_params ['address' ]),
@@ -368,7 +399,7 @@ def normalize_exec(exec_params):
368399 }
369400
370401
371- def normalize_callcreates (callcreates ) :
402+ def normalize_callcreates (callcreates : Sequence [ Dict [ str , Any ]]) -> List [ Dict [ str , Any ]] :
372403 return [
373404 {
374405 'data' : decode_hex (created_call ['data' ]),
@@ -384,7 +415,7 @@ def normalize_callcreates(callcreates):
384415
385416
386417@to_dict
387- def normalize_vmtest_fixture (fixture ) :
418+ def normalize_vmtest_fixture (fixture : Dict [ str , Any ]) -> Iterable [ Tuple [ str , Any ]] :
388419 yield 'env' , normalize_environment (fixture ['env' ])
389420 yield 'exec' , normalize_exec (fixture ['exec' ])
390421 yield 'pre' , normalize_account_state (fixture ['pre' ])
@@ -405,7 +436,7 @@ def normalize_vmtest_fixture(fixture):
405436 yield 'logs' , decode_hex (fixture ['logs' ])
406437
407438
408- def normalize_signed_transaction (transaction ) :
439+ def normalize_signed_transaction (transaction : Dict [ str , Any ]) -> Dict [ str , Any ] :
409440 return {
410441 'data' : robust_decode_hex (transaction ['data' ]),
411442 'gasLimit' : to_int (transaction ['gasLimit' ]),
@@ -420,7 +451,7 @@ def normalize_signed_transaction(transaction):
420451
421452
422453@curry
423- def normalize_transactiontest_fixture (fixture , fork ) :
454+ def normalize_transactiontest_fixture (fixture : Dict [ str , Any ], fork : str ) -> Dict [ str , Any ] :
424455
425456 normalized_fixture = {}
426457
@@ -440,7 +471,7 @@ def normalize_transactiontest_fixture(fixture, fork):
440471 return normalized_fixture
441472
442473
443- def normalize_block_header (header ) :
474+ def normalize_block_header (header : Dict [ str , Any ]) -> Dict [ str , Any ] :
444475 normalized_header = {
445476 'bloom' : big_endian_to_int (decode_hex (header ['bloom' ])),
446477 'coinbase' : to_canonical_address (header ['coinbase' ]),
@@ -468,7 +499,7 @@ def normalize_block_header(header):
468499 return normalized_header
469500
470501
471- def normalize_block (block ) :
502+ def normalize_block (block : Dict [ str , Any ]) -> Dict [ str , Any ] :
472503 normalized_block = {}
473504
474505 try :
@@ -487,7 +518,7 @@ def normalize_block(block):
487518 return normalized_block
488519
489520
490- def normalize_blockchain_fixtures (fixture ) :
521+ def normalize_blockchain_fixtures (fixture : Dict [ str , Any ]) -> Dict [ str , Any ] :
491522 normalized_fixture = {
492523 'blocks' : [normalize_block (block_fixture ) for block_fixture in fixture ['blocks' ]],
493524 'genesisBlockHeader' : normalize_block_header (fixture ['genesisBlockHeader' ]),
0 commit comments