Complete Type Hinting for eth.tools#1420
Conversation
|
@cburgdorf I had to change the
|
| # | ||
| @functools.lru_cache(maxsize=1024) | ||
| def normalize_int(value): | ||
| def normalize_int(value: Any) -> int: |
There was a problem hiding this comment.
Will go through these more thoroughly tomorrow but wanted to drop a note that these input types can probably be tightened up to a Union[int, bytes, eth_typing.HexStr, str] (and so on for the other normalize_ functions as we should be able to define the corpus of allowed inputs.
There was a problem hiding this comment.
Might even qualify for a type alias (not a NewType) to be put in eth.typing:
IntLike = Union[int, bytes, eth_typing.HexStr, str]
Alternative names: IntConvertible, IntRetrievable
|
@Bhargavasomu I'll give this a proper review tomorrow! |
cburgdorf
left a comment
There was a problem hiding this comment.
Solid work! Left a few minor notes inline.
|
|
||
|
|
||
| def hash_log_entries(log_entries): | ||
| def hash_log_entries(log_entries: Iterable[Tuple[bytes, bytes, bytes]]) -> bytes: |
There was a problem hiding this comment.
As a rule of thumb, keccak will always return a hash of 32 bytes, hence everything that returns keccak(x) should always have the return type Hash32 rather than just arbitrary bytes.
| # | ||
| @functools.lru_cache(maxsize=1024) | ||
| def normalize_int(value): | ||
| def normalize_int(value: Any) -> int: |
There was a problem hiding this comment.
Might even qualify for a type alias (not a NewType) to be put in eth.typing:
IntLike = Union[int, bytes, eth_typing.HexStr, str]
Alternative names: IntConvertible, IntRetrievable
| List[Tuple[Address, Dict[str, Union[int, bytes, Dict[int, int]]]]] | ||
| ] | ||
|
|
||
| TransactionType = Dict[str, Union[bytes, int, str]] |
There was a problem hiding this comment.
Did you try locking this down to a TypedDict? Also, in general I prefer not to use the suffix Type for any new types / aliases that we create. It feels redundant to me (we also say Chain, BaseComputation, BaseVM and not ChainType, BaseComputationType or BaseVMType.
There was a problem hiding this comment.
As a name suggestion, maybe just TransactionDict?
There was a problem hiding this comment.
def normalize_int(value: Union[int, bytes, str, ...]) -> int
"""
Robust to integer conversion, handling hex values, string representations,
and special cases like `0x`.
"""
if is_integer(value):
return value
...
...
@cburgdorf @pipermerriam if the type hinting was changed as above, then the return value would return type Union[int, bytes, str, ...] instead of int which would throw an error. I have tested those and tried tightening the types prior to this PR.
There was a problem hiding this comment.
Inside the if is_integer block, it seems reasonable to cast(int, value).
There was a problem hiding this comment.
If we need to cast it, then we would need to cast it to a new variable (which I feel is redundant). How do we overwrite the type of a variable, which is already type hinted? This is because, mypy is throwing error when trying to overwrite the type hinting of the variables which are already type hinted.
value = cast(int, value)
return value
The following error occurs Incompatible return value type (got "Union[int, bytes, ...]", expected "int")
There was a problem hiding this comment.
I think it's fine to: return cast(int, value)
I'm not sure why mypy doesn't like the cast. @cburgdorf is our resident mypy wrangler.
|
In general how do I overwrite the Type hinting of a variable? This arises in the following scenario. This above function is generic and used by a lot of functions. But I need to tighten the functions which are using this function. Explained in below example. The normalize_main_transaction function returns object of the type How can the above be achieved? |
1018e03 to
f08b381
Compare
|
@cburgdorf I have done some searching regarding
Because of this, in the code, I have used And by the How should I proceed with this? |
Since Lines 116 to 119 in e48b279 Once we drop Python 3.5 support for the |
ed7b1a8 to
4865d68
Compare
|
@cburgdorf I don't remember changing anything related to |
|
@Bhargavasomu what you're seeing looks to be the same issue that I fixed in #1416 |
4865d68 to
c616ac9
Compare
|
@pipermerriam , so maybe I should wait till #1416 gets merged? |
|
It has now been merged, try rebasing. |
c616ac9 to
0c14e29
Compare
|
@pipermerriam this is causing issues in the |
|
@cburgdorf I have made the changes as required, the tests are failing because of some |
|
@Bhargavasomu I'll take a look today |
cburgdorf
left a comment
There was a problem hiding this comment.
This looks good to me now. I applied some minor fine tuning on top. I think one could go crazy on generics to tighten some of the Any usage but honestly all these places are imho not worth going after. It's all "just" eth.tools stuff after all.
I'll leave this open just in case anyone else wants to give this another look. If I don't see anyone adding more comments by tomorrow, I'll merge ;)
@Bhargavasomu Feel free to jump to the next module (eth.vm, slicing PRs for submodules in whichever way works best for you)
As soon as this is merged, I'll trigger the payout for that achieved milestone. Great job again 👍
|
@cburgdorf forgot about the NormalizerType, sorry about that. Will make the PR for the |
|
@Bhargavasomu Likewise! You are doing great work and I'm happy to have you contributing 👍 I also just triggered the payout of the first 150 DAI for this milestone. |


What was wrong?
Issue : #1398
How was it fixed?
Adding the type hints for
eth.toolsmodule completely.Cute Animal Picture