Skip to content
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

Cleanup: Fix Mypy errors in init1.py #408

Merged
merged 1 commit into from
Sep 25, 2023
Merged
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
27 changes: 14 additions & 13 deletions runtimes/aleph-debian-11-python/init1.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just replace Line 35 with

ASGIApplication = Any

in this case? Reading up on NewType, you are not loosing anything important anyways in terms of type safetly, when subclassing Any.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from io import StringIO
from os import system
from shutil import make_archive
from typing import Any, AsyncIterable, Dict, List, NewType, Optional, Tuple, Union
from typing import Any, AsyncIterable, Dict, List, NewType, Optional, Tuple, Union, cast

import aiohttp
import msgpack
Expand Down Expand Up @@ -62,9 +62,9 @@ class ConfigurationPayload:
input_data: bytes
interface: Interface
vm_hash: str
code: bytes = None
encoding: Encoding = None
entrypoint: str = None
code: bytes
encoding: Encoding
entrypoint: str
ip: Optional[str] = None
ipv6: Optional[str] = None
route: Optional[str] = None
Expand Down Expand Up @@ -225,7 +225,7 @@ def setup_code_asgi(
app = locals[entrypoint]
else:
raise ValueError(f"Unknown encoding '{encoding}'")
return app
return ASGIApplication(app)


def setup_code_executable(
Expand Down Expand Up @@ -261,9 +261,9 @@ def setup_code_executable(


def setup_code(
code: Optional[bytes],
encoding: Optional[Encoding],
entrypoint: Optional[str],
code: bytes,
encoding: Encoding,
entrypoint: str,
interface: Interface,
) -> Union[ASGIApplication, subprocess.Popen]:
if interface == Interface.asgi:
Expand All @@ -284,15 +284,15 @@ async def run_python_code_http(
# Execute in the same process, saves ~20ms than a subprocess

# The body should not be part of the ASGI scope itself
body: bytes = scope.pop("body")
request_body: bytes = scope.pop("body")

async def receive():
type_ = (
"http.request"
if scope["type"] in ("http", "websocket")
else "aleph.message"
)
return {"type": type_, "body": body, "more_body": False}
return {"type": type_, "body": request_body, "more_body": False}

send_queue: asyncio.Queue = asyncio.Queue()

Expand All @@ -311,13 +311,13 @@ async def send(dico):
headers = {}

logger.debug("Waiting for body")
body: Dict = await send_queue.get()
response_body: Dict = await send_queue.get()

logger.debug("Waiting for buffer")
output = buf.getvalue()

logger.debug(f"Headers {headers}")
logger.debug(f"Body {body}")
logger.debug(f"Body {response_body}")
logger.debug(f"Output {output}")

logger.debug("Getting output data")
Expand All @@ -330,7 +330,7 @@ async def send(dico):
output_data = b""

logger.debug("Returning result")
return headers, body, output, output_data
return headers, response_body, output, output_data


async def make_request(session, scope):
Expand Down Expand Up @@ -429,6 +429,7 @@ async def process_instruction(
output_data: Optional[bytes]

if interface == Interface.asgi:
application = cast(ASGIApplication, application)
headers, body, output, output_data = await run_python_code_http(
application=application, scope=payload.scope
)
Expand Down