Skip to content

Commit

Permalink
refactor(mypy): unify hathor and test modules strictness
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco committed Mar 22, 2024
1 parent ded7fef commit 5e00ff5
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 39 deletions.
24 changes: 9 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,7 @@ module = [
"hathor.consensus.*",
"hathor.feature_activation.*",
"hathor.event.*",
"hathor.verification.*"
]
strict_equality = true
strict_concatenate = true
check_untyped_defs = true
disallow_any_generics = true
disallow_untyped_defs = true
no_implicit_reexport = true
warn_return_any = true
# disallow_subclassing_any = true
# disallow_untyped_calls = true

# We also override to add disallow_untyped_defs for some specific test modules
[[tool.mypy.overrides]]
module = [
"hathor.verification.*",
"tests.consensus.*",
"tests.crypto.*",
"tests.event.*",
Expand All @@ -168,7 +154,15 @@ module = [
"tests.unittest",
"tests.utils",
]
strict_equality = true
strict_concatenate = true
check_untyped_defs = true
disallow_any_generics = true
disallow_untyped_defs = true
no_implicit_reexport = true
warn_return_any = true
# disallow_subclassing_any = true
# disallow_untyped_calls = true

[tool.pydantic-mypy]
init_typed = true
Expand Down
4 changes: 2 additions & 2 deletions tests/consensus/test_consensus3.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def do_step(tx_fund: Transaction) -> Transaction:
outputs = []
outputs.append(WalletOutputInfo(decode_address(addr), 1, None))
outputs.append(WalletOutputInfo(decode_address(addr), 2*tx_fund.outputs[1].value, None))
tx5 = manager.wallet.prepare_transaction(Transaction, inputs, outputs, tx2.timestamp+1)
tx5: Transaction = manager.wallet.prepare_transaction(Transaction, inputs, outputs, tx2.timestamp+1)
tx5.weight = tx3.weight - tx1.weight + 0.1
tx5.parents = [tx2.hash, tx4.hash]
manager.cpu_mining_service.resolve(tx5)
Expand Down Expand Up @@ -174,7 +174,7 @@ def do_step(tx_fund: Transaction) -> Transaction:
outputs.append(WalletOutputInfo(decode_address(addr), 1, None))
outputs.append(WalletOutputInfo(decode_address(addr), 1, None))
outputs.append(WalletOutputInfo(decode_address(addr), 2*tx_fund.outputs[2].value, None))
tx5 = manager.wallet.prepare_transaction(Transaction, inputs, outputs, tx4.timestamp+1)
tx5: Transaction = manager.wallet.prepare_transaction(Transaction, inputs, outputs, tx4.timestamp+1)
tx5.weight = 1
tx5.parents = manager.get_new_tx_parents(tx5.timestamp)
manager.cpu_mining_service.resolve(tx5)
Expand Down
5 changes: 3 additions & 2 deletions tests/event/test_event_simulation_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def test_restart_with_ack_too_small(self) -> None:
# get response
response = self._get_error_response()

assert response.type == InvalidRequestType.ACK_TOO_SMALL.value
assert str(response.type) == InvalidRequestType.ACK_TOO_SMALL.value

def test_multiple_interactions(self) -> None:
miner = self.simulator.create_miner(self.manager, hashpower=1e6)
Expand Down Expand Up @@ -333,7 +333,8 @@ def test_multiple_interactions(self) -> None:
# get response
response = self._get_error_response()

assert response.type == InvalidRequestType.ACK_TOO_SMALL.value # ACK too small because we've already sent it
# ACK too small because we've already sent it
assert str(response.type) == InvalidRequestType.ACK_TOO_SMALL.value

# new ack
ack = AckRequest(type='ACK', window_size=4, ack_event_id=5)
Expand Down
5 changes: 3 additions & 2 deletions tests/feature_activation/test_feature_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
from hathor.feature_activation.resources.feature import FeatureResource
from hathor.feature_activation.settings import Settings as FeatureSettings
from hathor.simulator import FakeConnection
from hathor.simulator.utils import add_new_blocks
from hathor.transaction.exceptions import BlockMustSignalError
from hathor.util import not_none
from tests import unittest
from tests.resources.base_resource import StubSite
from tests.simulation.base import SimulatorTestCase
from tests.utils import HAS_ROCKSDB, add_new_blocks
from tests.utils import HAS_ROCKSDB


class BaseFeatureSimulationTest(SimulatorTestCase):
Expand All @@ -42,7 +43,7 @@ def get_simulator_builder(self) -> Builder:
def _get_result(web_client: StubSite) -> dict[str, Any]:
"""Returns the feature activation api response."""
response = web_client.get('feature')
result = response.result.json_value()
result: dict[str, Any] = response.result.json_value()

del result['block_hash'] # we don't assert the block hash because it's not always the same

Expand Down
8 changes: 5 additions & 3 deletions tests/feature_activation/test_mining_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ def test_signal_bits_in_mining(self) -> None:

def _get_signal_bits_from_get_block_template(self, web_client: StubSite) -> int:
result = self._get_result(web_client)
return result['signal_bits']
signal_bits: int = result['signal_bits']
return signal_bits

def _get_signal_bits_from_mining(self, web_client: StubSite) -> int:
result = self._get_result(web_client)
Expand All @@ -153,13 +154,14 @@ def _get_signal_bits_from_mining(self, web_client: StubSite) -> int:
@staticmethod
def _get_result(web_client: StubSite) -> dict[str, Any]:
response = web_client.get('')
return response.result.json_value()
result: dict[str, Any] = response.result.json_value()
return result

def _get_last_ws_signal_bits(self, transport: StringTransport) -> int:
messages = self._get_transport_messages(transport)
assert len(messages) > 0
last_message = messages[-1]
signal_bits = last_message['params'][0]['signal_bits']
signal_bits: int = last_message['params'][0]['signal_bits']

return signal_bits

Expand Down
5 changes: 3 additions & 2 deletions tests/feature_activation/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any

import pytest
from pydantic import ValidationError
Expand Down Expand Up @@ -56,7 +57,7 @@
)
]
)
def test_valid_settings(features: dict) -> None:
def test_valid_settings(features: dict[str, Any]) -> None:
data = dict(features=features)
FeatureSettings(**data) # type: ignore[arg-type]

Expand Down Expand Up @@ -114,7 +115,7 @@ def test_valid_settings(features: dict) -> None:
)
]
)
def test_conflicting_bits(features: list[dict]) -> None:
def test_conflicting_bits(features: list[dict[str, Any]]) -> None:
with pytest.raises(ValidationError) as e:
data = dict(features=features)
FeatureSettings(**data) # type: ignore[arg-type]
Expand Down
2 changes: 1 addition & 1 deletion tests/p2p/test_get_best_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _send_cmd(self, proto: Protocol, cmd: str, payload: str | None = None) -> No
else:
line = '{} {}\r\n'.format(cmd, payload)

return proto.dataReceived(line.encode('utf-8'))
proto.dataReceived(line.encode('utf-8'))

def test_get_best_blockchain(self) -> None:
manager1 = self.create_peer()
Expand Down
2 changes: 1 addition & 1 deletion tests/p2p/test_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def _send_cmd(self, proto: Protocol, cmd: str, payload: str | None = None) -> No
else:
line = '{} {}\r\n'.format(cmd, payload)

return proto.dataReceived(line.encode('utf-8'))
proto.dataReceived(line.encode('utf-8'))

def _check_result_only_cmd(self, result: bytes, expected_cmd: bytes) -> None:
cmd_list = []
Expand Down
2 changes: 1 addition & 1 deletion tests/p2p/test_split_brain.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create_peer(self, network: str, unlock_wallet: bool = True) -> HathorManager
wallet = HDWallet(gap_limit=2)
wallet._manually_initialize()

manager = super().create_peer(network, wallet=wallet)
manager: HathorManager = super().create_peer(network, wallet=wallet)
manager.daa.TEST_MODE = TestMode.TEST_ALL_WEIGHT
# manager.avg_time_between_blocks = 64 # FIXME: This property is not defined. Fix this test.

Expand Down
6 changes: 4 additions & 2 deletions tests/p2p/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def _add_new_tx(self, address: str, value: int) -> Transaction:
outputs.append(
WalletOutputInfo(address=decode_address(address), value=int(value), timelock=None))

tx = self.manager1.wallet.prepare_transaction_compute_inputs(Transaction, outputs, self.manager1.tx_storage)
tx: Transaction = self.manager1.wallet.prepare_transaction_compute_inputs(
Transaction, outputs, self.manager1.tx_storage
)
tx.timestamp = int(self.clock.seconds())
tx.storage = self.manager1.tx_storage
tx.weight = 10
Expand All @@ -57,7 +59,7 @@ def _add_new_transactions(self, num_txs: int) -> list[Transaction]:
return txs

def _add_new_block(self, propagate: bool = True) -> Block:
block = self.manager1.generate_mining_block()
block: Block = self.manager1.generate_mining_block()
self.assertTrue(self.manager1.cpu_mining_service.resolve(block))
self.manager1.verification_service.verify(block)
self.manager1.on_new_tx(block, propagate_to_peers=propagate)
Expand Down
6 changes: 4 additions & 2 deletions tests/p2p/test_sync_mempool.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def _add_new_tx(self, address: str, value: int) -> Transaction:
outputs.append(
WalletOutputInfo(address=decode_address(address), value=int(value), timelock=None))

tx = self.manager1.wallet.prepare_transaction_compute_inputs(Transaction, outputs, self.manager1.tx_storage)
tx: Transaction = self.manager1.wallet.prepare_transaction_compute_inputs(
Transaction, outputs, self.manager1.tx_storage
)
tx.timestamp = int(self.clock.seconds())
tx.storage = self.manager1.tx_storage
tx.weight = 10
Expand All @@ -49,7 +51,7 @@ def _add_new_transactions(self, num_txs: int) -> list[Transaction]:
return txs

def _add_new_block(self, propagate: bool = True) -> Block:
block = self.manager1.generate_mining_block()
block: Block = self.manager1.generate_mining_block()
self.assertTrue(self.manager1.cpu_mining_service.resolve(block))
self.manager1.verification_service.verify(block)
self.manager1.on_new_tx(block, propagate_to_peers=propagate)
Expand Down
4 changes: 2 additions & 2 deletions tests/pubsub/test_pubsub2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Callable
from typing import Any, Callable
from unittest.mock import Mock, patch

import pytest
Expand Down Expand Up @@ -78,7 +78,7 @@ def test_memory_reactor_clock_running_with_threading() -> None:
pubsub = PubSubManager(reactor)
handler = Mock()

def fake_call_from_thread(f: Callable) -> None:
def fake_call_from_thread(f: Callable[..., Any]) -> None:
reactor.callLater(0, f)

call_from_thread_mock = Mock(side_effect=fake_call_from_thread)
Expand Down
2 changes: 1 addition & 1 deletion tests/resources/healthcheck/test_healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from hathor.healthcheck.resources.healthcheck import HealthcheckResource
from hathor.manager import HathorManager
from hathor.simulator import FakeConnection
from hathor.simulator.utils import add_new_blocks
from tests import unittest
from tests.resources.base_resource import StubSite, _BaseResourceTest
from tests.utils import add_new_blocks


class BaseHealthcheckReadinessTest(_BaseResourceTest._ResourceTest):
Expand Down
4 changes: 2 additions & 2 deletions tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import shutil
import tempfile
import time
from typing import Callable, Collection, Iterable, Iterator, Optional
from typing import Any, Callable, Collection, Iterable, Iterator, Optional
from unittest import main as ut_main

from structlog import get_logger
Expand Down Expand Up @@ -121,7 +121,7 @@ def setUp(self) -> None:
self.seed = secrets.randbits(64) if self.seed_config is None else self.seed_config
self.log.info('set seed', seed=self.seed)
self.rng = Random(self.seed)
self._pending_cleanups: list[Callable] = []
self._pending_cleanups: list[Callable[..., Any]] = []
self._settings = get_global_settings()

def tearDown(self) -> None:
Expand Down
3 changes: 2 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ def request_server(
response = requests.put(url, json=data)
else:
raise ValueError('Unsuported method')
return response.json()
json_response: dict[str, Any] = response.json()
return json_response


def execute_mining(
Expand Down

0 comments on commit 5e00ff5

Please sign in to comment.