Skip to content

Commit

Permalink
Autumn cleaning: types (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
tilk authored Oct 23, 2023
1 parent dfd451e commit 7100860
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 33 deletions.
4 changes: 2 additions & 2 deletions coreblocks/fu/div_unit.py
Original file line number Diff line number Diff line change
@@ -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 *

Expand Down Expand Up @@ -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)


Expand Down
6 changes: 3 additions & 3 deletions coreblocks/fu/mul_unit.py
Original file line number Diff line number Diff line change
@@ -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 *
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion coreblocks/scheduler/scheduler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Sequence
from collections.abc import Sequence

from amaranth import *

Expand Down
2 changes: 1 addition & 1 deletion coreblocks/stages/func_blocks_unifier.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Iterable
from collections.abc import Iterable

from amaranth import *

Expand Down
3 changes: 2 additions & 1 deletion coreblocks/structs_common/rs.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
38 changes: 19 additions & 19 deletions coreblocks/utils/_typing.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion coreblocks/utils/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion transactron/_utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 5 additions & 4 deletions transactron/lib/transformers.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 7100860

Please sign in to comment.