Skip to content

Commit

Permalink
run lint flake8 and isort, use f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
pacrob committed Dec 4, 2023
1 parent c6aec89 commit 0c0a877
Show file tree
Hide file tree
Showing 67 changed files with 1,220 additions and 455 deletions.
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exclude: '.project-template|docs/conf.py'
exclude: '.project-template|docs/conf.py|.bumpversion.cfg'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
Expand All @@ -17,7 +17,7 @@ repos:
- id: flake8
additional_dependencies:
- flake8-bugbear==23.9.16
exclude: setup.py
exclude: 'setup.py'
- repo: https://github.com/PyCQA/autoflake
rev: v2.2.1
hooks:
Expand All @@ -42,4 +42,5 @@ repos:
rev: v1.5.1
hooks:
- id: mypy
exclude: tests/
# mypy checks are turned off for now, see issue #8
exclude: 'tests/|scripts/|ssz/'
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
[![Python versions](https://img.shields.io/pypi/pyversions/ssz.svg)](https://pypi.python.org/pypi/ssz)
[![Docs build](https://readthedocs.org/projects/ssz/badge/?version=latest)](https://ssz.readthedocs.io/en/latest/?badge=latest)


Python implementation of the Simple Serialization encoding and decoding

Read more in the [documentation on ReadTheDocs](https://ssz.readthedocs.io/). [View the change log](https://ssz.readthedocs.io/en/latest/release_notes.html).
Expand Down
1 change: 0 additions & 1 deletion docs/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,3 @@ v0.1.0-alpha.0
--------------

- Launched repository, claimed names for pip, RTD, github, etc

1 change: 1 addition & 0 deletions newsfragments/134.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Drop support for python 3.7
1 change: 1 addition & 0 deletions newsfragments/134.internal.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Merge changes from the template, including use ``pre-commit`` for linting and change the name of the ``master`` branch to ``main``
28 changes: 18 additions & 10 deletions scripts/benchmark/hash_tree.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
from contextlib import contextmanager
from itertools import chain
from contextlib import (
contextmanager,
)
from itertools import (
chain,
)
import random
import time

from ssz.hash_tree import HashTree
from ssz.utils import merkleize_with_cache
from ssz.hash_tree import (
HashTree,
)
from ssz.utils import (
merkleize_with_cache,
)

NUM_CHUNKS = 1000000
NUM_UPDATES = 100
Expand All @@ -30,28 +38,28 @@ def benchmark(message):
print()
print("-- Merkleize --")
cache = {}
with benchmark(f"First root access"):
with benchmark("First root access"):
merkleize_with_cache(chunks, cache)

with benchmark(f"Second root access"):
with benchmark("Second root access"):
merkleize_with_cache(chunks, cache)

for update_index in update_indices:
chunks[update_index] = get_random_chunk()
with benchmark(f"Update and root access"):
with benchmark("Update and root access"):
merkleize_with_cache(chunks, cache)

print()
print("-- Hash tree -- ")

with benchmark(f"First root access"):
with benchmark("First root access"):
hash_tree = HashTree.compute(chunks)
hash_tree.root

with benchmark(f"Second root access"):
with benchmark("Second root access"):
hash_tree.root

with benchmark(f"Update and root access"):
with benchmark("Update and root access"):
updates = chain(
*((update_index, get_random_chunk()) for update_index in update_indices)
)
Expand Down
30 changes: 18 additions & 12 deletions scripts/benchmark/tree_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
import time

import ssz
from ssz.cache.cache import SSZCache
from ssz.hashable_container import HashableContainer
from ssz.hashable_list import HashableList
from ssz.hashable_vector import HashableVector
from ssz.cache.cache import (
SSZCache,
)
from ssz.hashable_container import (
HashableContainer,
)
from ssz.hashable_list import (
HashableList,
)
from ssz.hashable_vector import (
HashableVector,
)
from ssz.sedes import (
List,
Serializable,
Expand Down Expand Up @@ -159,8 +167,8 @@ class HashableEth1Data(HashableContainer):

def update_tuple_item_with_fn(tuple_data, index, fn, *args):
"""
Update the ``index``th item of ``tuple_data`` to the result of calling ``fn`` on the existing
value.
Update the ``index``th item of ``tuple_data`` to the result of calling ``fn`` on
the existing value.
"""
list_data = list(tuple_data)

Expand All @@ -169,9 +177,8 @@ def update_tuple_item_with_fn(tuple_data, index, fn, *args):
list_data[index] = fn(old_value, *args)
except IndexError:
raise Exception(
"the length of the given tuple_data is {}, the given index {} is out of index".format(
len(tuple_data), index
)
f"The length of the given tuple_data is {len(tuple_data)}, the given index "
f"{index} is out of index"
)
else:
return tuple(list_data)
Expand Down Expand Up @@ -352,7 +359,6 @@ def benchmark():
raise RuntimeError("state benchmark has not been run")
elif results["state"] > TOLERABLE_PERFORMANCE:
raise TimeoutError(
"hash_tree_root is not fast enough. Tolerable: {}, Actual: {}".format(
TOLERABLE_PERFORMANCE, results["state"]
)
f"hash_tree_root is not fast enough. Tolerable: {TOLERABLE_PERFORMANCE}, "
f"Actual: {results['state']}"
)
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@
}

extras_require["dev"] = (
extras_require["dev"] + extras_require["docs"] + extras_require["test"] + extras_require["yaml"]
extras_require["dev"]
+ extras_require["docs"]
+ extras_require["test"]
+ extras_require["yaml"]
)

with open("./README.md") as readme:
Expand Down
23 changes: 18 additions & 5 deletions ssz/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
from .cache import SSZCache # noqa: F401
from .codec import decode, encode # noqa: F401
from .exceptions import ( # noqa: F401
from importlib.metadata import (
version as __version,
)

from .cache import (
SSZCache,
)
from .codec import (
decode,
encode,
)
from .exceptions import (
DeserializationError,
SerializationError,
SSZException,
)
from .sedes import ( # noqa: F401
from .sedes import (
BaseSedes,
BasicSedes,
Bitlist,
Expand Down Expand Up @@ -34,4 +43,8 @@
uint128,
uint256,
)
from .tree_hash import get_hash_tree_root # noqa: F401
from .tree_hash import (
get_hash_tree_root,
)

__version__ = __version("ssz")
35 changes: 27 additions & 8 deletions ssz/abc.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
from abc import ABC, abstractmethod
from typing import Any, Generic, Iterable, Iterator, Optional, TypeVar, Union

from eth_typing import Hash32
from pyrsistent.typing import PVector

from ssz.hash_tree import HashTree
from ssz.sedes.base import BaseProperCompositeSedes
from abc import (
ABC,
abstractmethod,
)
from typing import (
Any,
Generic,
Iterable,
Iterator,
Optional,
TypeVar,
Union,
)

from eth_typing import (
Hash32,
)
from pyrsistent.typing import (
PVector,
)

from ssz.hash_tree import (
HashTree,
)
from ssz.sedes.base import (
BaseProperCompositeSedes,
)

TStructure = TypeVar("TStructure")
TElement = TypeVar("TElement")
Expand Down
4 changes: 3 additions & 1 deletion ssz/cache/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .cache import SSZCache # noqa: F401
from .cache import (
SSZCache,
)
15 changes: 11 additions & 4 deletions ssz/cache/cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from collections.abc import MutableMapping
from typing import TYPE_CHECKING, Iterator

from lru import LRU
from collections.abc import (
MutableMapping,
)
from typing import (
TYPE_CHECKING,
Iterator,
)

from lru import (
LRU,
)

if TYPE_CHECKING:
MM = MutableMapping[bytes, bytes]
Expand Down
25 changes: 18 additions & 7 deletions ssz/cache/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import functools
from typing import Any, Iterable

from eth_typing import Hash32
from eth_utils import to_tuple

from ssz.sedes.base import TSedes
from ssz.typing import CacheObj
from typing import (
Any,
Iterable,
)

from eth_typing import (
Hash32,
)
from eth_utils import (
to_tuple,
)

from ssz.sedes.base import (
TSedes,
)
from ssz.typing import (
CacheObj,
)


def get_key(sedes, value: Any) -> str:
Expand Down
15 changes: 11 additions & 4 deletions ssz/codec.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from eth_utils import is_bytes

from ssz.sedes import infer_sedes, sedes_by_name
from ssz.sedes.base import BaseSedes
from eth_utils import (
is_bytes,
)

from ssz.sedes import (
infer_sedes,
sedes_by_name,
)
from ssz.sedes.base import (
BaseSedes,
)


def encode(value, sedes=None):
Expand Down
13 changes: 10 additions & 3 deletions ssz/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from eth_typing import Hash32
from eth_utils.toolz import iterate, take
from eth_typing import (
Hash32,
)
from eth_utils.toolz import (
iterate,
take,
)

from ssz.hash import hash_eth2
from ssz.hash import (
hash_eth2,
)

CHUNK_SIZE = 32 # named BYTES_PER_CHUNK in the spec
EMPTY_CHUNK = Hash32(b"\x00" * CHUNK_SIZE)
Expand Down
5 changes: 4 additions & 1 deletion ssz/examples/zoo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from ssz.hashable_container import HashableContainer, SignedHashableContainer
from ssz.hashable_container import (
HashableContainer,
SignedHashableContainer,
)
from ssz.sedes import (
Bitlist,
Bitvector,
Expand Down
6 changes: 0 additions & 6 deletions ssz/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ class SSZException(Exception):
Base class for exceptions raised by this package.
"""

pass


class SerializationError(SSZException):
"""
Exception raised if serialization fails.
"""

pass


class DeserializationError(SSZException):
"""
Exception raised if deserialization fails.
"""

pass
8 changes: 5 additions & 3 deletions ssz/hash.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import functools
import hashlib

from eth_typing import Hash32
from eth_typing import (
Hash32,
)


@functools.lru_cache(maxsize=2**12)
def hash_eth2(data: bytes) -> Hash32:
"""
Return SHA-256 hashed result.
Note: it's a placeholder and we aim to migrate to a S[T/N]ARK-friendly hash function in
a future Ethereum 2.0 deployment phase.
Note: it's a placeholder and we aim to migrate to a S[T/N]ARK-friendly hash function
in a future Ethereum 2.0 deployment phase.
"""
return Hash32(hashlib.sha256(data).digest())
Loading

0 comments on commit 0c0a877

Please sign in to comment.