Skip to content

Commit

Permalink
Merge pull request #74 from palash25/type-hints
Browse files Browse the repository at this point in the history
[WIP] Add type hints
  • Loading branch information
davesque committed Aug 30, 2018
2 parents 7fd3bcc + 52abb85 commit 05963d5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ venv
pip-log.txt

# Unit test / coverage reports
.mypy_cache
.coverage
htmlcov/
.tox
Expand Down
19 changes: 14 additions & 5 deletions eth_abi/utils/numeric.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import decimal
from typing import (
Tuple,
)

abi_decimal_context = decimal.Context(prec=999)

ZERO = decimal.Decimal(0)
TEN = decimal.Decimal(10)


def ceil32(x):
def ceil32(x: int) -> int:
return x if x % 32 == 0 else x + 32 - (x % 32)


def compute_unsigned_integer_bounds(num_bits):
def compute_unsigned_integer_bounds(num_bits: int) -> Tuple[int, int]:
return (
0,
2 ** num_bits - 1,
)


def compute_signed_integer_bounds(num_bits):
def compute_signed_integer_bounds(num_bits: int) -> Tuple[int, int]:
return (
-1 * 2 ** (num_bits - 1),
2 ** (num_bits - 1) - 1,
)


def compute_unsigned_fixed_bounds(num_bits, frac_places):
def compute_unsigned_fixed_bounds(
num_bits: int,
frac_places: int,
) -> Tuple[decimal.Decimal, decimal.Decimal]:
int_upper = compute_unsigned_integer_bounds(num_bits)[1]

with decimal.localcontext(abi_decimal_context):
Expand All @@ -33,7 +39,10 @@ def compute_unsigned_fixed_bounds(num_bits, frac_places):
return ZERO, upper


def compute_signed_fixed_bounds(num_bits, frac_places):
def compute_signed_fixed_bounds(
num_bits: int,
frac_places: int,
) -> Tuple[decimal.Decimal, decimal.Decimal]:
int_lower, int_upper = compute_signed_integer_bounds(num_bits)

with decimal.localcontext(abi_decimal_context):
Expand Down
6 changes: 3 additions & 3 deletions eth_abi/utils/padding.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@


@curry
def zpad(value, length):
def zpad(value: bytes, length: int) -> bytes:
return value.rjust(length, b'\x00')


zpad32 = zpad(length=32)


@curry
def zpad_right(value, length):
def zpad_right(value: bytes, length: int) -> bytes:
return value.ljust(length, b'\x00')


zpad32_right = zpad_right(length=32)


@curry
def fpad(value, length):
def fpad(value: bytes, length: int) -> bytes:
return value.rjust(length, b'\xff')


Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'lint': [
"flake8==3.4.1",
"isort>=4.2.15,<5",
"mypy==0.620",
],
'doc': [
"Sphinx>=1.6.5,<2",
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ extras=lint
commands=
flake8 {toxinidir}/eth_abi {toxinidir}/tests
isort --recursive --check-only --diff {toxinidir}/eth_abi {toxinidir}/tests
mypy --follow-imports=silent --ignore-missing-imports --check-untyped-defs --disallow-incomplete-defs -p eth_abi.utils

0 comments on commit 05963d5

Please sign in to comment.