From 71008605e1df07b98f42118e904ab37cb637953e Mon Sep 17 00:00:00 2001 From: Marek Materzok Date: Mon, 23 Oct 2023 20:19:25 +0200 Subject: [PATCH] Autumn cleaning: types (#479) --- coreblocks/fu/div_unit.py | 4 +-- coreblocks/fu/mul_unit.py | 6 ++-- coreblocks/scheduler/scheduler.py | 2 +- coreblocks/stages/func_blocks_unifier.py | 2 +- coreblocks/structs_common/rs.py | 3 +- coreblocks/utils/_typing.py | 38 ++++++++++++------------ coreblocks/utils/utils.py | 3 +- transactron/_utils.py | 3 +- transactron/lib/transformers.py | 9 +++--- 9 files changed, 37 insertions(+), 33 deletions(-) diff --git a/coreblocks/fu/div_unit.py b/coreblocks/fu/div_unit.py index f78feadf8..c874d439f 100644 --- a/coreblocks/fu/div_unit.py +++ b/coreblocks/fu/div_unit.py @@ -1,6 +1,6 @@ from dataclasses import KW_ONLY, dataclass from enum import IntFlag, auto -from typing import Sequence, Tuple +from collections.abc import Sequence from amaranth import * @@ -34,7 +34,7 @@ def get_instructions(self) -> Sequence[tuple]: ] -def get_input(arg: Record) -> Tuple[Value, Value]: +def get_input(arg: Record) -> tuple[Value, Value]: return arg.s1_val, Mux(arg.imm, arg.imm, arg.s2_val) diff --git a/coreblocks/fu/mul_unit.py b/coreblocks/fu/mul_unit.py index a7230c774..3d4888cfa 100644 --- a/coreblocks/fu/mul_unit.py +++ b/coreblocks/fu/mul_unit.py @@ -1,5 +1,5 @@ from enum import IntFlag, IntEnum, auto -from typing import Sequence, Tuple +from collections.abc import Sequence from dataclasses import KW_ONLY, dataclass from amaranth import * @@ -45,7 +45,7 @@ def get_instructions(self) -> Sequence[tuple]: ] -def get_input(arg: Record) -> Tuple[Value, Value]: +def get_input(arg: Record) -> tuple[Value, Value]: """ Operation of getting two input values. @@ -56,7 +56,7 @@ def get_input(arg: Record) -> Tuple[Value, Value]: Returns ------- - return : Tuple[Value, Value] + return : tuple[Value, Value] Two input values. """ return arg.s1_val, Mux(arg.imm, arg.imm, arg.s2_val) diff --git a/coreblocks/scheduler/scheduler.py b/coreblocks/scheduler/scheduler.py index b85e6d052..eab680fe2 100644 --- a/coreblocks/scheduler/scheduler.py +++ b/coreblocks/scheduler/scheduler.py @@ -1,4 +1,4 @@ -from typing import Sequence +from collections.abc import Sequence from amaranth import * diff --git a/coreblocks/stages/func_blocks_unifier.py b/coreblocks/stages/func_blocks_unifier.py index 8bd97c423..8748bf5df 100644 --- a/coreblocks/stages/func_blocks_unifier.py +++ b/coreblocks/stages/func_blocks_unifier.py @@ -1,4 +1,4 @@ -from typing import Iterable +from collections.abc import Iterable from amaranth import * diff --git a/coreblocks/structs_common/rs.py b/coreblocks/structs_common/rs.py index 60bcdcce3..eb045c3a4 100644 --- a/coreblocks/structs_common/rs.py +++ b/coreblocks/structs_common/rs.py @@ -1,4 +1,5 @@ -from typing import Iterable, Optional +from collections.abc import Iterable +from typing import Optional from amaranth import * from amaranth.lib.coding import PriorityEncoder from transactron import Method, def_method, TModule diff --git a/coreblocks/utils/_typing.py b/coreblocks/utils/_typing.py index 124066124..5e27a229d 100644 --- a/coreblocks/utils/_typing.py +++ b/coreblocks/utils/_typing.py @@ -1,18 +1,14 @@ from typing import ( - ContextManager, Generic, NoReturn, Optional, Protocol, - Sequence, - Tuple, - Type, TypeAlias, - Iterable, - Mapping, TypeVar, runtime_checkable, ) +from collections.abc import Iterable, Mapping, Sequence +from contextlib import AbstractContextManager from enum import Enum from amaranth import * from amaranth.lib.data import View @@ -21,16 +17,18 @@ from amaranth.hdl.rec import Direction, Layout # Types representing Amaranth concepts -FragmentLike = Fragment | Elaboratable -ValueLike = Value | int | Enum | ValueCastable -ShapeLike = Shape | ShapeCastable | int | range | Type[Enum] +FragmentLike: TypeAlias = Fragment | Elaboratable +ValueLike: TypeAlias = Value | int | Enum | ValueCastable +ShapeLike: TypeAlias = Shape | ShapeCastable | int | range | type[Enum] StatementLike: TypeAlias = Statement | Iterable["StatementLike"] -LayoutLike = Layout | Sequence[Tuple[str, ShapeLike | "LayoutLike"] | Tuple[str, ShapeLike | "LayoutLike", Direction]] +LayoutLike: TypeAlias = ( + Layout | Sequence[tuple[str, "ShapeLike | LayoutLike"] | tuple[str, "ShapeLike | LayoutLike", Direction]] +) SwitchKey: TypeAlias = str | int | Enum # Internal Coreblocks types SignalBundle: TypeAlias = Signal | Record | View | Iterable["SignalBundle"] | Mapping[str, "SignalBundle"] -LayoutList = list[Tuple[str, ShapeLike | "LayoutList"]] +LayoutList: TypeAlias = list[tuple[str, "ShapeLike | LayoutList"]] class _ModuleBuilderDomainsLike(Protocol): @@ -55,28 +53,30 @@ class ModuleLike(Protocol, Generic[_T_ModuleBuilderDomains]): domains: _ModuleBuilderDomainSet d: _T_ModuleBuilderDomains - def If(self, cond: ValueLike) -> ContextManager[None]: # noqa: N802 + def If(self, cond: ValueLike) -> AbstractContextManager[None]: # noqa: N802 ... - def Elif(self, cond: ValueLike) -> ContextManager[None]: # noqa: N802 + def Elif(self, cond: ValueLike) -> AbstractContextManager[None]: # noqa: N802 ... - def Else(self) -> ContextManager[None]: # noqa: N802 + def Else(self) -> AbstractContextManager[None]: # noqa: N802 ... - def Switch(self, test: ValueLike) -> ContextManager[None]: # noqa: N802 + def Switch(self, test: ValueLike) -> AbstractContextManager[None]: # noqa: N802 ... - def Case(self, *patterns: SwitchKey) -> ContextManager[None]: # noqa: N802 + def Case(self, *patterns: SwitchKey) -> AbstractContextManager[None]: # noqa: N802 ... - def Default(self) -> ContextManager[None]: # noqa: N802 + def Default(self) -> AbstractContextManager[None]: # noqa: N802 ... - def FSM(self, reset: Optional[str] = ..., domain: str = ..., name: str = ...) -> ContextManager[FSM]: # noqa: N802 + def FSM( # noqa: N802 + self, reset: Optional[str] = ..., domain: str = ..., name: str = ... + ) -> AbstractContextManager[FSM]: ... - def State(self, name: str) -> ContextManager[None]: # noqa: N802 + def State(self, name: str) -> AbstractContextManager[None]: # noqa: N802 ... @property diff --git a/coreblocks/utils/utils.py b/coreblocks/utils/utils.py index 11a0df1b1..31fc830ab 100644 --- a/coreblocks/utils/utils.py +++ b/coreblocks/utils/utils.py @@ -1,6 +1,7 @@ from contextlib import contextmanager from enum import Enum -from typing import Iterable, Literal, Mapping, Optional, TypeAlias, cast, overload +from typing import Literal, Optional, TypeAlias, cast, overload +from collections.abc import Iterable, Mapping from amaranth import * from amaranth.hdl.ast import Assign, ArrayProxy from amaranth.lib import data diff --git a/transactron/_utils.py b/transactron/_utils.py index 0df18db2e..f86b0d647 100644 --- a/transactron/_utils.py +++ b/transactron/_utils.py @@ -1,7 +1,8 @@ import itertools import sys from inspect import Parameter, signature -from typing import Callable, Iterable, Optional, TypeAlias, TypeVar, Mapping +from typing import Optional, TypeAlias, TypeVar +from collections.abc import Callable, Iterable, Mapping from amaranth import * from coreblocks.utils._typing import LayoutLike from coreblocks.utils import OneHotSwitchDynamic diff --git a/transactron/lib/transformers.py b/transactron/lib/transformers.py index 23c385b53..e4b7aa0c0 100644 --- a/transactron/lib/transformers.py +++ b/transactron/lib/transformers.py @@ -1,7 +1,8 @@ from amaranth import * from ..core import * from ..core import RecordDict -from typing import Optional, Callable, Tuple +from typing import Optional +from collections.abc import Callable from coreblocks.utils import ValueLike, assign, AssignType from .connectors import Forwarder, ManyToOneConnectTrans, ConnectTrans @@ -35,8 +36,8 @@ def __init__( self, target: Method, *, - i_transform: Optional[Tuple[MethodLayout, Callable[[TModule, Record], RecordDict]]] = None, - o_transform: Optional[Tuple[MethodLayout, Callable[[TModule, Record], RecordDict]]] = None, + i_transform: Optional[tuple[MethodLayout, Callable[[TModule, Record], RecordDict]]] = None, + o_transform: Optional[tuple[MethodLayout, Callable[[TModule, Record], RecordDict]]] = None, ): """ Parameters @@ -132,7 +133,7 @@ class MethodProduct(Elaboratable): def __init__( self, targets: list[Method], - combiner: Optional[Tuple[MethodLayout, Callable[[TModule, list[Record]], RecordDict]]] = None, + combiner: Optional[tuple[MethodLayout, Callable[[TModule, list[Record]], RecordDict]]] = None, ): """Method product.