Skip to content

Commit

Permalink
typing changes so overrides match
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-B committed Nov 29, 2023
1 parent 5f312d1 commit ba61255
Show file tree
Hide file tree
Showing 22 changed files with 66 additions and 51 deletions.
4 changes: 2 additions & 2 deletions pacman/model/graphs/abstract_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def label(self) -> Optional[str]:

@property
@abstractmethod
def pre_vertex(self) -> V:
def pre_vertex(self) -> AbstractVertex:
"""
The vertex at the start of the edge.
Expand All @@ -49,7 +49,7 @@ def pre_vertex(self) -> V:

@property
@abstractmethod
def post_vertex(self) -> V:
def post_vertex(self) -> AbstractVertex:
"""
The vertex at the end of the edge.
Expand Down
6 changes: 4 additions & 2 deletions pacman/model/graphs/abstract_edge_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
from .abstract_vertex import AbstractVertex
#: :meta private:
E = TypeVar("E", bound='AbstractEdge')
VERTEX_COLLECTION_TYPE = TypeVar(
"VERTEX_COLLECTION_TYPE", bound='Collection[AbstractVertex]')


class AbstractEdgePartition(Generic[E], metaclass=AbstractBase):
Expand Down Expand Up @@ -52,7 +54,7 @@ def __init__(self, identifier: str,
self._allowed_edge_types = allowed_edge_types
self._edges: OrderedSet[E] = OrderedSet()

def add_edge(self, edge: E):
def add_edge(self, edge: AbstractEdge):
"""
Add an edge to the edge partition.
Expand Down Expand Up @@ -119,7 +121,7 @@ def __contains__(self, edge):

@property
@abstractmethod
def pre_vertices(self) -> Collection[AbstractVertex]:
def pre_vertices(self) -> VERTEX_COLLECTION_TYPE:
"""
The vertices associated with this partition.
Expand Down
9 changes: 5 additions & 4 deletions pacman/model/graphs/abstract_multiple_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
from spinn_utilities.ordered_set import OrderedSet
from spinn_utilities.overrides import overrides
from pacman.exceptions import PacmanConfigurationException, PacmanValueError
from pacman.model.graphs import (
AbstractVertex, AbstractEdge, AbstractEdgePartition)
from pacman.model.graphs import (AbstractVertex, AbstractEdge)
from pacman.model.graphs.abstract_edge_partition import (
AbstractEdgePartition, VERTEX_COLLECTION_TYPE)
#: :meta private:
V = TypeVar("V", bound=AbstractVertex)
#: :meta private:
Expand Down Expand Up @@ -53,7 +54,7 @@ def __init__(
"There were clones in your list of acceptable pre vertices")

@overrides(AbstractEdgePartition.add_edge)
def add_edge(self, edge: E):
def add_edge(self, edge: AbstractEdge):
# safety checks
if edge.pre_vertex not in self._pre_vertices.keys():
raise PacmanValueError(
Expand All @@ -67,5 +68,5 @@ def add_edge(self, edge: E):

@property
@overrides(AbstractEdgePartition.pre_vertices)
def pre_vertices(self) -> Collection[V]:
def pre_vertices(self) -> VERTEX_COLLECTION_TYPE:
return self._pre_vertices.keys()
7 changes: 4 additions & 3 deletions pacman/model/graphs/abstract_single_source_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from spinn_utilities.abstract_base import AbstractBase
from spinn_utilities.overrides import overrides
from pacman.exceptions import PacmanConfigurationException
from pacman.model.graphs import AbstractEdgePartition
from pacman.model.graphs.abstract_edge_partition import (
AbstractEdgePartition, VERTEX_COLLECTION_TYPE)
if TYPE_CHECKING:
from .abstract_vertex import AbstractVertex # @UnusedImport
from .abstract_edge import AbstractEdge # @UnusedImport
Expand All @@ -44,7 +45,7 @@ def __init__(
self._pre_vertex = pre_vertex

@overrides(AbstractEdgePartition.add_edge)
def add_edge(self, edge: E):
def add_edge(self, edge: AbstractEdge):
super().add_edge(edge)
if edge.pre_vertex != self._pre_vertex:
raise PacmanConfigurationException(
Expand All @@ -61,5 +62,5 @@ def pre_vertex(self) -> V:

@property
@overrides(AbstractEdgePartition.pre_vertices)
def pre_vertices(self) -> Sequence[V]:
def pre_vertices(self) -> VERTEX_COLLECTION_TYPE:
return (self.pre_vertex, )
4 changes: 2 additions & 2 deletions pacman/model/graphs/abstract_virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
from typing import List, Optional, TYPE_CHECKING
from spinn_utilities.abstract_base import abstractmethod
from spinn_utilities.require_subclass import require_subclass
from spinn_machine import Machine
from spinn_machine.link_data_objects import AbstractLinkData
from pacman.model.graphs import AbstractVertex
if TYPE_CHECKING:
from spinn_utilities.typing.coords import XY
from spinn_machine import Machine
from spinn_machine.link_data_objects import AbstractLinkData
from pacman.model.routing_info import BaseKeyAndMask


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def __init__(self, machine_vertex: V, label: Optional[str],
super().remember_machine_vertex(machine_vertex)

@overrides(ApplicationVertex.remember_machine_vertex)
def remember_machine_vertex(self, machine_vertex: V):
def remember_machine_vertex(self, machine_vertex: MachineVertex):
assert (machine_vertex == self._machine_vertex)

@property
def machine_vertex(self) -> V:
def machine_vertex(self) -> MachineVertex:
"""
Provides access to the machine vertex at all times
Expand Down
4 changes: 2 additions & 2 deletions pacman/model/graphs/application/application_2d_fpga_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import List, Optional
from typing import List, Optional, Tuple
from spinn_utilities.overrides import overrides
from .application_fpga_vertex import ApplicationFPGAVertex
from pacman.model.graphs.application.abstract import Abstract2DDeviceVertex
Expand Down Expand Up @@ -90,7 +90,7 @@ def sub_height(self) -> int:

@property
@overrides(ApplicationFPGAVertex.atoms_shape)
def atoms_shape(self):
def atoms_shape(self) -> Tuple[int, ...]:
return (self.__width, self.__height)

@overrides(ApplicationFPGAVertex.get_incoming_slice_for_link)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional
from typing import Optional, Tuple
from spinn_utilities.overrides import overrides
from .application_spinnaker_link_vertex import ApplicationSpiNNakerLinkVertex
from pacman.model.graphs.application.abstract import Abstract2DDeviceVertex
Expand Down Expand Up @@ -88,7 +88,7 @@ def sub_height(self) -> int:

@property
@overrides(ApplicationSpiNNakerLinkVertex.atoms_shape)
def atoms_shape(self):
def atoms_shape(self) -> Tuple[int, ...]:
return (self.__width, self.__height)

@overrides(ApplicationSpiNNakerLinkVertex.get_incoming_slice)
Expand Down
4 changes: 2 additions & 2 deletions pacman/model/graphs/application/application_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ def label(self) -> Optional[str]:
return self._label

@property
@overrides(AbstractEdge.pre_vertex)
@overrides(AbstractEdge.pre_vertex, return_narrowing=True)
def pre_vertex(self) -> ApplicationVertex:
return self._pre_vertex

@property
@overrides(AbstractEdge.post_vertex)
@overrides(AbstractEdge.post_vertex, return_narrowing=True)
def post_vertex(self) -> ApplicationVertex:
return self._post_vertex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from typing import TYPE_CHECKING
from spinn_utilities.overrides import overrides
from pacman.model.graphs import AbstractSingleSourcePartition
from pacman.model.graphs import AbstractEdge
from .application_edge import ApplicationEdge
if TYPE_CHECKING:
from .application_vertex import ApplicationVertex
Expand All @@ -41,6 +42,6 @@ def __init__(self, identifier: str, pre_vertex: ApplicationVertex):
allowed_edge_types=ApplicationEdge)

@overrides(AbstractSingleSourcePartition.add_edge, return_narrowing=True)
def add_edge(self, edge: ApplicationEdge):
def add_edge(self, edge: AbstractEdge):
super().add_edge(edge)
edge.post_vertex.add_incoming_edge(edge, self)
3 changes: 2 additions & 1 deletion pacman/model/graphs/application/application_fpga_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ def outgoing_fpga_connection(self) -> Optional[FPGAConnection]:
"""
return self._outgoing_fpga_connection

@overrides(ApplicationVirtualVertex.get_outgoing_link_data)
@overrides(ApplicationVirtualVertex.get_outgoing_link_data,
return_narrowing=True)
def get_outgoing_link_data(self, machine: Machine) -> FPGALinkData:
fpga = self._outgoing_fpga_connection
if fpga is None:
Expand Down
2 changes: 1 addition & 1 deletion pacman/model/graphs/application/application_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def splitter(self, new_value: AbstractSplitterCommon[Self]):
self._splitter = new_value
self._splitter.set_governed_app_vertex(self)

def remember_machine_vertex(self, machine_vertex: MV):
def remember_machine_vertex(self, machine_vertex: MachineVertex):
"""
Adds the machine vertex to the iterable returned by machine_vertices
Expand Down
4 changes: 2 additions & 2 deletions pacman/model/graphs/common/mdslice.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def dimension(self) -> Tuple[slice, ...]:
def end(self) -> Tuple[int, ...]:
return tuple((numpy.array(self.start) + numpy.array(self.shape)) - 1)

@overrides(Slice.get_ids_as_slice_or_list)
@overrides(Slice.get_ids_as_slice_or_list, return_narrowing=True)
def get_ids_as_slice_or_list(self) -> numpy.ndarray:
return self.get_raster_ids()

Expand Down Expand Up @@ -132,7 +132,7 @@ def __hash__(self) -> int:

@classmethod
@overrides(Slice.from_string, extend_doc=False)
def from_string(cls, as_str: str) -> Union[MDSlice, Slice]:
def from_string(cls, as_str: str) -> Slice:
"""
Convert the string form of a :py:class:`MDSlice` into an object
instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
from typing import Generic, Optional, Type, TypeVar, cast
from pacman.model.graphs.machine import AbstractSDRAMPartition
from pacman.model.graphs import AbstractSingleSourcePartition
from pacman.model.graphs import AbstractEdge, AbstractSingleSourcePartition
from spinn_utilities.overrides import overrides
from pacman.exceptions import (
PacmanConfigurationException, PartitionMissingEdgesException,
Expand Down Expand Up @@ -49,7 +49,7 @@ def __init__(self, identifier: str, pre_vertex: V):
self._sdram_base_address: Optional[int] = None

@overrides(AbstractSingleSourcePartition.add_edge)
def add_edge(self, edge):
def add_edge(self, edge: AbstractEdge):
if self._sdram_size is None:
self._sdram_size = edge.sdram_size
elif self._sdram_size != edge.sdram_size:
Expand Down
8 changes: 5 additions & 3 deletions pacman/model/graphs/machine/machine_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ def __init__(self, pre_vertex: MachineVertex, post_vertex: MachineVertex,

@property
@overrides(AbstractEdge.label)
def label(self):
def label(self) -> Optional[str]:
return self._label

@property
@overrides(AbstractEdge.pre_vertex, extend_doc=False)
@overrides(
AbstractEdge.pre_vertex, extend_doc=False, return_narrowing=True)
def pre_vertex(self) -> MachineVertex:
"""
The vertex at the start of the edge.
Expand All @@ -65,7 +66,8 @@ def pre_vertex(self) -> MachineVertex:
return self._pre_vertex

@property
@overrides(AbstractEdge.post_vertex, extend_doc=False)
@overrides(
AbstractEdge.post_vertex, extend_doc=False, return_narrowing=True)
def post_vertex(self) -> MachineVertex:
"""
The vertex at the end of the edge.
Expand Down
6 changes: 4 additions & 2 deletions pacman/model/graphs/machine/machine_fpga_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
from __future__ import annotations
from typing import List, Optional, TYPE_CHECKING
from spinn_utilities.overrides import overrides
from spinn_machine import Machine
from spinn_machine.link_data_objects import FPGALinkData
from pacman.model.graphs import AbstractVirtual
from pacman.model.resources import ConstantSDRAM
from .machine_vertex import MachineVertex
from spinn_machine.link_data_objects import FPGALinkData

if TYPE_CHECKING:
from spinn_utilities.typing.coords import XY
from spinn_machine import Machine
from spinn_machine.link_data_objects import FPGALinkData
from pacman.model.graphs.application import ApplicationVertex
from pacman.model.graphs.common import Slice
from pacman.model.routing_info import BaseKeyAndMask
Expand Down
4 changes: 2 additions & 2 deletions pacman/model/graphs/machine/machine_spinnaker_link_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
from __future__ import annotations
from typing import List, Optional, TYPE_CHECKING
from spinn_utilities.overrides import overrides
from spinn_machine import Machine
from spinn_machine.link_data_objects import SpinnakerLinkData
from pacman.model.resources import ConstantSDRAM
from .machine_vertex import MachineVertex
from pacman.model.graphs import AbstractVirtual
if TYPE_CHECKING:
from spinn_utilities.typing.coords import XY
from spinn_machine import Machine
from spinn_machine.link_data_objects import SpinnakerLinkData
from pacman.model.graphs.application import ApplicationVertex
from pacman.model.graphs.common import Slice
from pacman.model.routing_info import BaseKeyAndMask
Expand Down
8 changes: 3 additions & 5 deletions pacman/model/graphs/machine/machine_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
from spinn_utilities.abstract_base import AbstractBase, abstractmethod
from spinn_utilities.overrides import overrides
from pacman.model.graphs import AbstractVertex
from pacman.model.graphs.common import Slice
from pacman.model.graphs.common import ChipAndCore, Slice
from pacman.model.resources import (
AbstractSDRAM, IPtagResource, ReverseIPtagResource)
from pacman.utilities.utility_calls import get_n_bits
if TYPE_CHECKING:
from pacman.model.graphs.application import ApplicationVertex
from pacman.model.resources import AbstractSDRAM
from pacman.model.resources import IPtagResource
from pacman.model.resources import ReverseIPtagResource
from pacman.model.graphs.common import ChipAndCore


class MachineVertex(AbstractVertex, metaclass=AbstractBase):
Expand Down
9 changes: 6 additions & 3 deletions pacman/model/graphs/machine/simple_machine_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Iterable
from .machine_vertex import MachineVertex
from spinn_utilities.overrides import overrides
from pacman.model.resources import (
AbstractSDRAM, IPtagResource, ReverseIPtagResource)


class SimpleMachineVertex(MachineVertex):
Expand Down Expand Up @@ -41,15 +44,15 @@ def __init__(self, sdram, label=None,

@property
@overrides(MachineVertex.sdram_required)
def sdram_required(self):
def sdram_required(self) -> AbstractSDRAM:
return self._sdram

@property
@overrides(MachineVertex.iptags)
def iptags(self):
def iptags(self) -> Iterable[IPtagResource]:
return self._iptags

@property
@overrides(MachineVertex.reverse_iptags)
def reverse_iptags(self):
def reverse_iptags(self) -> Iterable[ReverseIPtagResource]:
return self._reverse_iptags
8 changes: 5 additions & 3 deletions pacman/model/resources/constant_sdram.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Optional, TextIO
from spinn_utilities.overrides import overrides
from .abstract_sdram import AbstractSDRAM

Expand All @@ -35,7 +36,7 @@ def __init__(self, sdram: int):
self._sdram = int(sdram)

@overrides(AbstractSDRAM.get_total_sdram)
def get_total_sdram(self, n_timesteps) -> int: # @UnusedVariable
def get_total_sdram(self, n_timesteps: Optional[int]) -> int: # @UnusedVariable
return self._sdram

@property
Expand Down Expand Up @@ -65,7 +66,7 @@ def __sub__(self, other):
return other.sub_from(self)

@overrides(AbstractSDRAM.sub_from)
def sub_from(self, other):
def sub_from(self, other: AbstractSDRAM) -> AbstractSDRAM:
if isinstance(other, ConstantSDRAM):
return ConstantSDRAM(
other.fixed - self._sdram)
Expand All @@ -74,5 +75,6 @@ def sub_from(self, other):
return other - self

@overrides(AbstractSDRAM.report)
def report(self, timesteps, indent="", preamble="", target=None):
def report(self, timesteps: Optional[int], indent: str = "",
preamble: str = "", target: Optional[TextIO] = None):
print(indent, preamble, f"Constant {self._sdram} bytes", file=target)
Loading

0 comments on commit ba61255

Please sign in to comment.