From 0d3deb68e9f9df14da822f6bd57b65052478c86f Mon Sep 17 00:00:00 2001 From: Gabriel Levcovitz Date: Wed, 13 Mar 2024 00:47:31 -0300 Subject: [PATCH] refactor(mypy): add stricter rules to specific modules --- hathor/builder/builder.py | 3 +- hathor/cli/mining.py | 3 +- hathor/consensus/block_consensus.py | 4 +- hathor/consensus/transaction_consensus.py | 4 +- hathor/event/model/base_event.py | 6 +- hathor/event/resources/event.py | 3 +- hathor/event/websocket/protocol.py | 22 +++----- hathor/event/websocket/request.py | 4 +- hathor/sysctl/sysctl.py | 8 ++- hathor/types.py | 16 +++--- poetry.lock | 68 +++++++++++------------ pyproject.toml | 27 ++++++++- tests/sysctl/test_sysctl.py | 2 +- 13 files changed, 97 insertions(+), 73 deletions(-) diff --git a/hathor/builder/builder.py b/hathor/builder/builder.py index b5cb7a0c0..7636aa2b3 100644 --- a/hathor/builder/builder.py +++ b/hathor/builder/builder.py @@ -47,7 +47,8 @@ TransactionStorage, ) from hathor.util import Random, get_environment_info, not_none -from hathor.verification.verification_service import VerificationService, VertexVerifiers +from hathor.verification.verification_service import VerificationService +from hathor.verification.vertex_verifiers import VertexVerifiers from hathor.wallet import BaseWallet, Wallet logger = get_logger() diff --git a/hathor/cli/mining.py b/hathor/cli/mining.py index 491aff1e4..38c08adde 100644 --- a/hathor/cli/mining.py +++ b/hathor/cli/mining.py @@ -139,7 +139,8 @@ def execute(args: Namespace) -> None: from hathor.conf.get_settings import get_global_settings from hathor.daa import DifficultyAdjustmentAlgorithm - from hathor.verification.verification_service import VerificationService, VertexVerifiers + from hathor.verification.verification_service import VerificationService + from hathor.verification.vertex_verifiers import VertexVerifiers settings = get_global_settings() daa = DifficultyAdjustmentAlgorithm(settings=settings) verifiers = VertexVerifiers.create_defaults(settings=settings, daa=daa, feature_service=Mock()) diff --git a/hathor/consensus/block_consensus.py b/hathor/consensus/block_consensus.py index 515b96a07..644a53238 100644 --- a/hathor/consensus/block_consensus.py +++ b/hathor/consensus/block_consensus.py @@ -13,7 +13,7 @@ # limitations under the License. from itertools import chain -from typing import TYPE_CHECKING, Iterable, Optional, cast +from typing import TYPE_CHECKING, Any, Iterable, Optional, cast from structlog import get_logger @@ -39,7 +39,7 @@ def __init__(self, context: 'ConsensusAlgorithmContext') -> None: self.context = context @classproperty - def log(cls): + def log(cls) -> Any: """ This is a workaround because of a bug on structlog (or abc). See: https://github.com/hynek/structlog/issues/229 diff --git a/hathor/consensus/transaction_consensus.py b/hathor/consensus/transaction_consensus.py index 17a32202d..286a2534d 100644 --- a/hathor/consensus/transaction_consensus.py +++ b/hathor/consensus/transaction_consensus.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import TYPE_CHECKING, Iterable, cast +from typing import TYPE_CHECKING, Any, Iterable, cast from structlog import get_logger @@ -38,7 +38,7 @@ def __init__(self, context: 'ConsensusAlgorithmContext') -> None: self.context = context @classproperty - def log(cls): + def log(cls) -> Any: """ This is a workaround because of a bug on structlog (or abc). See: https://github.com/hynek/structlog/issues/229 diff --git a/hathor/event/model/base_event.py b/hathor/event/model/base_event.py index 8f15fca88..e59db1f7c 100644 --- a/hathor/event/model/base_event.py +++ b/hathor/event/model/base_event.py @@ -12,11 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Optional +from typing import Any, Optional from pydantic import NonNegativeInt, validator -from hathor.event.model.event_data import EventData +from hathor.event.model.event_data import BaseEventData, EventData from hathor.event.model.event_type import EventType from hathor.pubsub import EventArguments from hathor.utils.pydantic import BaseModel @@ -58,7 +58,7 @@ def from_event_arguments( ) @validator('data') - def data_type_must_match_event_type(cls, v, values): + def data_type_must_match_event_type(cls, v: BaseEventData, values: dict[str, Any]) -> BaseEventData: event_type = EventType(values['type']) expected_data_type = event_type.data_type() diff --git a/hathor/event/resources/event.py b/hathor/event/resources/event.py index febc2bb62..87e7ada9b 100644 --- a/hathor/event/resources/event.py +++ b/hathor/event/resources/event.py @@ -16,6 +16,7 @@ from typing import Optional from pydantic import Field, NonNegativeInt +from twisted.web.http import Request from hathor.api_util import Resource, set_cors from hathor.cli.openapi_files.register import register_resource @@ -35,7 +36,7 @@ def __init__(self, event_manager: Optional[EventManager]): super().__init__() self.event_manager = event_manager - def render_GET(self, request): + def render_GET(self, request: Request) -> bytes: request.setHeader(b'content-type', b'application/json; charset=utf-8') set_cors(request, 'GET') diff --git a/hathor/event/websocket/protocol.py b/hathor/event/websocket/protocol.py index 102617546..c8da7e1f6 100644 --- a/hathor/event/websocket/protocol.py +++ b/hathor/event/websocket/protocol.py @@ -12,13 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import TYPE_CHECKING, Callable, Optional +from typing import TYPE_CHECKING, Optional from autobahn.exception import Disconnected from autobahn.twisted.websocket import WebSocketServerProtocol from autobahn.websocket import ConnectionRequest from pydantic import ValidationError from structlog import get_logger +from typing_extensions import assert_never from hathor.event.websocket.request import AckRequest, Request, RequestWrapper, StartStreamRequest, StopStreamRequest from hathor.event.websocket.response import EventResponse, InvalidRequestResponse, InvalidRequestType, Response @@ -50,7 +51,7 @@ class EventWebsocketProtocol(WebSocketServerProtocol): # Whether the stream is enabled or not. _stream_is_active: bool = False - def __init__(self): + def __init__(self) -> None: super().__init__() self.log = logger.new() @@ -102,18 +103,11 @@ def onMessage(self, payload: bytes, isBinary: bool) -> None: def _handle_request(self, request: Request) -> None: """Handles a request message according to its type.""" - # This could be a pattern match in Python 3.10 - request_type = type(request) - handlers: dict[type, Callable] = { - StartStreamRequest: self._handle_start_stream_request, - AckRequest: self._handle_ack_request, - StopStreamRequest: lambda _: self._handle_stop_stream_request() - } - handle_fn = handlers.get(request_type) - - assert handle_fn is not None, f'cannot handle request of unknown type "{request_type}"' - - handle_fn(request) + match request: + case StartStreamRequest(): self._handle_start_stream_request(request) + case AckRequest(): self._handle_ack_request(request) + case StopStreamRequest(): self._handle_stop_stream_request() + case _: assert_never(request) def _handle_start_stream_request(self, request: StartStreamRequest) -> None: """ diff --git a/hathor/event/websocket/request.py b/hathor/event/websocket/request.py index 446c62840..64446887d 100644 --- a/hathor/event/websocket/request.py +++ b/hathor/event/websocket/request.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Annotated, Literal, Optional, Union +from typing import Annotated, Literal, Optional from pydantic import Field, NonNegativeInt @@ -54,7 +54,7 @@ class StopStreamRequest(BaseModel): type: Literal['STOP_STREAM'] -Request = Annotated[Union[StartStreamRequest, AckRequest, StopStreamRequest], Field(discriminator='type')] +Request = Annotated[StartStreamRequest | AckRequest | StopStreamRequest, Field(discriminator='type')] class RequestWrapper(BaseModel): diff --git a/hathor/sysctl/sysctl.py b/hathor/sysctl/sysctl.py index 79bf3c5b0..28339365d 100644 --- a/hathor/sysctl/sysctl.py +++ b/hathor/sysctl/sysctl.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import Any, Callable, Iterator, NamedTuple, Optional +from typing import Any, Callable, Iterator, NamedTuple, Optional, ParamSpec, TypeVar from pydantic import validate_arguments from structlog import get_logger @@ -21,16 +21,18 @@ Getter = Callable[[], Any] Setter = Callable[..., None] +P = ParamSpec('P') +T = TypeVar('T') logger = get_logger() -def signal_handler_safe(f): +def signal_handler_safe(f: Callable[P, T]) -> Callable[P, T]: """Decorator to mark methods as signal handler safe. It should only be used if that method can be executed during a signal handling. Notice that a signal handling can pause the code execution at any point and the execution will resume after.""" - f._signal_handler_safe = True + f._signal_handler_safe = True # type: ignore[attr-defined] return f diff --git a/hathor/types.py b/hathor/types.py index f035a8c80..40ad6dead 100644 --- a/hathor/types.py +++ b/hathor/types.py @@ -12,13 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. +from typing import TypeAlias + # XXX There is a lot of refactor to be done before we can use `NewType`. # So, let's skip using NewType until everything is refactored. -VertexId = bytes # NewType('TxId', bytes) -Address = bytes # NewType('Address', bytes) -AddressB58 = str -TxOutputScript = bytes # NewType('TxOutputScript', bytes) -Timestamp = int # NewType('Timestamp', int) -TokenUid = VertexId # NewType('TokenUid', VertexId) -Amount = int # NewType('Amount', int) +VertexId: TypeAlias = bytes # NewType('TxId', bytes) +Address: TypeAlias = bytes # NewType('Address', bytes) +AddressB58: TypeAlias = str +TxOutputScript: TypeAlias = bytes # NewType('TxOutputScript', bytes) +Timestamp: TypeAlias = int # NewType('Timestamp', int) +TokenUid: TypeAlias = VertexId # NewType('TokenUid', VertexId) +Amount: TypeAlias = int # NewType('Amount', int) diff --git a/poetry.lock b/poetry.lock index 475743517..163ae443e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. [[package]] name = "aiohttp" @@ -1061,38 +1061,38 @@ files = [ [[package]] name = "mypy" -version = "1.8.0" +version = "1.9.0" description = "Optional static typing for Python" optional = false python-versions = ">=3.8" files = [ - {file = "mypy-1.8.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:485a8942f671120f76afffff70f259e1cd0f0cfe08f81c05d8816d958d4577d3"}, - {file = "mypy-1.8.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:df9824ac11deaf007443e7ed2a4a26bebff98d2bc43c6da21b2b64185da011c4"}, - {file = "mypy-1.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2afecd6354bbfb6e0160f4e4ad9ba6e4e003b767dd80d85516e71f2e955ab50d"}, - {file = "mypy-1.8.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:8963b83d53ee733a6e4196954502b33567ad07dfd74851f32be18eb932fb1cb9"}, - {file = "mypy-1.8.0-cp310-cp310-win_amd64.whl", hash = "sha256:e46f44b54ebddbeedbd3d5b289a893219065ef805d95094d16a0af6630f5d410"}, - {file = "mypy-1.8.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:855fe27b80375e5c5878492f0729540db47b186509c98dae341254c8f45f42ae"}, - {file = "mypy-1.8.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:4c886c6cce2d070bd7df4ec4a05a13ee20c0aa60cb587e8d1265b6c03cf91da3"}, - {file = "mypy-1.8.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d19c413b3c07cbecf1f991e2221746b0d2a9410b59cb3f4fb9557f0365a1a817"}, - {file = "mypy-1.8.0-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:9261ed810972061388918c83c3f5cd46079d875026ba97380f3e3978a72f503d"}, - {file = "mypy-1.8.0-cp311-cp311-win_amd64.whl", hash = "sha256:51720c776d148bad2372ca21ca29256ed483aa9a4cdefefcef49006dff2a6835"}, - {file = "mypy-1.8.0-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:52825b01f5c4c1c4eb0db253ec09c7aa17e1a7304d247c48b6f3599ef40db8bd"}, - {file = "mypy-1.8.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:f5ac9a4eeb1ec0f1ccdc6f326bcdb464de5f80eb07fb38b5ddd7b0de6bc61e55"}, - {file = "mypy-1.8.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:afe3fe972c645b4632c563d3f3eff1cdca2fa058f730df2b93a35e3b0c538218"}, - {file = "mypy-1.8.0-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:42c6680d256ab35637ef88891c6bd02514ccb7e1122133ac96055ff458f93fc3"}, - {file = "mypy-1.8.0-cp312-cp312-win_amd64.whl", hash = "sha256:720a5ca70e136b675af3af63db533c1c8c9181314d207568bbe79051f122669e"}, - {file = "mypy-1.8.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:028cf9f2cae89e202d7b6593cd98db6759379f17a319b5faf4f9978d7084cdc6"}, - {file = "mypy-1.8.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:4e6d97288757e1ddba10dd9549ac27982e3e74a49d8d0179fc14d4365c7add66"}, - {file = "mypy-1.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7f1478736fcebb90f97e40aff11a5f253af890c845ee0c850fe80aa060a267c6"}, - {file = "mypy-1.8.0-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:42419861b43e6962a649068a61f4a4839205a3ef525b858377a960b9e2de6e0d"}, - {file = "mypy-1.8.0-cp38-cp38-win_amd64.whl", hash = "sha256:2b5b6c721bd4aabaadead3a5e6fa85c11c6c795e0c81a7215776ef8afc66de02"}, - {file = "mypy-1.8.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:5c1538c38584029352878a0466f03a8ee7547d7bd9f641f57a0f3017a7c905b8"}, - {file = "mypy-1.8.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:4ef4be7baf08a203170f29e89d79064463b7fc7a0908b9d0d5114e8009c3a259"}, - {file = "mypy-1.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7178def594014aa6c35a8ff411cf37d682f428b3b5617ca79029d8ae72f5402b"}, - {file = "mypy-1.8.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:ab3c84fa13c04aeeeabb2a7f67a25ef5d77ac9d6486ff33ded762ef353aa5592"}, - {file = "mypy-1.8.0-cp39-cp39-win_amd64.whl", hash = "sha256:99b00bc72855812a60d253420d8a2eae839b0afa4938f09f4d2aa9bb4654263a"}, - {file = "mypy-1.8.0-py3-none-any.whl", hash = "sha256:538fd81bb5e430cc1381a443971c0475582ff9f434c16cd46d2c66763ce85d9d"}, - {file = "mypy-1.8.0.tar.gz", hash = "sha256:6ff8b244d7085a0b425b56d327b480c3b29cafbd2eff27316a004f9a7391ae07"}, + {file = "mypy-1.9.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:f8a67616990062232ee4c3952f41c779afac41405806042a8126fe96e098419f"}, + {file = "mypy-1.9.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:d357423fa57a489e8c47b7c85dfb96698caba13d66e086b412298a1a0ea3b0ed"}, + {file = "mypy-1.9.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:49c87c15aed320de9b438ae7b00c1ac91cd393c1b854c2ce538e2a72d55df150"}, + {file = "mypy-1.9.0-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:48533cdd345c3c2e5ef48ba3b0d3880b257b423e7995dada04248725c6f77374"}, + {file = "mypy-1.9.0-cp310-cp310-win_amd64.whl", hash = "sha256:4d3dbd346cfec7cb98e6cbb6e0f3c23618af826316188d587d1c1bc34f0ede03"}, + {file = "mypy-1.9.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:653265f9a2784db65bfca694d1edd23093ce49740b2244cde583aeb134c008f3"}, + {file = "mypy-1.9.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:3a3c007ff3ee90f69cf0a15cbcdf0995749569b86b6d2f327af01fd1b8aee9dc"}, + {file = "mypy-1.9.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:2418488264eb41f69cc64a69a745fad4a8f86649af4b1041a4c64ee61fc61129"}, + {file = "mypy-1.9.0-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:68edad3dc7d70f2f17ae4c6c1b9471a56138ca22722487eebacfd1eb5321d612"}, + {file = "mypy-1.9.0-cp311-cp311-win_amd64.whl", hash = "sha256:85ca5fcc24f0b4aeedc1d02f93707bccc04733f21d41c88334c5482219b1ccb3"}, + {file = "mypy-1.9.0-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:aceb1db093b04db5cd390821464504111b8ec3e351eb85afd1433490163d60cd"}, + {file = "mypy-1.9.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:0235391f1c6f6ce487b23b9dbd1327b4ec33bb93934aa986efe8a9563d9349e6"}, + {file = "mypy-1.9.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:d4d5ddc13421ba3e2e082a6c2d74c2ddb3979c39b582dacd53dd5d9431237185"}, + {file = "mypy-1.9.0-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:190da1ee69b427d7efa8aa0d5e5ccd67a4fb04038c380237a0d96829cb157913"}, + {file = "mypy-1.9.0-cp312-cp312-win_amd64.whl", hash = "sha256:fe28657de3bfec596bbeef01cb219833ad9d38dd5393fc649f4b366840baefe6"}, + {file = "mypy-1.9.0-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:e54396d70be04b34f31d2edf3362c1edd023246c82f1730bbf8768c28db5361b"}, + {file = "mypy-1.9.0-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:5e6061f44f2313b94f920e91b204ec600982961e07a17e0f6cd83371cb23f5c2"}, + {file = "mypy-1.9.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:81a10926e5473c5fc3da8abb04119a1f5811a236dc3a38d92015cb1e6ba4cb9e"}, + {file = "mypy-1.9.0-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:b685154e22e4e9199fc95f298661deea28aaede5ae16ccc8cbb1045e716b3e04"}, + {file = "mypy-1.9.0-cp38-cp38-win_amd64.whl", hash = "sha256:5d741d3fc7c4da608764073089e5f58ef6352bedc223ff58f2f038c2c4698a89"}, + {file = "mypy-1.9.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:587ce887f75dd9700252a3abbc9c97bbe165a4a630597845c61279cf32dfbf02"}, + {file = "mypy-1.9.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:f88566144752999351725ac623471661c9d1cd8caa0134ff98cceeea181789f4"}, + {file = "mypy-1.9.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:61758fabd58ce4b0720ae1e2fea5cfd4431591d6d590b197775329264f86311d"}, + {file = "mypy-1.9.0-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:e49499be624dead83927e70c756970a0bc8240e9f769389cdf5714b0784ca6bf"}, + {file = "mypy-1.9.0-cp39-cp39-win_amd64.whl", hash = "sha256:571741dc4194b4f82d344b15e8837e8c5fcc462d66d076748142327626a1b6e9"}, + {file = "mypy-1.9.0-py3-none-any.whl", hash = "sha256:a260627a570559181a9ea5de61ac6297aa5af202f06fd7ab093ce74e7181e43e"}, + {file = "mypy-1.9.0.tar.gz", hash = "sha256:3cc5da0127e6a478cddd906068496a97a7618a21ce9b54bde5bf7e539c7af974"}, ] [package.dependencies] @@ -1119,17 +1119,17 @@ files = [ [[package]] name = "mypy-zope" -version = "1.0.3" +version = "1.0.4" description = "Plugin for mypy to support zope interfaces" optional = false python-versions = "*" files = [ - {file = "mypy-zope-1.0.3.tar.gz", hash = "sha256:149081bd2754d947747baefac569bb1c2bc127b4a2cc1fa505492336946bb3b4"}, - {file = "mypy_zope-1.0.3-py3-none-any.whl", hash = "sha256:7a30ce1a2589173f0be66662c9a9179f75737afc40e4104df4c76fb5a8421c14"}, + {file = "mypy-zope-1.0.4.tar.gz", hash = "sha256:a9569e73ae85a65247787d98590fa6d4290e76f26aabe035d1c3e94a0b9ab6ee"}, + {file = "mypy_zope-1.0.4-py3-none-any.whl", hash = "sha256:c7298f93963a84f2b145c2b5cc98709fc2a5be4adf54bfe23fa7fdd8fd19c975"}, ] [package.dependencies] -mypy = ">=1.0.0,<1.9.0" +mypy = ">=1.0.0,<1.10.0" "zope.interface" = "*" "zope.schema" = "*" @@ -2490,4 +2490,4 @@ sentry = ["sentry-sdk", "structlog-sentry"] [metadata] lock-version = "2.0" python-versions = ">=3.10,<4" -content-hash = "cce7b9832ae2d13cc56fb572af82face7a824307ddd6953387737a27d6e7088a" +content-hash = "1eed0fc6c02c4ddb7b4a6634d6c5ba4873ce5a82c6b3d4197ca88b4644474c53" diff --git a/pyproject.toml b/pyproject.toml index e27cfb609..7a4d67554 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,8 +38,8 @@ hathor-cli = 'hathor.cli.main:main' [tool.poetry.dev-dependencies] flake8 = "~6.1.0" isort = {version = "~5.12.0", extras = ["colors"]} -mypy = {version = "^1.5.1", markers = "implementation_name == 'cpython'"} -mypy-zope = {version = "^1.0.1", markers = "implementation_name == 'cpython'"} +mypy = {version = "^1.9.0", markers = "implementation_name == 'cpython'"} +mypy-zope = {version = "^1.0.4", markers = "implementation_name == 'cpython'"} pytest = "~7.4.3" pytest-cov = "~4.1.0" flaky = "~3.7.0" @@ -97,6 +97,8 @@ multi_line_output = 3 pretty = true disallow_incomplete_defs = true no_implicit_optional = true +extra_checks = true +disallow_untyped_decorators = true warn_redundant_casts = true warn_unused_configs = true warn_unused_ignores = true @@ -131,6 +133,27 @@ module = [ ] ignore_missing_imports = true +# This override enables stricter rules for some specific modules. +# Currently, we have only two options from strict-mode that are disabled, but we have to opt-in instead of opt-out +# because setting strict=true doesn't work for module-level settings. +# Reference: https://mypy.readthedocs.io/en/stable/existing_code.html#introduce-stricter-options +[[tool.mypy.overrides]] +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 + [tool.pydantic-mypy] init_typed = true init_forbid_extra = true diff --git a/tests/sysctl/test_sysctl.py b/tests/sysctl/test_sysctl.py index d629d4230..01d8b46cc 100644 --- a/tests/sysctl/test_sysctl.py +++ b/tests/sysctl/test_sysctl.py @@ -13,7 +13,7 @@ class SysctlTest(unittest.TestCase): # We need this patch because pydantic.validate_arguments fails when it gets a mock function. - @patch('hathor.sysctl.sysctl.validate_arguments', new=lambda x: x) + @patch('hathor.sysctl.sysctl.validate_arguments', new=lambda x: x) # type: ignore def setUp(self) -> None: super().setUp()