Skip to content

Use ruff for formatting and document code quality #729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,35 @@ Unless your focus is developing the VM-Connector, using the Docker image is easi
## Testing
See [Testinc doc](./TESTING.md)

## Code Formatting and Linting

To help maintain a clean and consistent codebase, we provide automated tools for formatting and style checks.
To ensure your code is properly **formatted** according to project standards, you can use:

```bash
hatch linting:fmt
```

**Typing** helps ensure your code adheres to expected type annotations, improving reliability and clarity. To validate
typing in your code, use:
```bash
hatch linting:typing
```

These checks are also validated in Continuous Integration (CI) alongside unit tests. To ensure a smooth workflow, we
recommend running these commands before committing changes.

**Linting** checks for potential errors, coding style violations, and patterns that may lead to bugs or reduce code
quality (e.g., unused variables, incorrect imports, or inconsistent naming). While linting is not currently enforced in
Continuous Integration (CI), it is considered a best practice to check linting manually to maintain high-quality code.
You can manually lint your code by running:

```bash
hatch fmt
```

Following these best practices can help streamline code reviews and improve overall project quality.

# Architecture

![Aleph im VM - Details](https://user-images.githubusercontent.com/404665/127126908-3225a633-2c36-4129-8766-9810f2fcd7d6.png)
Expand Down
1 change: 1 addition & 0 deletions examples/example_django/manage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""

import os
import sys

Expand Down
17 changes: 11 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ python = [ "3.10", "3.11", "3.12" ]
[tool.hatch.envs.linting]
detached = true
dependencies = [
"black==24.3.0",
"mypy==1.8.0",
"ruff==0.4.6",
"isort==5.13.2",
Expand All @@ -126,16 +125,16 @@ dependencies = [
]
[tool.hatch.envs.linting.scripts]
typing = "mypy {args:src/aleph/vm/ tests/ examples/example_fastapi runtimes/aleph-debian-12-python}"
# Check
style = [
# "ruff {args:.}",
"black --check --diff {args:.}",
"ruff format --diff {args:.}",
"isort --check-only --profile black {args:.}",
"yamlfix --check .",
"pyproject-fmt --check pyproject.toml",
]
# Do modification
fmt = [
"black {args:.}",
# "ruff --fix {args:.}",
"ruff format {args:.}",
"isort --profile black {args:.}",
"yamlfix .",
"pyproject-fmt pyproject.toml",
Expand All @@ -154,6 +153,7 @@ line-length = 120
[tool.ruff]
target-version = "py310"
line-length = 120
src = [ "src" ]
lint.select = [
"A",
"ARG",
Expand Down Expand Up @@ -182,6 +182,8 @@ lint.select = [
"YTT",
]
lint.ignore = [
"ISC001",
# https://docs.astral.sh/ruff/rules/single-line-implicit-string-concatenation/#single-line-implicit-string-concatenation-isc001
# # Allow non-abstract empty methods in abstract base classes
# "B027",
# # Allow boolean positional values in function calls, like `dict.get(... True)`
Expand All @@ -199,11 +201,14 @@ lint.ignore = [
# # Don't touch unused imports
# "F401",
#]
#lint.isort = [ "aleph.vm" ]

# Tests can use magic values, assertions, and relative imports
lint.per-file-ignores."tests/**/*" = [ "PLR2004", "S101", "TID252" ]

[tool.isort]
profile = "black"
extra_standard_library = [ "packaging" ]

[tool.pytest.ini_options]
pythonpath = [
"src",
Expand Down
1 change: 1 addition & 0 deletions runtimes/aleph-debian-12-python/init1.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ async def main() -> None:

class ServerReference:
"Reference used to close the server from within `handle_instruction"

server: asyncio.AbstractServer

server_reference = ServerReference()
Expand Down
1 change: 0 additions & 1 deletion src/aleph/vm/hypervisors/qemu_confidential/qemuvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@


class QemuConfidentialVM(QemuVM):

sev_policy: str = hex(AMDSEVPolicy.NO_DBG)
sev_dh_cert_file: Path # "vm_godh.b64"
sev_session_file: Path # "vm_session.b64"
Expand Down
1 change: 0 additions & 1 deletion src/aleph/vm/orchestrator/custom_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class InjectingFilter(logging.Filter):
"""

def filter(self, record):

vm_hash = ctx_current_execution_hash.get(None)
if not vm_hash:
vm_execution: VmExecution | None = ctx_current_execution.get(None)
Expand Down
2 changes: 1 addition & 1 deletion src/aleph/vm/orchestrator/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from decimal import Decimal
from hashlib import sha256
from json import JSONDecodeError
from packaging.version import InvalidVersion, Version
from pathlib import Path
from secrets import compare_digest
from string import Template
Expand Down Expand Up @@ -55,7 +56,6 @@
get_ref_from_dns,
)
from aleph.vm.version import __version__
from packaging.version import InvalidVersion, Version

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion src/aleph/vm/orchestrator/views/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ async def authenticate_websocket_message(message) -> str:


def require_jwk_authentication(
handler: Callable[[web.Request, str], Coroutine[Any, Any, web.StreamResponse]]
handler: Callable[[web.Request, str], Coroutine[Any, Any, web.StreamResponse]],
) -> Callable[[web.Request], Awaitable[web.StreamResponse]]:
"""A decorator to enforce JWK-based authentication for HTTP requests.

Expand Down
1 change: 0 additions & 1 deletion src/aleph/vm/orchestrator/views/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ async def operate_confidential_initialize(request: web.Request, authenticated_se
"""Start the confidential virtual machine if possible."""
vm_hash = get_itemhash_or_400(request.match_info)
with set_vm_for_logging(vm_hash=vm_hash):

pool: VmPool = request.app["vm_pool"]
logger.debug(f"Iterating through running executions... {pool.executions}")
execution = get_execution_or_404(vm_hash, pool=pool)
Expand Down
3 changes: 3 additions & 0 deletions tests/supervisor/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
to_0x_hex,
)

# Ensure this is not removed by ruff
assert patch_datetime_now


@pytest.mark.asyncio
async def test_require_jwk_authentication_missing_header(aiohttp_client):
Expand Down
5 changes: 2 additions & 3 deletions tests/supervisor/test_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging
from asyncio.subprocess import Process
from pathlib import Path
from typing import Optional

import pytest
from aleph_message.models import ItemHash
Expand All @@ -21,8 +20,8 @@

@pytest.mark.asyncio
class MockSystemDManager(SystemDManager):
execution: Optional[MicroVM] = None
process: Optional[Process] = None
execution: MicroVM | None = None
process: Process | None = None

async def enable_and_start(self, vm_hash: str):
config_path = Path(f"{settings.EXECUTION_ROOT}/{vm_hash}-controller.json")
Expand Down
5 changes: 2 additions & 3 deletions tests/supervisor/test_qemu_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging
from asyncio.subprocess import Process
from pathlib import Path
from typing import Optional

import pytest
from aleph_message.models import ItemHash
Expand All @@ -21,8 +20,8 @@

@pytest.mark.asyncio
class MockSystemDManager(SystemDManager):
execution: Optional[QemuVM] = None
process: Optional[Process] = None
execution: QemuVM | None = None
process: Process | None = None

async def enable_and_start(self, vm_hash: str):
config_path = Path(f"{settings.EXECUTION_ROOT}/{vm_hash}-controller.json")
Expand Down
2 changes: 0 additions & 2 deletions tests/supervisor/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


def test_check_system_module_enabled():

with mock.patch(
"pathlib.Path.exists",
return_value=True,
Expand All @@ -19,7 +18,6 @@ def test_check_system_module_enabled():
"aleph.vm.utils.Path.open",
mock.mock_open(read_data=expected_value),
):

output = check_system_module("kvm_amd/parameters/sev_enp")
assert output == expected_value

Expand Down
2 changes: 1 addition & 1 deletion tests/supervisor/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import tempfile
from pathlib import Path, PosixPath
from pathlib import Path
from unittest import mock
from unittest.mock import call

Expand Down
6 changes: 4 additions & 2 deletions tests/supervisor/views/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@
import aiohttp
import pytest
from aiohttp.test_utils import TestClient
from aleph_message.models import ItemHash, ProgramMessage
from aleph_message.models import ItemHash

from aleph.vm.conf import settings
from aleph.vm.orchestrator.metrics import ExecutionRecord
from aleph.vm.orchestrator.supervisor import setup_webapp
from aleph.vm.pool import VmPool
from aleph.vm.storage import get_message
from aleph.vm.utils.logs import EntryDict
from aleph.vm.utils.test_helpers import (
generate_signer_and_signed_headers_for_operation,
patch_datetime_now,
)

# Ensure this is not removed by ruff
assert patch_datetime_now


@pytest.mark.asyncio
async def test_operator_confidential_initialize_not_authorized(aiohttp_client):
Expand Down
Loading