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

Make the switch to docc #833

Merged
merged 13 commits into from
Jan 9, 2024
Prev Previous commit
Next Next commit
Fix mypy issues in common code
SamWilsn committed Oct 31, 2023

Verified

This commit was signed with the committer’s verified signature.
SamWilsn Sam Wilson
commit 60121fe3c068b83fa9c145814e3c70e7588c65d6
54 changes: 44 additions & 10 deletions src/ethereum/base_types.py
Original file line number Diff line number Diff line change
@@ -14,8 +14,28 @@

from __future__ import annotations

from dataclasses import replace
from typing import Any, Callable, ClassVar, Optional, Tuple, Type, TypeVar
from dataclasses import is_dataclass, replace
from typing import (
Any,
Callable,
ClassVar,
Optional,
Protocol,
Tuple,
Type,
TypeVar,
runtime_checkable,
)


@runtime_checkable
class SlottedFreezable(Protocol):
"""
Represents data classes created with `@slotted_freezable`.
"""

_frozen: bool


U8_MAX_VALUE = (2**8) - 1
U32_MAX_VALUE = (2**32) - 1
@@ -186,7 +206,9 @@ def __rdivmod__(self, left: int) -> Tuple["Uint", "Uint"]:
int.__new__(self.__class__, result[1]),
)

def __pow__(self, right: int, modulo: Optional[int] = None) -> "Uint":
def __pow__( # type: ignore[override]
self, right: int, modulo: Optional[int] = None
) -> "Uint":
if modulo is not None:
if not isinstance(modulo, int):
return NotImplemented
@@ -202,7 +224,9 @@ def __pow__(self, right: int, modulo: Optional[int] = None) -> "Uint":

return int.__new__(self.__class__, int.__pow__(self, right, modulo))

def __rpow__(self, left: int, modulo: Optional[int] = None) -> "Uint":
def __rpow__( # type: ignore[misc]
self, left: int, modulo: Optional[int] = None
) -> "Uint":
if modulo is not None:
if not isinstance(modulo, int):
return NotImplemented
@@ -218,7 +242,9 @@ def __rpow__(self, left: int, modulo: Optional[int] = None) -> "Uint":

return int.__new__(self.__class__, int.__rpow__(self, left, modulo))

def __ipow__(self, right: int, modulo: Optional[int] = None) -> "Uint":
def __ipow__( # type: ignore[override]
self, right: int, modulo: Optional[int] = None
) -> "Uint":
return self.__pow__(right, modulo)

def __xor__(self, right: int) -> "Uint":
@@ -268,7 +294,7 @@ def to_be_bytes(self) -> "Bytes":
byte_length = (bit_length + 7) // 8
return self.to_bytes(byte_length, "big")

def to_le_bytes(self, number_bytes: int = None) -> "Bytes":
def to_le_bytes(self, number_bytes: Optional[int] = None) -> "Bytes":
"""
Converts this arbitrarily sized unsigned integer into its little endian
representation.
@@ -517,7 +543,9 @@ def __rdivmod__(self: T, left: int) -> Tuple[T, T]:
int.__new__(self.__class__, result[1]),
)

def __pow__(self: T, right: int, modulo: Optional[int] = None) -> T:
def __pow__( # type: ignore[override]
self: T, right: int, modulo: Optional[int] = None
) -> T:
if modulo is not None:
if not isinstance(modulo, int):
return NotImplemented
@@ -572,7 +600,9 @@ def wrapping_pow(self: T, right: int, modulo: Optional[int] = None) -> T:
self.__class__, int.__pow__(self, right, modulo) & self.MAX_VALUE
)

def __rpow__(self: T, left: int, modulo: Optional[int] = None) -> T:
def __rpow__( # type: ignore[misc]
self: T, left: int, modulo: Optional[int] = None
) -> T:
if modulo is not None:
if not isinstance(modulo, int):
return NotImplemented
@@ -588,7 +618,9 @@ def __rpow__(self: T, left: int, modulo: Optional[int] = None) -> T:

return int.__new__(self.__class__, int.__rpow__(self, left, modulo))

def __ipow__(self: T, right: int, modulo: Optional[int] = None) -> T:
def __ipow__( # type: ignore[override]
self: T, right: int, modulo: Optional[int] = None
) -> T:
return self.__pow__(right, modulo)

def __and__(self: T, right: int) -> T:
@@ -1002,7 +1034,9 @@ def modify(obj: S, f: Callable[[S], None]) -> S:
new_obj : `S`
Compact byte array.
"""
assert is_dataclass(obj)
assert isinstance(obj, SlottedFreezable)
new_obj = replace(obj, _frozen=False)
f(new_obj)
new_obj._frozen = True # type: ignore
new_obj._frozen = True
return new_obj
3 changes: 3 additions & 0 deletions src/ethereum/crypto/finite_field.py
Original file line number Diff line number Diff line change
@@ -375,6 +375,9 @@ def __pow__(self: U, exponent: int) -> U:
exponent //= 2
return res

def __ipow__(self: U, right: int) -> U:
return self.__pow__(right)

@classmethod
def calculate_frobenius_coefficients(cls: Type[U]) -> Tuple[U, ...]:
"""
4 changes: 1 addition & 3 deletions src/ethereum/fork_criteria.py
Original file line number Diff line number Diff line change
@@ -17,9 +17,7 @@
from typing import Final, Tuple


# MyPy doesn't support decorators on abstract classes
# See https://github.com/python/mypy/issues/4717
@functools.total_ordering # type: ignore
@functools.total_ordering
class ForkCriteria(ABC):
"""
Type that represents the condition required for a fork to occur.
4 changes: 2 additions & 2 deletions src/ethereum/rlp.py
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@
from __future__ import annotations

from dataclasses import astuple, fields, is_dataclass
from typing import Any, List, Sequence, Tuple, Type, TypeVar, Union
from typing import Any, List, Sequence, Tuple, Type, TypeVar, Union, cast

from ethereum.crypto.hash import Hash32, keccak256
from ethereum.exceptions import RLPDecodingError, RLPEncodingError
@@ -293,7 +293,7 @@ def _decode_to(cls: Type[T], raw_rlp: RLP) -> T:
ensure(len(fields(cls)) == len(raw_rlp), RLPDecodingError)
for field, rlp_item in zip(fields(cls), raw_rlp):
args.append(_decode_to(field.type, rlp_item))
return cls(*args)
return cast(T, cls(*args))
else:
raise RLPDecodingError(
"RLP Decoding to type {} is not supported".format(cls)
4 changes: 2 additions & 2 deletions src/ethereum_spec_tools/forks.py
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ def discover(cls: Type[H], base: Optional[PurePath] = None) -> List[H]:
raise ValueError("unable to find module from file")
ethereum = importlib.util.module_from_spec(spec)
if spec.loader and hasattr(spec.loader, "exec_module"):
spec.loader.exec_module(ethereum) # type: ignore[attr-defined]
spec.loader.exec_module(ethereum)

path = getattr(ethereum, "__path__", None)
if path is None:
@@ -172,7 +172,7 @@ def criteria(self) -> "ForkCriteria":
"""
from ethereum.fork_criteria import ForkCriteria

criteria = self.mod.FORK_CRITERIA # type: ignore[attr-defined]
criteria = self.mod.FORK_CRITERIA
assert isinstance(criteria, ForkCriteria)
return criteria

5 changes: 2 additions & 3 deletions tests/helpers/load_evm_tools_tests.py
Original file line number Diff line number Diff line change
@@ -2,11 +2,10 @@
import os
import sys
from io import StringIO
from typing import Dict, Generator, Tuple
from typing import Dict, Generator, Optional, Tuple

import pytest

from ethereum import rlp
from ethereum.utils.hexadecimal import hex_to_bytes
from ethereum_spec_tools.evm_tools import parser, subparsers
from ethereum_spec_tools.evm_tools.t8n import T8N, t8n_arguments
@@ -17,7 +16,7 @@
def fetch_evm_tools_tests(
test_dir: str,
fork_name: str,
slow_tests: Tuple[str, ...] = None,
slow_tests: Optional[Tuple[str, ...]] = None,
) -> Generator:
"""
Fetches all the general state tests from the given directory
6 changes: 3 additions & 3 deletions tests/test_rlp.py
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ def test_rlp_encode_single_elem_list_uint() -> None:


def test_rlp_encode_10_elem_byte_uint_combo() -> None:
raw_data = [b"hello"] * 5 + [Uint(35)] * 5 # type: ignore
raw_data = [b"hello"] * 5 + [Uint(35)] * 5
expected = (
bytearray([0xE3])
+ b"\x85hello\x85hello\x85hello\x85hello\x85hello#####"
@@ -130,7 +130,7 @@ def test_rlp_encode_10_elem_byte_uint_combo() -> None:


def test_rlp_encode_20_elem_byte_uint_combo() -> None:
raw_data = [Uint(35)] * 10 + [b"hello"] * 10 # type: ignore
raw_data = [Uint(35)] * 10 + [b"hello"] * 10
expected = (
bytearray([0xF8])
+ b"F"
@@ -159,7 +159,7 @@ def test_rlp_encode_successfully() -> None:
(Uint(255), b"\x81\xff"),
([], bytearray([0xC0])),
(
[b"hello"] * 5 + [Uint(35)] * 5, # type: ignore
[b"hello"] * 5 + [Uint(35)] * 5,
bytearray([0xE3])
+ bytearray(b"\x85hello\x85hello\x85hello\x85hello\x85hello#####"),
),