Skip to content
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

Enable Type Hinting Completely #48

Merged
merged 1 commit into from
Nov 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions eth_keys/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
import codecs
import collections
import sys
from typing import (Any, Optional, Tuple, Union, Type) # noqa: F401
from typing import ( # noqa: F401
Any,
Tuple,
Union,
Type,
TYPE_CHECKING,
)

from eth_utils import (
big_endian_to_int,
Expand Down Expand Up @@ -39,11 +45,7 @@
validate_signature_bytes,
)


# Workaround for import cycles caused by type annotations:
# http://mypy.readthedocs.io/en/latest/common_issues.html#import-cycles
MYPY = False
if MYPY:
if TYPE_CHECKING:
from eth_keys.backends.base import BaseECCBackend # noqa: F401


Expand Down Expand Up @@ -83,21 +85,21 @@ def __init__(self,

self.backend = backend

_backend = None # type: bytes
_backend = None # type: BaseECCBackend

@property
def backend(self):
def backend(self) -> 'BaseECCBackend':
if self._backend is None:
return self.get_backend()
else:
return self._backend

@backend.setter
def backend(self, value):
def backend(self, value: 'BaseECCBackend') -> None:
self._backend = value

@classmethod
def get_backend(cls, *args, **kwargs):
def get_backend(cls, *args: Any, **kwargs: Any) -> 'BaseECCBackend':
from eth_keys.backends import get_backend
return get_backend(*args, **kwargs)

Expand Down Expand Up @@ -335,8 +337,7 @@ def __bytes__(self) -> bytes:
vb = int_to_byte(self.v)
rb = pad32(int_to_big_endian(self.r))
sb = pad32(int_to_big_endian(self.s))
# FIXME: Enable type checking once we have type annotations in eth_utils
return b''.join((rb, sb, vb)) # type: ignore
return b''.join((rb, sb, vb))

def __str__(self) -> str:
return self.to_hex()
Expand Down
21 changes: 8 additions & 13 deletions eth_keys/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ class KeyAPI(LazyBackend):
# Proxy method calls to the backends
#
def ecdsa_sign(self,
message_hash, # type: bytes
private_key # type: _PrivateKey
):
# type: (...) -> _Signature
message_hash: bytes,
private_key: _PrivateKey) -> _Signature:
validate_message_hash(message_hash)
if not isinstance(private_key, PrivateKey):
raise ValidationError(
Expand All @@ -51,21 +49,18 @@ def ecdsa_sign(self,
return signature

def ecdsa_verify(self,
message_hash, # type: bytes
signature, # type: _Signature
public_key # type: _PublicKey
) -> bool:
message_hash: bytes,
signature: _Signature,
public_key: _PublicKey) -> bool:
if not isinstance(public_key, PublicKey):
raise ValidationError(
"The `public_key` must be an instance of `eth_keys.datatypes.PublicKey`"
)
return self.ecdsa_recover(message_hash, signature) == public_key

def ecdsa_recover(self,
message_hash, # type: bytes
signature # type: _Signature
):
# type: (...) -> _PublicKey
message_hash: bytes,
signature: _Signature) -> _PublicKey:
validate_message_hash(message_hash)
if not isinstance(signature, Signature):
raise ValidationError(
Expand All @@ -79,7 +74,7 @@ def ecdsa_recover(self,
)
return public_key

def private_key_to_public_key(self, private_key):
def private_key_to_public_key(self, private_key: _PrivateKey) -> _PublicKey:
if not isinstance(private_key, PrivateKey):
raise ValidationError(
"The `private_key` must be an instance of `eth_keys.datatypes.PrivateKey`"
Expand Down
Empty file added eth_keys/py.typed
Empty file.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
py_modules=['eth_keys'],
license="MIT",
zip_safe=False,
package_data={'eth-keys': ['py.typed']},
keywords='ethereum',
packages=find_packages(exclude=["tests", "tests.*"]),
classifiers=[
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ deps=mypy<0.600
setenv=MYPYPATH={toxinidir}
# TODO: Drop --ignore-missing-imports once we have type annotations for eth_utils, coincurve and cytoolz
commands=
mypy --ignore-missing-imports --check-untyped-defs --disallow-incomplete-defs -p eth_keys
mypy --follow-imports=silent --warn-unused-ignores --ignore-missing-imports --no-strict-optional --check-untyped-defs --disallow-incomplete-defs --disallow-untyped-defs --disallow-any-generics -p eth_keys