-
Notifications
You must be signed in to change notification settings - Fork 685
Complete Type Hinting for eth.tools
#1420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@cburgdorf I had to change the
|
eth/tools/_utils/normalization.py
Outdated
|
||
# | ||
# Primitives | ||
# | ||
@functools.lru_cache(maxsize=1024) | ||
def normalize_int(value): | ||
def normalize_int(value: Any) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solid work! Left a few minor notes inline.
eth/tools/_utils/hashing.py
Outdated
from eth.rlp.logs import Log | ||
|
||
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
eth/tools/_utils/normalization.py
Outdated
|
||
# | ||
# Primitives | ||
# | ||
@functools.lru_cache(maxsize=1024) | ||
def normalize_int(value): | ||
def normalize_int(value: Any) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
eth/typing.py
Outdated
@@ -32,4 +34,8 @@ | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As a name suggestion, maybe just TransactionDict
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inside the if is_integer
block, it seems reasonable to cast(int, value)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 https://github.com/ethereum/py-evm/pull/1416/files#diff-1d37e48f9ceff6d8030570cd36286a61L269 |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.tools
module completely.Cute Animal Picture