Skip to content

Commit

Permalink
Make TransactionBase a Protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekcyjna committed Oct 8, 2023
1 parent aff89a1 commit ea24527
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
27 changes: 21 additions & 6 deletions transactron/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
from collections.abc import Sequence, Iterable, Callable, Mapping, Iterator
from contextlib import contextmanager
from enum import Enum, auto
from typing import ClassVar, NoReturn, TypeAlias, TypedDict, Union, Optional, Tuple
from typing import (
ClassVar,
NoReturn,
TypeAlias,
TypedDict,
Union,
Optional,
Tuple,
TypeVar,
Protocol,
runtime_checkable,
)
from graphlib import TopologicalSorter
from typing_extensions import Self
from amaranth import *
Expand Down Expand Up @@ -38,6 +49,7 @@
TransactionScheduler: TypeAlias = Callable[["MethodMap", TransactionGraph, TransactionGraphCC, PriorityOrder], Module]
RecordDict: TypeAlias = ValueLike | Mapping[str, "RecordDict"]
TransactionOrMethod: TypeAlias = Union["Transaction", "Method"]
TransactionOrMethodBound = TypeVar("TransactionOrMethodBound", bound=Union["Transaction", "Method"])


class Priority(Enum):
Expand Down Expand Up @@ -670,15 +682,20 @@ def elaborate(self, platform):
return self.main_module


class TransactionBase(Owned):
@runtime_checkable
class TransactionBase(Owned, Protocol):
stack: ClassVar[list[Union["Transaction", "Method"]]] = []
def_counter: ClassVar[count] = count()
def_order: int
defined: bool = False
name: str
method_uses: dict["Method", Tuple[ValueLike, ValueLike]]
relations: list[RelationBase]
simultaneous_list: list[TransactionOrMethod]
independent_list: list[TransactionOrMethod]

def __init__(self):
self.method_uses: dict[Method, Tuple[ValueLike, ValueLike]] = dict()
self.method_uses: dict["Method", Tuple[ValueLike, ValueLike]] = dict()
self.relations: list[RelationBase] = []
self.simultaneous_list: list[TransactionOrMethod] = []
self.independent_list: list[TransactionOrMethod] = []
Expand Down Expand Up @@ -769,9 +786,7 @@ def _independent(self, *others: TransactionOrMethod) -> None:
self.independent_list += others

@contextmanager
def context(self, m: TModule) -> Iterator[Self]:
assert isinstance(self, Transaction) or isinstance(self, Method) # for typing

def context(self: TransactionOrMethodBound, m: TModule) -> Iterator[TransactionOrMethodBound]:
parent = TransactionBase.peek()
if parent is not None:
parent.schedule_before(self)
Expand Down
5 changes: 2 additions & 3 deletions transactron/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
"""

from enum import IntFlag
from abc import ABC
from collections import defaultdict
from typing import Literal, Optional
from typing import Literal, Optional, Protocol

from amaranth.hdl.ir import Elaboratable, Fragment
from .tracing import TracingFragment


class Owned(ABC):
class Owned(Protocol):
name: str
owner: Optional[Elaboratable]

Expand Down

0 comments on commit ea24527

Please sign in to comment.