Skip to content

Commit

Permalink
Use mypy strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
amh4r committed Oct 22, 2023
1 parent de63c86 commit bc45e6c
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 54 deletions.
6 changes: 4 additions & 2 deletions inngest/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from .client import Inngest
from .errors import NonRetriableError
from .event import Event
from .frameworks import flask, tornado
from .function import Function, FunctionOpts, NonRetriableError, Step, create_function
from .function import Function, FunctionOpts, Step, create_function
from .function_config import TriggerCron, TriggerEvent

__all__ = [
"create_function",
"Event",
"Function",
"FunctionOpts",
Expand All @@ -14,5 +14,7 @@
"Step",
"TriggerCron",
"TriggerEvent",
"create_function",
"flask",
"tornado",
]
10 changes: 0 additions & 10 deletions inngest/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,3 @@ class Event(BaseModel):
name: str
ts: int = 0
user: dict[str, object] = {}

@staticmethod
def from_raw(raw: dict) -> Event:
return Event(
data=raw["data"],
id=raw["id"],
name=raw["name"],
ts=raw["ts"],
user=raw["user"],
)
35 changes: 1 addition & 34 deletions inngest/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,12 @@
class Call(BaseModel):
ctx: CallContext
event: Event
steps: dict[str, MemoizedStep]

@staticmethod
def from_raw(raw: dict) -> Call:
ctx = CallContext.from_raw(raw["ctx"])

steps = {
step_id: MemoizedStep.from_raw(step)
for step_id, step in raw["steps"].items()
}

return Call(
ctx=ctx,
event=Event.from_raw(raw["event"]),
steps=steps,
)
steps: dict[str, object]


class CallContext(BaseModel):
stack: CallStack

@staticmethod
def from_raw(raw: dict) -> CallContext:
return CallContext(
stack=CallStack(
stack=raw["stack"]["stack"],
),
)


class CallStack(BaseModel):
stack: list[str]
Expand All @@ -65,13 +42,3 @@ class CallResponse(BaseModel):
class Opcode(Enum):
SLEEP = "Sleep"
STEP = "Step"


class MemoizedStep(BaseModel):
data: object

@staticmethod
def from_raw(raw: dict) -> MemoizedStep:
return MemoizedStep(
data=raw,
)
2 changes: 1 addition & 1 deletion inngest/frameworks/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def inngest_api() -> Response | str:

return _to_response(
comm.call_function(
call=Call.from_raw(json.loads(request.data)),
call=Call.from_dict(json.loads(request.data)),
fn_id=fn_id,
)
)
Expand Down
2 changes: 1 addition & 1 deletion inngest/frameworks/tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def post(self) -> None:
fn_id = raw_fn_id[0].decode("utf-8")

comm_res = comm.call_function(
call=Call.from_raw(json.loads(self.request.body)),
call=Call.from_dict(json.loads(self.request.body)),
fn_id=fn_id,
)

Expand Down
8 changes: 4 additions & 4 deletions inngest/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .client import Inngest
from .errors import NonRetriableError
from .event import Event
from .execution import Call, CallError, CallResponse, MemoizedStep, Opcode
from .execution import Call, CallError, CallResponse, Opcode
from .function_config import (
FunctionConfig,
Runtime,
Expand Down Expand Up @@ -66,7 +66,7 @@ def call(
except EarlyReturn as out:
return [
CallResponse(
data=out.data, # type: ignore
data=out.data,
display_name=out.display_name,
id=out.hashed_id,
name=out.name,
Expand Down Expand Up @@ -141,7 +141,7 @@ class _Step:
def __init__(
self,
client: Inngest,
memos: dict[str, MemoizedStep],
memos: dict[str, object],
step_id_counter: _StepIDCounter,
) -> None:
self._client = client
Expand All @@ -150,7 +150,7 @@ def __init__(

def _get_memo(self, hashed_id: str) -> object:
if hashed_id in self._memos:
return self._memos[hashed_id].data
return self._memos[hashed_id]

return EmptySentinel

Expand Down
12 changes: 11 additions & 1 deletion inngest/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from enum import Enum
from typing import TypeVar
from typing import Type, TypeVar

from pydantic import BaseModel as _BaseModel

Expand All @@ -11,6 +11,13 @@


class BaseModel(_BaseModel):
@classmethod
def from_dict(
cls: Type[TBaseModel],
raw: dict[str, object],
) -> TBaseModel:
return cls.model_validate(raw)

def to_dict(self) -> dict[str, object]:
dump = self.model_dump(
# Enable since we want to serialize to aliases.
Expand All @@ -23,3 +30,6 @@ def to_dict(self) -> dict[str, object]:
dump[k] = v.value

return dump


TBaseModel = TypeVar("TBaseModel", bound=BaseModel) # pylint: disable=invalid-name
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ profile = "black"

[tool.mypy]
disallow_untyped_defs = true
incremental = false
strict = true

[tool.pylint.'MESSAGES CONTROL']
disable = [
Expand Down
2 changes: 1 addition & 1 deletion tests/dev_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


class _DevServer:
_process: subprocess.Popen | None = None
_process: subprocess.Popen[bytes] | None = None
_thread: threading.Thread | None = None

def __init__(self) -> None:
Expand Down

0 comments on commit bc45e6c

Please sign in to comment.