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

bump mypy to run locally, move tools/factories into tests folder #2197

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ repos:
- id: mdformat
additional_dependencies:
- mdformat-gfm
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.5.1
- repo: local
hooks:
- id: mypy
files: eth/
- id: mypy-local
name: run mypy with all dev dependencies present
entry: python -m mypy -p eth
language: system
always_run: true
pass_filenames: false
- repo: https://github.com/PrincetonUniversity/blocklint
rev: v0.2.5
hooks:
Expand Down
1 change: 1 addition & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ python:
path: .
extra_requirements:
- docs
- test

# Build all formats for RTD Downloads - htmlzip, pdf, epub
formats: all
10 changes: 7 additions & 3 deletions eth/consensus/clique/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import (
Iterable,
cast,
)

from eth_keys import (
Expand Down Expand Up @@ -91,9 +92,12 @@ def get_block_signer(header: BlockHeaderAPI) -> Address:

signature = keys.Signature(signature_bytes=signature_bytes)

return signature.recover_public_key_from_msg_hash(
signature_hash
).to_canonical_address()
return cast(
Address,
signature.recover_public_key_from_msg_hash(
signature_hash
).to_canonical_address(),
)


def is_in_turn(signer: Address, snapshot: Snapshot, header: BlockHeaderAPI) -> bool:
Expand Down
6 changes: 3 additions & 3 deletions eth/consensus/clique/snapshot_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@


def make_snapshot_lookup_key(block_hash: Hash32) -> bytes:
return f"block-hash-to-snapshot:{block_hash}".encode()
return f"block-hash-to-snapshot:{block_hash!r}".encode()


class SnapshotManager:
Expand Down Expand Up @@ -123,11 +123,11 @@ def apply(self, current_snapshot: Snapshot, header: BlockHeaderAPI) -> Snapshot:
if tally.votes > len(snapshot.signers) / 2:
if tally.action is VoteAction.NOMINATE:
snapshot.signers.append(header.coinbase)
self.logger.debug(f"New signer added: {header.coinbase}")
self.logger.debug(f"New signer added: {header.coinbase!r}")
else:
if header.coinbase in snapshot.signers:
snapshot.signers.remove(header.coinbase)
self.logger.debug(f"Signer removed: {header.coinbase}")
self.logger.debug(f"Signer removed: {header.coinbase!r}")

for vote in snapshot.votes.copy():
# Discard any pending votes *from* the added or removed member
Expand Down
4 changes: 2 additions & 2 deletions eth/db/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ def _persist_block(
uncles_hash = EMPTY_UNCLE_HASH
if uncles_hash != block.header.uncles_hash:
raise ValidationError(
f"Block's uncles_hash ({block.header.uncles_hash}) does not match "
f"actual uncles' hash ({uncles_hash})"
f"Block's uncles_hash ({block.header.uncles_hash!r}) does not match "
f"actual uncles' hash ({uncles_hash!r})"
)
new_canonical_hashes = tuple(header.hash for header in new_canonical_headers)
old_canonical_hashes = tuple(header.hash for header in old_canonical_headers)
Expand Down
6 changes: 3 additions & 3 deletions eth/db/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def make_block_number_to_hash_lookup_key(block_number: BlockNumber) -> bytes:

@staticmethod
def make_block_hash_to_score_lookup_key(block_hash: Hash32) -> bytes:
return f"block-hash-to-score:{block_hash}".encode()
return f"block-hash-to-score:{block_hash!r}".encode()

@staticmethod
def make_header_chain_gaps_lookup_key() -> bytes:
Expand All @@ -39,8 +39,8 @@ def make_checkpoint_headers_key() -> bytes:

@staticmethod
def make_transaction_hash_to_block_lookup_key(transaction_hash: Hash32) -> bytes:
return f"transaction-hash-to-block:{transaction_hash}".encode()
return f"transaction-hash-to-block:{transaction_hash!r}".encode()

@staticmethod
def make_withdrawal_hash_to_block_lookup_key(withdrawal_hash: Hash32) -> bytes:
return f"withdrawal-hash-to-block:{withdrawal_hash}".encode()
return f"withdrawal-hash-to-block:{withdrawal_hash!r}".encode()
7 changes: 6 additions & 1 deletion eth/precompiles/point_evaluation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import hashlib
import os
from typing import (
cast,
)

from ckzg import (
load_trusted_setup,
Expand Down Expand Up @@ -29,7 +32,9 @@


def kzg_to_versioned_hash(commitment: bytes) -> Hash32:
return VERSIONED_HASH_VERSION_KZG + hashlib.sha256(commitment).digest()[1:]
return cast(
Hash32, VERSIONED_HASH_VERSION_KZG + hashlib.sha256(commitment).digest()[1:]
)


def point_evaluation_precompile(computation: ComputationAPI) -> ComputationAPI:
Expand Down
27 changes: 27 additions & 0 deletions eth/rlp/blocks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import (
Sequence,
Type,
)

Expand All @@ -12,13 +13,39 @@
)
from eth.abc import (
BlockAPI,
BlockHeaderAPI,
SignedTransactionAPI,
TransactionBuilderAPI,
WithdrawalAPI,
)


class BaseBlock(Configurable, rlp.Serializable, BlockAPI):
transaction_builder: Type[TransactionBuilderAPI] = None

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was added to clarify that it is the rlp.Serializable's __init__ that we want, not BlockAPI's. At runtime, the correct one was being chosen by default, but mypy was confused. And I think it's better to be explicit about it.

def __init__(
self,
header: BlockHeaderAPI,
transactions: Sequence[SignedTransactionAPI] = None,
uncles: Sequence[BlockHeaderAPI] = None,
withdrawals: Sequence[WithdrawalAPI] = None,
) -> None:
if withdrawals is not None:
rlp.Serializable.__init__(
self,
header=header,
transactions=transactions,
uncles=uncles,
withdrawals=withdrawals,
)
else:
rlp.Serializable.__init__(
self,
header=header,
transactions=transactions,
uncles=uncles,
)

@classmethod
def get_transaction_builder(cls) -> Type[TransactionBuilderAPI]:
if cls.transaction_builder is None:
Expand Down
10 changes: 9 additions & 1 deletion eth/tools/fixtures/loading.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from pytest import (
Mark,
MarkDecorator,
)
import functools
import json
import os
from typing import (
Any,
Callable,
Collection,
Dict,
Iterable,
Tuple,
Union,
)

from eth_utils import (
Expand Down Expand Up @@ -81,7 +87,9 @@ def load_fixture(
def filter_fixtures(
all_fixtures: Iterable[Any],
fixtures_base_dir: str,
mark_fn: Callable[[str, str], bool] = None,
mark_fn: Callable[
[str, str], Union[MarkDecorator, Collection[Union[MarkDecorator, Mark]], None]
] = None,
ignore_fn: Callable[..., bool] = None,
) -> Any:
"""
Expand Down
4 changes: 3 additions & 1 deletion eth/tools/mining.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class POWMiningMixin(VirtualMachineAPI):
"""

def finalize_block(self, block: BlockAPI) -> BlockAndMetaWitness:
block_result = super().finalize_block(block)
# type ignored because as a mixin, we expect to only use this with another
# class that properly implements finalize_block
block_result = super().finalize_block(block) # type: ignore[safe-super]
block = block_result.block

nonce, mix_hash = pow.mine_pow_nonce(
Expand Down
4 changes: 2 additions & 2 deletions eth/vm/forks/berlin/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def max_fee_per_blob_gas(self) -> int:
)

@property
def blob_versioned_hashes(self) -> Hash32:
def blob_versioned_hashes(self) -> Sequence[Hash32]:
raise NotImplementedError(
"blob_versioned_hashes is not implemented until Cancun."
)
Expand Down Expand Up @@ -376,7 +376,7 @@ def max_fee_per_blob_gas(self) -> int:
)

@property
def blob_versioned_hashes(self) -> Hash32:
def blob_versioned_hashes(self) -> Sequence[Hash32]:
raise NotImplementedError(
"blob_versioned_hashes is not implemented until Cancun."
)
Expand Down
6 changes: 5 additions & 1 deletion eth/vm/forks/cancun/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from eth_typing import (
Address,
)

HISTORY_BUFFER_LENGTH = 8191
BEACON_ROOTS_ADDRESS = (
BEACON_ROOTS_ADDRESS = Address(
b'\x00\x0f=\xf6\xd72\x80~\xf11\x9f\xb7\xb8\xbb\x85"\xd0\xbe\xac\x02'
)
BEACON_ROOTS_CONTRACT_CODE = b"3s\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xfe\x14`MW` 6\x14`$W__\xfd[_5\x80\x15`IWb\x00\x1f\xff\x81\x06\x90\x81T\x14`<W__\xfd[b\x00\x1f\xff\x01T_R` _\xf3[__\xfd[b\x00\x1f\xffB\x06B\x81U_5\x90b\x00\x1f\xff\x01U\x00" # noqa: E501
Expand Down
2 changes: 1 addition & 1 deletion eth/vm/forks/london/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def max_fee_per_blob_gas(self) -> int:
)

@property
def blob_versioned_hashes(self) -> Hash32:
def blob_versioned_hashes(self) -> Sequence[Hash32]:
raise NotImplementedError(
"blob_versioned_hashes is not implemented until Cancun."
)
Expand Down
1 change: 1 addition & 0 deletions newsfragments/2197.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Adds running ``mypy`` locally with all deps installed, then updating typing as needed. Moves ``eth/tools/factories`` into ``tests`` as it is only ever used there.
6 changes: 3 additions & 3 deletions scripts/benchmark/checks/deploy_dos.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
from eth.rlp.blocks import (
BaseBlock,
)
from eth.tools.factories.transaction import (
new_transaction,
)
from scripts.benchmark._utils.chain_plumbing import (
FUNDED_ADDRESS,
FUNDED_ADDRESS_PRIVATE_KEY,
Expand All @@ -38,6 +35,9 @@
from scripts.benchmark._utils.reporting import (
DefaultStat,
)
from tests.tools.factories.transaction import (
new_transaction,
)

from .base_benchmark import (
BaseBenchmark,
Expand Down
6 changes: 3 additions & 3 deletions scripts/benchmark/checks/erc20_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
from eth.rlp.blocks import (
BaseBlock,
)
from eth.tools.factories.transaction import (
new_transaction,
)
from scripts.benchmark._utils.chain_plumbing import (
FUNDED_ADDRESS,
FUNDED_ADDRESS_PRIVATE_KEY,
Expand All @@ -39,6 +36,9 @@
from scripts.benchmark._utils.reporting import (
DefaultStat,
)
from tests.tools.factories.transaction import (
new_transaction,
)

from .base_benchmark import (
BaseBenchmark,
Expand Down
6 changes: 3 additions & 3 deletions scripts/benchmark/checks/simple_value_transfers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
from eth.rlp.blocks import (
BaseBlock,
)
from eth.tools.factories.transaction import (
new_transaction,
)
from scripts.benchmark._utils.address import (
generate_random_address,
)
Expand All @@ -32,6 +29,9 @@
from scripts.benchmark._utils.shellart import (
bold_yellow,
)
from tests.tools.factories.transaction import (
new_transaction,
)

from .base_benchmark import (
BaseBenchmark,
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"build>=0.9.0",
"bump_my_version>=0.19.0",
"ipython",
"mypy==1.10.0",
"pre-commit>=3.4.0",
"tox>=4.0.0",
"twine",
Expand Down
2 changes: 1 addition & 1 deletion tests/core/builder-tools/test_chain_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
mine_block,
mine_blocks,
)
from eth.tools.factories.transaction import (
from tests.tools.factories.transaction import (
new_transaction,
)

Expand Down
2 changes: 1 addition & 1 deletion tests/core/chain-object/test_build_block_incrementally.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from eth.chains.base import (
MiningChain,
)
from eth.tools.factories.transaction import (
from tests.tools.factories.transaction import (
new_transaction,
)

Expand Down
6 changes: 3 additions & 3 deletions tests/core/chain-object/test_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
from eth.exceptions import (
TransactionNotFound,
)
from eth.tools.factories.transaction import (
new_transaction,
)
from eth.vm.forks.frontier.blocks import (
FrontierBlock,
)
from tests.core.fixtures import (
valid_block_rlp,
)
from tests.tools.factories.transaction import (
new_transaction,
)


@pytest.fixture
Expand Down
6 changes: 3 additions & 3 deletions tests/core/chain-object/test_contract_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
OutOfGas,
Revert,
)
from eth.tools.factories.transaction import (
new_transaction,
)
from eth.vm.forks import (
ArrowGlacierVM,
BerlinVM,
Expand All @@ -32,6 +29,9 @@
SpuriousDragonVM,
TangerineWhistleVM,
)
from tests.tools.factories.transaction import (
new_transaction,
)


@pytest.fixture
Expand Down
6 changes: 3 additions & 3 deletions tests/core/chain-object/test_gas_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
from eth.estimators.gas import (
binary_gas_search_1000_tolerance,
)
from eth.tools.factories.transaction import (
new_transaction,
)
from eth.vm.forks import (
ArrowGlacierVM,
BerlinVM,
Expand All @@ -26,6 +23,9 @@
from tests.core.helpers import (
fill_block,
)
from tests.tools.factories.transaction import (
new_transaction,
)

ADDRESS_2 = b"\0" * 19 + b"\x02"

Expand Down
Loading