Skip to content

Commit c9893c2

Browse files
committed
Move and reuse StaticMethod helper tighten type support
1 parent d05e3fa commit c9893c2

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

eth/chains/base.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
Type,
2121
TYPE_CHECKING,
2222
Union,
23+
TypeVar,
24+
Generic,
2325
)
2426

2527
import logging
@@ -72,8 +74,9 @@
7274
BaseUnsignedTransaction,
7375
)
7476

75-
from eth.typing import (
77+
from eth.typing import ( # noqa: F401
7678
AccountState,
79+
StaticMethod,
7780
)
7881

7982
from eth.utils.spoof import (
@@ -99,7 +102,7 @@
99102
validate_vm_configuration,
100103
)
101104
from eth.vm.computation import BaseComputation
102-
from eth.vm.state import BaseState # noqa: F401
105+
from eth.vm.state import BaseState # noqa: F401
103106

104107
from eth._warnings import catch_and_ignore_import_warning
105108
with catch_and_ignore_import_warning():
@@ -335,7 +338,7 @@ class Chain(BaseChain):
335338
current block number.
336339
"""
337340
logger = logging.getLogger("eth.chain.chain.Chain")
338-
gas_estimator = None # type: Callable[[BaseState, BaseTransaction], int]
341+
gas_estimator = None # type: StaticMethod[Callable[[BaseState, Union[BaseTransaction, SpoofTransaction]], int]] # noqa: E501
339342

340343
chaindb_class = ChainDB # type: Type[BaseChainDB]
341344

@@ -349,8 +352,8 @@ def __init__(self, base_db: BaseAtomicDB) -> None:
349352

350353
self.chaindb = self.get_chaindb_class()(base_db)
351354
self.headerdb = HeaderDB(base_db)
352-
if self.gas_estimator is None: # type: ignore
353-
self.gas_estimator = get_gas_estimator() # type: ignore
355+
if self.gas_estimator is None:
356+
self.gas_estimator = get_gas_estimator()
354357

355358
#
356359
# Helpers
@@ -648,7 +651,7 @@ def estimate_gas(
648651
if at_header is None:
649652
at_header = self.get_canonical_head()
650653
with self.get_vm(at_header).state_in_temp_block() as state:
651-
return self.gas_estimator(state, transaction) # type: ignore
654+
return self.gas_estimator(state, transaction)
652655

653656
def import_block(self,
654657
block: BaseBlock,

eth/estimators/__init__.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11
import os
22
from typing import (
33
Callable,
4-
cast
4+
cast,
5+
Union,
56
)
67

7-
8-
from eth_utils import import_string
9-
10-
from eth.rlp.transactions import BaseTransaction
11-
from eth.vm.state import BaseState
8+
from eth.rlp.transactions import (
9+
BaseTransaction,
10+
)
11+
from eth.utils.module_loading import (
12+
import_string,
13+
)
14+
from eth.utils.spoof import (
15+
SpoofTransaction,
16+
)
17+
from eth.vm.state import (
18+
BaseState,
19+
)
1220

1321

14-
def get_gas_estimator() -> Callable[[BaseState, BaseTransaction], int]:
22+
def get_gas_estimator() -> Callable[[BaseState, Union[BaseTransaction, SpoofTransaction]], int]:
1523
import_path = os.environ.get(
1624
'GAS_ESTIMATOR_BACKEND_FUNC',
1725
'eth.estimators.gas.binary_gas_search_intrinsic_tolerance',
1826
)
19-
return cast(Callable[[BaseState, BaseTransaction], int], import_string(import_path))
27+
return cast(
28+
Callable[[BaseState, Union[BaseTransaction, SpoofTransaction]], int],
29+
import_string(import_path)
30+
)

eth/typing.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
Any,
33
Callable,
44
Dict,
5+
Generic,
56
Iterable,
67
List,
78
NewType,
89
Tuple,
910
Union,
11+
TypeVar,
1012
)
1113

1214
from eth_typing import (
@@ -52,3 +54,18 @@
5254
VRS = NewType("VRS", Tuple[int, int, int])
5355

5456
IntConvertible = Union[int, bytes, HexStr, str]
57+
58+
59+
TFunc = TypeVar('TFunc')
60+
61+
62+
class StaticMethod(Generic[TFunc]):
63+
"""
64+
A property class purely to convince mypy to let us assign a function to an
65+
instance variable. See more at: https://github.com/python/mypy/issues/708#issuecomment-405812141
66+
"""
67+
def __get__(self, oself: Any, owner: Any) -> TFunc:
68+
return self._func
69+
70+
def __set__(self, oself: Any, value: TFunc) -> None:
71+
self._func = value

trinity/utils/datastructures.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,20 @@
4444
pipe,
4545
)
4646

47+
from eth.typing import (
48+
StaticMethod,
49+
)
50+
4751
from trinity.utils.queues import (
4852
queue_get_batch,
4953
queue_get_nowait,
5054
)
5155

52-
TFunc = TypeVar('TFunc')
5356
TPrerequisite = TypeVar('TPrerequisite', bound=Enum)
5457
TTask = TypeVar('TTask')
5558
TTaskID = TypeVar('TTaskID')
5659

5760

58-
class StaticMethod(Generic[TFunc]):
59-
"""
60-
A property class purely to convince mypy to let us assign a function to an
61-
instance variable. See more at: https://github.com/python/mypy/issues/708#issuecomment-405812141
62-
"""
63-
def __get__(self, oself: Any, owner: Any) -> TFunc:
64-
return self._func
65-
66-
def __set__(self, oself: Any, value: TFunc) -> None:
67-
self._func = value
68-
69-
7061
@total_ordering
7162
class SortableTask(Generic[TTask]):
7263
_order_fn: StaticMethod[Callable[[TTask], Any]] = None

0 commit comments

Comments
 (0)