Skip to content

Commit

Permalink
Fix compatibility with Python 3.8
Browse files Browse the repository at this point in the history
- Change cache decorator to lru_cache, as cache decorator is only available
since Python 3.9 and is an alias for lru_cache(maxsize=None)
- Change Python 3.9-style type annotations to older Python 3.8-style - using
(lowercase) list or tuple with subscript type wasn't supported until Python 3.9
- Replace | dict operator that isn't available until Python 3.9 with .update()

Internal-tag: [#53526]
Signed-off-by: Krzysztof Obłonczek <koblonczek@internships.antmicro.com>
  • Loading branch information
koblonczek committed Jan 10, 2024
1 parent 6385a04 commit ecc30b8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
22 changes: 13 additions & 9 deletions fpga_topwrap/elaboratable_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Copyright (C) 2023 Antmicro
# SPDX-License-Identifier: Apache-2.0

from functools import cache
from typing import Iterable, Mapping, Union
from functools import lru_cache
from typing import Iterable, List, Mapping, Union

from amaranth import *
from amaranth.build import Platform
Expand Down Expand Up @@ -35,20 +35,24 @@ def __init__(self, name: str, elaboratable: Elaboratable) -> None:
port_width=1, port_flow=wiring.In, name="rst", port_name="rst", iface_name=""
)

def get_ports(self) -> list[WrapperPort]:
def get_ports(self) -> List[WrapperPort]:
"""Return a list of external ports."""
return self._flatten_hier(self.get_ports_hier())

def get_ports_hier(self) -> SignalMapping:
"""Maps elaboratable's Signature to a nested dictionary of WrapperPorts.
See _gather_signature_ports for more details.
"""
return self._gather_signature_ports(self.elaboratable.signature) | {
"clk": self.clk,
"rst": self.rst,
}
ports = self._gather_signature_ports(self.elaboratable.signature)
ports.update(
{
"clk": self.clk,
"rst": self.rst,
}
)
return ports

@cache
@lru_cache(maxsize=None)
def _cached_wrapper(
self, port_width: int, port_flow: wiring.Flow, name: str, port_name: str, iface_name: str
) -> WrapperPort:
Expand Down Expand Up @@ -136,7 +140,7 @@ def _flatten_hier(self, hier: SignalMapping) -> Iterable[Signal]:
ports += [hier]
return ports

def _connect_ports(self, ports: SignalMapping, iface: InterfaceLike) -> list[Assign]:
def _connect_ports(self, ports: SignalMapping, iface: InterfaceLike) -> List[Assign]:
"""Returns a list of amaranth assignments between the wrapped elaboratable and external ports.
:param ports: nested dictionary of WrapperPorts mirroring that of iface's signature
Expand Down
15 changes: 8 additions & 7 deletions tests/tests_build/test_elaboratable_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (C) 2023 Antmicro
# SPDX-License-Identifier: Apache-2.0

from typing import Callable, Union
from typing import Callable, List, Tuple, Union

import pytest
from amaranth import *
Expand Down Expand Up @@ -115,7 +115,7 @@ def nested_signature_mapping() -> SignalMapping:


@pytest.fixture
def flattened_nested_signature_mapping() -> list[WrapperPort]:
def flattened_nested_signature_mapping() -> List[WrapperPort]:
return [
WrapperPort(
bounds=[15, 0, 15, 0],
Expand Down Expand Up @@ -217,7 +217,7 @@ def clock_domain_signals(elaboratable_wrapper: ElaboratableWrapper) -> SignalMap
@pytest.fixture
def interface_connections(
elaboratable: Elaboratable, nested_signature_mapping: SignalMapping
) -> list[tuple[Signal, Signal]]:
) -> List[Tuple[Signal, Signal]]:
m = elaboratable
d = nested_signature_mapping
return [
Expand Down Expand Up @@ -269,7 +269,7 @@ def test_flatten_hier(
self,
elaboratable_wrapper: ElaboratableWrapper,
nested_signature_mapping: SignalMapping,
flattened_nested_signature_mapping: list[WrapperPort],
flattened_nested_signature_mapping: List[WrapperPort],
) -> None:
def ordering(p):
return p.name
Expand Down Expand Up @@ -299,12 +299,13 @@ def test_get_ports_hier(
clock_domain_signals: SignalMapping,
) -> None:
hier_ports = elaboratable_wrapper.get_ports_hier()
assert signal_mapping_eq(hier_ports, nested_signature_mapping | clock_domain_signals)
nested_signature_mapping.update(clock_domain_signals)
assert signal_mapping_eq(hier_ports, nested_signature_mapping)

def test_get_ports(
self,
elaboratable_wrapper: ElaboratableWrapper,
flattened_nested_signature_mapping: list[WrapperPort],
flattened_nested_signature_mapping: List[WrapperPort],
clock_domain_signals: SignalMapping,
) -> None:
def ordering(p):
Expand All @@ -322,7 +323,7 @@ def test_connect_ports(
elaboratable_wrapper: ElaboratableWrapper,
nested_signature_mapping: SignalMapping,
elaboratable: Elaboratable,
interface_connections: list[tuple[Signal, Signal]],
interface_connections: List[Tuple[Signal, Signal]],
) -> None:
conns_test = sorted(
elaboratable_wrapper._connect_ports(nested_signature_mapping, elaboratable),
Expand Down

0 comments on commit ecc30b8

Please sign in to comment.