diff --git a/pyproject.toml b/pyproject.toml index 7fd634751..12bf8c23e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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.*", @@ -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 diff --git a/tests/consensus/test_consensus3.py b/tests/consensus/test_consensus3.py index 215766bf0..dad7ca70b 100644 --- a/tests/consensus/test_consensus3.py +++ b/tests/consensus/test_consensus3.py @@ -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) @@ -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) diff --git a/tests/event/test_event_simulation_responses.py b/tests/event/test_event_simulation_responses.py index 2bc628088..50557296c 100644 --- a/tests/event/test_event_simulation_responses.py +++ b/tests/event/test_event_simulation_responses.py @@ -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) @@ -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) diff --git a/tests/feature_activation/test_feature_simulation.py b/tests/feature_activation/test_feature_simulation.py index efb80f83b..53e02f1b9 100644 --- a/tests/feature_activation/test_feature_simulation.py +++ b/tests/feature_activation/test_feature_simulation.py @@ -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): @@ -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 diff --git a/tests/feature_activation/test_mining_simulation.py b/tests/feature_activation/test_mining_simulation.py index cb306a693..f65056ff1 100644 --- a/tests/feature_activation/test_mining_simulation.py +++ b/tests/feature_activation/test_mining_simulation.py @@ -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) @@ -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 diff --git a/tests/feature_activation/test_settings.py b/tests/feature_activation/test_settings.py index 92b3f374e..b2c7eac9a 100644 --- a/tests/feature_activation/test_settings.py +++ b/tests/feature_activation/test_settings.py @@ -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 @@ -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] @@ -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] diff --git a/tests/p2p/test_get_best_blockchain.py b/tests/p2p/test_get_best_blockchain.py index f64b64a36..ff0d95149 100644 --- a/tests/p2p/test_get_best_blockchain.py +++ b/tests/p2p/test_get_best_blockchain.py @@ -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() diff --git a/tests/p2p/test_protocol.py b/tests/p2p/test_protocol.py index f0b0e95e7..a834f9e20 100644 --- a/tests/p2p/test_protocol.py +++ b/tests/p2p/test_protocol.py @@ -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 = [] diff --git a/tests/p2p/test_split_brain.py b/tests/p2p/test_split_brain.py index efd0f0b10..3a2352853 100644 --- a/tests/p2p/test_split_brain.py +++ b/tests/p2p/test_split_brain.py @@ -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. diff --git a/tests/p2p/test_sync.py b/tests/p2p/test_sync.py index bd75e52a9..69bd417e4 100644 --- a/tests/p2p/test_sync.py +++ b/tests/p2p/test_sync.py @@ -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 @@ -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) diff --git a/tests/p2p/test_sync_mempool.py b/tests/p2p/test_sync_mempool.py index d094a1af7..dff3c27bf 100644 --- a/tests/p2p/test_sync_mempool.py +++ b/tests/p2p/test_sync_mempool.py @@ -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 @@ -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) diff --git a/tests/pubsub/test_pubsub2.py b/tests/pubsub/test_pubsub2.py index faaf9c758..d0ede02ac 100644 --- a/tests/pubsub/test_pubsub2.py +++ b/tests/pubsub/test_pubsub2.py @@ -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 @@ -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) diff --git a/tests/resources/healthcheck/test_healthcheck.py b/tests/resources/healthcheck/test_healthcheck.py index c616d3a03..5beff1f24 100644 --- a/tests/resources/healthcheck/test_healthcheck.py +++ b/tests/resources/healthcheck/test_healthcheck.py @@ -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): diff --git a/tests/unittest.py b/tests/unittest.py index 38f6c1c08..939e32853 100644 --- a/tests/unittest.py +++ b/tests/unittest.py @@ -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 @@ -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: diff --git a/tests/utils.py b/tests/utils.py index bffffd613..7b9518cf1 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -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(