Skip to content

Christoph/fix/typehints #1412

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

Merged
merged 7 commits into from
Oct 19, 2018
Merged

Conversation

cburgdorf
Copy link
Contributor

@cburgdorf cburgdorf commented Oct 19, 2018

This is mostly #1404 by @Bhargavasomu with some minimal fine tuning on top.

What was wrong?

The eth module was not type hinted till now. This needs to be fixed and also the tox.ini file needs to be updated.
Issue : #1398

How was it fixed?

Adding type hints to the arguments and the function. The phase of adding the type hints is as follows

  • eth.utils (Started previously and hence wanted to finish it)
  • eth.tools
  • eth.vm
  • Everything else which is leftover

Cute Animal Picture

put a cute animal picture link inside the parentheses


VMConfigurationType = Iterable[Tuple[int, Type[BaseVM]]]
GeneralStateType = Union[AccountState, List[Tuple[Address, Dict[str, Union[int, bytes, Dict[int, int]]]]]] # noqa: E501
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the inner Dict should be updated to be an AccountDetals.


# TODO: Move into eth_typing

AccountDetails = TypedDict('AccountDetails',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooooh! TypedDict is cool.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, they let us avoid sprinkling the code with assert statements #1404 (comment)


def import_string(dotted_path):

def import_string(dotted_path: str) -> Any:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use types.ModuleType here instead of Any?

@@ -12,7 +15,7 @@
)


def int_to_bytes32(value):
def int_to_bytes32(value: Union[int, bool]) -> bytes:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Hash32 is more appropriate than bytes

def create_transaction_signature(unsigned_txn, private_key, chain_id=None):
def create_transaction_signature(unsigned_txn: BaseTransaction,
private_key: datatypes.PrivateKey,
chain_id: int=None) -> Tuple[int, int, int]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

candidate for eth-typing VRS = NewType("VRS", Tuple[int, int, int])

also, UINT256 = NewType("UINT256", int) for these values that are specifically bounded within the 256 bit integer space.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So VRS would then be a VRS = NewType("VRS", Tuple[UINT256, UINT256, UINT256])?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, though v might have a lower ceiling than the others (not sure what it is, might be a single byte).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't talk low level math with me! What does v even stand for? Vitalik? ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://bitcoin.stackexchange.com/questions/38351/ecdsa-v-r-s-what-is-v/38909

v is the recovery id and looks to be reasonably bounded within a single byte (2**8 or UINT8)

@cburgdorf cburgdorf force-pushed the christoph/fix/typehints branch from a649738 to d75a4ff Compare October 19, 2018 15:38
@cburgdorf cburgdorf force-pushed the christoph/fix/typehints branch from 7d5e5ad to 141a415 Compare October 19, 2018 15:56
@cburgdorf cburgdorf force-pushed the christoph/fix/typehints branch from 141a415 to e27638a Compare October 19, 2018 16:05
@cburgdorf cburgdorf merged commit e4a203a into ethereum:master Oct 19, 2018
@cburgdorf
Copy link
Contributor Author

@Bhargavasomu thanks again for doing the bulk of the work! You may want to check out the few extra commits on top just to get a feel what kind of changes were made.

@@ -45,7 +45,7 @@ commands=
native-state-metropolis: pytest {posargs:tests/json-fixtures/test_state.py -k Metropolis}
lightchain_integration: pytest --integration {posargs:tests/trinity/integration/test_lightchain_integration.py}

deps = .[p2p,trinity,eth-extra,test]
deps = .[eth,p2p,trinity,eth-extra,test]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is confusing to me: . should already be installing those packages:
https://github.com/cburgdorf/py-evm/blob/db8bf2e1003bb18542a3e36ff46fbffabfddc808/setup.py#L102

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out, now I'm confused as well. Let's see #1417

@@ -12,6 +12,7 @@
"eth-typing>=2.0.0,<3.0.0",
"eth-utils>=1.3.0b0,<2.0.0",
"lru-dict>=1.1.6",
"mypy_extensions>=0.4.1,<1.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels a little weird, like including flake8 in the distribution. Don't feel strongly about it though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the end of the day, mypy_extensions is a library as any other and we are using it now for things like TypedDict. Notice that this is not just a type hint per se. We have to have it in order for code such as the following to work.

py-evm/eth/typing.py

Lines 20 to 25 in 7ad4259

AccountDetails = TypedDict('AccountDetails',
{'balance': int,
'nonce': int,
'code': bytes,
'storage': Dict[int, int]
})

@Bhargavasomu
Copy link
Contributor

@cburgdorf I noticed that you have moved some type definitions to eth.typing and importing from there, which is fancy BTW. I haven't known we could do this, definitely will use this method for frequently invoking type definitions

@cburgdorf
Copy link
Contributor Author

Yep, and in the long run we might move some if not all to https://github.com/ethereum/eth-typing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants