core: improve mypy type checks without generating runtime problems

This commit is contained in:
Helmut Merz 2026-10-17 17:23:11 +02:00
parent 9aebbff176
commit 48a5105eea

View file

@ -2,21 +2,33 @@
"""The scopes.core package""" """The scopes.core package"""
from __future__ import annotations # no check of type annotations at runtime
import asyncio import asyncio
from dataclasses import dataclass from collections.abc import AsyncGenerator, Callable, Coroutine
from types import CoroutineType, FunctionType from dataclasses import dataclass, field
from typing import Any from typing import Any
from scopes.core.message import Message
from scopes.util import keywords as k, Symbol from scopes.util import keywords as k, Symbol
type Proc = Callable[[Scope, Message], AsyncGenerator[Scope]]
type Program = Callable[[Scope], Proc]
type Syn = Callable[[Message], Message]
async def proc_dummy(scope: Scope, msg: Message) -> AsyncGenerator[Scope]:
yield scope
@dataclass @dataclass
class Scope: class Scope:
value: Any = 0 value: Any = 0
categ: tuple[Symbol, Symbol] = (k.csys, k.c00) categ: tuple[Symbol, Symbol] = (k.csys, k.c00)
stage: Symbol = k.initial stage: Symbol = k.initial
proc: CoroutineType | None = None proc: Proc = proc_dummy
program: dict[tuple[Symbol, Symbol], CoroutineType] = {} program: Program = lambda x: proc_dummy
syns: list[FunctionType] = [] syns: list[Syn] = field(default_factory=list)
environ: asyncio.Queue | None = None environ: asyncio.Queue = field(default_factory=asyncio.Queue)
def set_proc(self) -> Scope:
self.proc = self.program(self)
return self