Skip to content

Commit

Permalink
chore(custom-checks): add check to prevent circular imports
Browse files Browse the repository at this point in the history
  • Loading branch information
glevco committed Dec 6, 2023
1 parent 353c6c4 commit 9c984aa
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 34 deletions.
13 changes: 13 additions & 0 deletions extras/custom_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,25 @@ function check_do_not_import_tests_in_hathor() {
return 0
}

function check_do_not_import_from_hathor_in_entrypoints() {
PATTERN='^import .*hathor.*\|^from .*hathor.* import'

if grep -R "$PATTERN" "hathor/cli" | grep -v '# no-custom-check'; then
echo 'do not import from `hathor` in the module-level of a CLI entrypoint.'
echo 'instead, import locally inside the function that uses the import.'
echo 'alternatively, comment `# no-custom-check` to exclude a line.'
return 1
fi
return 0
}

# List of functions to be executed
checks=(
check_version_match
check_do_not_use_builtin_random_in_tests
check_deprecated_typing
check_do_not_import_tests_in_hathor
check_do_not_import_from_hathor_in_entrypoints
)

# Initialize a variable to track if any check fails
Expand Down
2 changes: 1 addition & 1 deletion hathor/builder/cli_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from structlog import get_logger

from hathor.cli.run_node import RunNodeArgs
from hathor.cli.run_node_args import RunNodeArgs
from hathor.consensus import ConsensusAlgorithm
from hathor.daa import DifficultyAdjustmentAlgorithm
from hathor.event import EventManager
Expand Down
2 changes: 1 addition & 1 deletion hathor/cli/db_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from argparse import ArgumentParser, FileType
from typing import TYPE_CHECKING, Iterator, Optional

from hathor.cli.run_node import RunNode
from hathor.cli.run_node import RunNode # no-custom-check

if TYPE_CHECKING:
from hathor.transaction import BaseTransaction
Expand Down
4 changes: 2 additions & 2 deletions hathor/cli/db_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
from argparse import ArgumentParser, FileType
from typing import TYPE_CHECKING, Iterator

from hathor.cli.db_export import MAGIC_HEADER
from hathor.cli.run_node import RunNode
from hathor.cli.run_node import RunNode # no-custom-check

if TYPE_CHECKING:
from hathor.transaction import BaseTransaction
Expand All @@ -46,6 +45,7 @@ def prepare(self, *, register_resources: bool = True) -> None:
self.in_file = io.BufferedReader(self._args.import_file)

def run(self) -> None:
from hathor.cli.db_export import MAGIC_HEADER
from hathor.util import tx_progress

header = self.in_file.read(len(MAGIC_HEADER))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any
from typing import TYPE_CHECKING, Any

from twisted.internet.interfaces import IAddress

from hathor.cli.events_simulator.event_forwarding_websocket_protocol import EventForwardingWebsocketProtocol
from hathor.event.websocket import EventWebsocketFactory
from hathor.simulator import Simulator
from hathor.event.websocket import EventWebsocketFactory # no-custom-check

if TYPE_CHECKING:
from hathor.cli.events_simulator.event_forwarding_websocket_protocol import EventForwardingWebsocketProtocol
from hathor.simulator import Simulator


class EventForwardingWebsocketFactory(EventWebsocketFactory):
def __init__(self, simulator: Simulator, *args: Any, **kwargs: Any) -> None:
def __init__(self, simulator: 'Simulator', *args: Any, **kwargs: Any) -> None:
self._simulator = simulator
super().__init__(*args, **kwargs)

def buildProtocol(self, _: IAddress) -> EventForwardingWebsocketProtocol:
def buildProtocol(self, _: IAddress) -> 'EventForwardingWebsocketProtocol':
protocol = EventForwardingWebsocketProtocol(self._simulator)
protocol.factory = self
return protocol
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@

from autobahn.websocket import ConnectionRequest

from hathor.event.websocket import EventWebsocketProtocol
from hathor.simulator import Simulator
from hathor.event.websocket import EventWebsocketProtocol # no-custom-check

if TYPE_CHECKING:
from hathor.cli.events_simulator.event_forwarding_websocket_factory import EventForwardingWebsocketFactory
from hathor.simulator import Simulator


class EventForwardingWebsocketProtocol(EventWebsocketProtocol):
factory: 'EventForwardingWebsocketFactory'

def __init__(self, simulator: Simulator) -> None:
def __init__(self, simulator: 'Simulator') -> None:
self._simulator = simulator
super().__init__()

Expand Down
3 changes: 1 addition & 2 deletions hathor/cli/mining.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

import requests

from hathor.conf.get_settings import get_settings

_SLEEP_ON_ERROR_SECONDS = 5
_MAX_CONN_RETRIES = math.inf

Expand Down Expand Up @@ -137,6 +135,7 @@ def execute(args: Namespace) -> None:
block.nonce, block.weight))

try:
from hathor.conf.get_settings import get_settings
from hathor.daa import DifficultyAdjustmentAlgorithm
from hathor.verification.verification_service import VerificationService, VertexVerifiers
settings = get_settings()
Expand Down
3 changes: 1 addition & 2 deletions hathor/cli/multisig_spend.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

from argparse import ArgumentParser, Namespace

from hathor.mining.cpu_mining_service import CpuMiningService


def create_parser() -> ArgumentParser:
from hathor.cli.util import create_parser
Expand All @@ -29,6 +27,7 @@ def create_parser() -> ArgumentParser:


def execute(args: Namespace) -> None:
from hathor.mining.cpu_mining_service import CpuMiningService
from hathor.transaction import Transaction
from hathor.transaction.scripts import MultiSig

Expand Down
3 changes: 1 addition & 2 deletions hathor/cli/nginx_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
from enum import Enum
from typing import Any, NamedTuple, Optional, TextIO

from hathor.cli.openapi_json import get_openapi_dict

BASE_PATH = os.path.join(os.path.dirname(__file__), 'nginx_files')


def get_openapi(src_file: Optional[TextIO] = None) -> dict[str, Any]:
""" Open and parse the json file or generate OpenAPI dict on-the-fly
"""
if src_file is None:
from hathor.cli.openapi_json import get_openapi_dict
return get_openapi_dict()
else:
return json.load(src_file)
Expand Down
2 changes: 1 addition & 1 deletion hathor/cli/openapi_files/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from typing import TypeVar

from hathor.api_util import Resource
from hathor.api_util import Resource # no-custom-check

_registered_resources: list[type[Resource]] = []

Expand Down
2 changes: 1 addition & 1 deletion hathor/cli/quick_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from argparse import ArgumentParser

from hathor.cli.run_node import RunNode
from hathor.cli.run_node import RunNode # no-custom-check


class QuickTest(RunNode):
Expand Down
19 changes: 11 additions & 8 deletions hathor/cli/run_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@
import os
import sys
from argparse import SUPPRESS, ArgumentParser, Namespace
from typing import Any, Callable, Optional
from typing import TYPE_CHECKING, Any, Callable, Optional

from pydantic import ValidationError
from structlog import get_logger

from hathor.cli.run_node_args import RunNodeArgs
from hathor.conf import TESTNET_SETTINGS_FILEPATH, HathorSettings
from hathor.exception import PreInitializationError
from hathor.feature_activation.feature import Feature

logger = get_logger()
# LOGGING_CAPTURE_STDOUT = True

if TYPE_CHECKING:
from hathor.cli.run_node_args import RunNodeArgs


class RunNode:
UNSAFE_ARGUMENTS: list[tuple[str, Callable[[RunNodeArgs], bool]]] = [
UNSAFE_ARGUMENTS: list[tuple[str, Callable[['RunNodeArgs'], bool]]] = [
('--test-mode-tx-weight', lambda args: bool(args.test_mode_tx_weight)),
('--enable-crash-api', lambda args: bool(args.enable_crash_api)),
('--x-sync-bridge', lambda args: bool(args.x_sync_bridge)),
Expand All @@ -45,6 +43,7 @@ def create_parser(cls) -> ArgumentParser:
Arguments must also be added to hathor.cli.run_node_args.RunNodeArgs
"""
from hathor.cli.util import create_parser
from hathor.feature_activation.feature import Feature
parser = create_parser()

parser.add_argument('--hostname', help='Hostname used to be accessed by other peers')
Expand Down Expand Up @@ -346,6 +345,9 @@ def check_python_version(self) -> None:
]))

def __init__(self, *, argv=None):
from hathor.cli.run_node_args import RunNodeArgs
from hathor.conf import TESTNET_SETTINGS_FILEPATH
from hathor.conf.get_settings import get_settings
self.log = logger.new()

if argv is None:
Expand All @@ -363,8 +365,9 @@ def __init__(self, *, argv=None):
os.environ['HATHOR_CONFIG_YAML'] = TESTNET_SETTINGS_FILEPATH

try:
HathorSettings()
get_settings()
except (TypeError, ValidationError) as e:
from hathor.exception import PreInitializationError
raise PreInitializationError(
'An error was found while trying to initialize HathorSettings. See above for details.'
) from e
Expand Down
4 changes: 2 additions & 2 deletions hathor/cli/run_node_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

from pydantic import Extra

from hathor.feature_activation.feature import Feature
from hathor.utils.pydantic import BaseModel
from hathor.feature_activation.feature import Feature # no-custom-check
from hathor.utils.pydantic import BaseModel # no-custom-check


class RunNodeArgs(BaseModel, extra=Extra.allow):
Expand Down
2 changes: 1 addition & 1 deletion hathor/cli/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from argparse import Namespace
from typing import Any, Callable

from hathor.cli.run_node import RunNode
from hathor.cli.run_node import RunNode # no-custom-check


def get_ipython(extra_args: list[Any], imported_objects: dict[str, Any]) -> Callable[[], None]:
Expand Down
3 changes: 1 addition & 2 deletions hathor/cli/twin_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import requests

from hathor.mining.cpu_mining_service import CpuMiningService


def create_parser() -> ArgumentParser:
from hathor.cli.util import create_parser
Expand All @@ -36,6 +34,7 @@ def create_parser() -> ArgumentParser:


def execute(args: Namespace) -> None:
from hathor.mining.cpu_mining_service import CpuMiningService
from hathor.transaction import Transaction

# Get tx you want to create a twin
Expand Down

0 comments on commit 9c984aa

Please sign in to comment.