Skip to content

Commit

Permalink
Fix: Small code quality fixes by ruff check --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hoh committed Mar 6, 2024
1 parent b497e9a commit e2ca2de
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 22 deletions.
3 changes: 2 additions & 1 deletion src/aleph/vm/controllers/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import logging
from abc import ABC
from asyncio.subprocess import Process
from typing import Any, Coroutine, Optional
from collections.abc import Coroutine
from typing import Any, Optional

from aleph_message.models import ItemHash
from aleph_message.models.execution.environment import MachineResources
Expand Down
9 changes: 4 additions & 5 deletions src/aleph/vm/controllers/qemu/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from asyncio import Task
from asyncio.subprocess import Process
from pathlib import Path
from typing import Callable, Dict, Generic, Optional, Tuple, TypedDict, TypeVar, Union
from typing import Callable, Generic, Optional, TypedDict, TypeVar, Union

import psutil
from aleph_message.models import ItemHash
Expand Down Expand Up @@ -37,11 +37,10 @@


class AlephQemuResources(AlephFirecrackerResources):
async def download_all(self):
async def download_all(self) -> None:
volume = self.message_content.rootfs
parent_image_path = await get_rootfs_base_path(volume.parent.ref)
self.rootfs_path = await self.make_writable_volume(parent_image_path, volume)
return

async def make_writable_volume(self, parent_image_path, volume: Union[PersistentVolume, RootfsVolume]):
"""Create a new qcow2 image file based on the passed one, that we give to the VM to write onto"""
Expand Down Expand Up @@ -90,7 +89,7 @@ class EntryDict(TypedDict):
MESSAGE: str


def make_logs_queue(stdout_identifier, stderr_identifier, skip_past=True) -> Tuple[asyncio.Queue, Callable[[], None]]:
def make_logs_queue(stdout_identifier, stderr_identifier, skip_past=True) -> tuple[asyncio.Queue, Callable[[], None]]:
"""Create a queue which streams the logs for the process.
@param stdout_identifier: journald identifier for process stdout
Expand Down Expand Up @@ -149,7 +148,7 @@ class AlephQemuInstance(Generic[ConfigurationType], CloudInitMixin, AlephVmContr
support_snapshot = False
qmp_socket_path = None
persistent = True
_queue_cancellers: Dict[asyncio.Queue, Callable] = {}
_queue_cancellers: dict[asyncio.Queue, Callable] = {}
controller_configuration: Configuration

def __repr__(self):
Expand Down
4 changes: 2 additions & 2 deletions src/aleph/vm/hypervisors/qemu/qemuvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from aleph.vm.controllers.qemu.instance import logger


class QemuVM(object):
class QemuVM:

Check warning on line 14 in src/aleph/vm/hypervisors/qemu/qemuvm.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/hypervisors/qemu/qemuvm.py#L14

Added line #L14 was not covered by tests
qemu_bin_path: str
cloud_init_drive_path: Optional[str]
image_path: str
Expand All @@ -26,7 +26,7 @@ def __repr__(self) -> str:
if self.qemu_process:
return f"<QemuVM: {self.qemu_process.pid}>"
else:
return f"<QemuVM: not running>"
return "<QemuVM: not running>"

Check warning on line 29 in src/aleph/vm/hypervisors/qemu/qemuvm.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/hypervisors/qemu/qemuvm.py#L29

Added line #L29 was not covered by tests

def __init__(self, config: QemuVMConfiguration):
self.qemu_bin_path = config.qemu_bin_path
Expand Down
3 changes: 2 additions & 1 deletion src/aleph/vm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import logging
import uuid
from asyncio import Task
from collections.abc import Coroutine
from dataclasses import dataclass
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Callable, Coroutine, Optional, Union
from typing import TYPE_CHECKING, Callable, Optional, Union

from aleph_message.models import (
ExecutableContent,
Expand Down
3 changes: 2 additions & 1 deletion src/aleph/vm/orchestrator/payment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import asyncio
import logging
from collections.abc import Iterable
from decimal import Decimal
from typing import Iterable, Optional
from typing import Optional

import aiohttp
from aleph_message.models import ItemHash, PaymentType
Expand Down
3 changes: 2 additions & 1 deletion src/aleph/vm/orchestrator/views/authentication.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import functools
import json
import logging
from collections.abc import Awaitable, Coroutine
from datetime import datetime, timedelta, timezone
from typing import Any, Awaitable, Callable, Coroutine, Literal, Union
from typing import Any, Callable, Literal, Union

import pydantic
from aiohttp import web
Expand Down
5 changes: 3 additions & 2 deletions src/aleph/vm/orchestrator/views/host_status.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import socket
from typing import Any, Awaitable, Callable, Optional, Tuple
from collections.abc import Awaitable
from typing import Any, Callable, Optional

import aiohttp

Expand Down Expand Up @@ -45,7 +46,7 @@ async def check_host_egress_ipv6() -> bool:
return await check_ip_connectivity(settings.CONNECTIVITY_IPV6_URL)


async def resolve_dns(hostname: str) -> Tuple[Optional[str], Optional[str]]:
async def resolve_dns(hostname: str) -> tuple[Optional[str], Optional[str]]:
"""Resolve a hostname to an IPv4 and IPv6 address."""
ipv4: Optional[str] = None
ipv6: Optional[str] = None
Expand Down
6 changes: 3 additions & 3 deletions src/aleph/vm/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
from collections.abc import Iterable
from datetime import datetime, timezone
from typing import Dict, Optional
from typing import Optional

from aleph_message.models import (
Chain,
Expand Down Expand Up @@ -284,9 +284,9 @@ def get_instance_executions(self) -> Iterable[VmExecution]:
)
return executions or []

def get_executions_by_sender(self, payment_type: PaymentType) -> Dict[str, Dict[str, list[VmExecution]]]:
def get_executions_by_sender(self, payment_type: PaymentType) -> dict[str, dict[str, list[VmExecution]]]:
"""Return all executions of the given type, grouped by sender and by chain."""
executions_by_sender: Dict[str, Dict[str, list[VmExecution]]] = {}
executions_by_sender: dict[str, dict[str, list[VmExecution]]] = {}

Check warning on line 289 in src/aleph/vm/pool.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/pool.py#L289

Added line #L289 was not covered by tests
for vm_hash, execution in self.executions.items():
if execution.vm_hash in (settings.CHECK_FASTAPI_VM_ID, settings.LEGACY_CHECK_FASTAPI_VM_ID):
# Ignore Diagnostic VM execution
Expand Down
12 changes: 6 additions & 6 deletions src/aleph/vm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from dataclasses import is_dataclass
from pathlib import Path
from shutil import disk_usage
from typing import Any, Callable, Dict, Optional
from typing import Any, Callable, Optional

import aiodns
import msgpack
Expand All @@ -22,10 +22,10 @@
logger = logging.getLogger(__name__)


def get_message_executable_content(message_dict: Dict) -> ExecutableContent:
def get_message_executable_content(message_dict: dict) -> ExecutableContent:
try:
return ProgramContent.parse_obj(message_dict)
except ValueError as error:
except ValueError:

Check warning on line 28 in src/aleph/vm/utils.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/utils.py#L28

Added line #L28 was not covered by tests
return InstanceContent.parse_obj(message_dict)


Expand Down Expand Up @@ -190,11 +190,11 @@ def to_normalized_address(value: str) -> HexAddress:
try:
hex_address = hexstr_if_str(to_hex, value).lower()
except AttributeError:
raise TypeError("Value must be any string, instead got type {}".format(type(value)))
raise TypeError(f"Value must be any string, instead got type {type(value)}")

Check warning on line 193 in src/aleph/vm/utils.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/utils.py#L193

Added line #L193 was not covered by tests
if is_address(hex_address):
return HexAddress(HexStr(hex_address))
else:
raise ValueError("Unknown format {}, attempted to normalize to {}".format(value, hex_address))
raise ValueError(f"Unknown format {value}, attempted to normalize to {hex_address}")

Check warning on line 197 in src/aleph/vm/utils.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/utils.py#L197

Added line #L197 was not covered by tests


def md5sum(file_path: Path) -> str:
Expand All @@ -205,7 +205,7 @@ def md5sum(file_path: Path) -> str:
def file_hashes_differ(source: Path, destination: Path, checksum: Callable[[Path], str] = md5sum) -> bool:
"""Check if the MD5 hash of two files differ."""
if not source.exists():
raise FileNotFoundError("Source file does not exist: {}".format(source))
raise FileNotFoundError(f"Source file does not exist: {source}")

Check warning on line 208 in src/aleph/vm/utils.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/utils.py#L208

Added line #L208 was not covered by tests

if not destination.exists():
return True
Expand Down

0 comments on commit e2ca2de

Please sign in to comment.