Skip to content

Commit

Permalink
typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-B committed Nov 13, 2024
1 parent dd5df50 commit c7c1409
Show file tree
Hide file tree
Showing 23 changed files with 75 additions and 70 deletions.
8 changes: 4 additions & 4 deletions pacman/model/graphs/abstract_edge_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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: E) -> None:
"""
Add an edge to the edge partition.
Expand Down Expand Up @@ -101,14 +101,14 @@ def n_edges(self) -> int:
"""
return len(self._edges)

def __repr__(self):
def __repr__(self) -> str:
return (f"{self.__class__.__name__}(identifier={self.identifier}"
f", n_edges={self.n_edges})")

def __str__(self):
def __str__(self) -> str:
return self.__repr__()

def __contains__(self, edge):
def __contains__(self, edge: AbstractEdge) -> bool:
"""
Check if the edge is contained within this partition.
Expand Down
2 changes: 1 addition & 1 deletion pacman/model/graphs/abstract_multiple_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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: E) -> None:
# safety checks
if edge.pre_vertex not in self._pre_vertices:
raise PacmanValueError(
Expand Down
2 changes: 1 addition & 1 deletion pacman/model/graphs/abstract_single_source_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
self._pre_vertex = pre_vertex

@overrides(AbstractEdgePartition.add_edge)
def add_edge(self, edge: E):
def add_edge(self, edge: E) -> None:
super().add_edge(edge)
if edge.pre_vertex != self._pre_vertex:
raise PacmanConfigurationException(
Expand Down
5 changes: 3 additions & 2 deletions pacman/model/graphs/abstract_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def label(self) -> Optional[str]:
"""
return self._label

def set_label(self, label: str):
def set_label(self, label: str) -> None:
"""
Changes the label for a vertex *not yet added* to a graph.
Expand Down Expand Up @@ -90,7 +90,8 @@ def get_fixed_location(self) -> Optional[ChipAndCore]:
"""
return self._fixed_location

def set_fixed_location(self, x: int, y: int, p: Optional[int] = None):
def set_fixed_location(
self, x: int, y: int, p: Optional[int] = None) -> None:
"""
Set the location where the vertex must be placed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ def __init__(self, identifier: str, pre_vertex: ApplicationVertex):
allowed_edge_types=ApplicationEdge)

@overrides(AbstractSingleSourcePartition.add_edge)
def add_edge(self, edge: ApplicationEdge):
def add_edge(self, edge: ApplicationEdge) -> None:
super().add_edge(edge)
edge.post_vertex.add_incoming_edge(edge, self)
31 changes: 17 additions & 14 deletions pacman/model/graphs/application/application_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ def __init__(
self._max_atoms_per_dimension_per_core: Optional[Tuple[int, ...]]
self._set_max_atoms_per_dimension_per_core(max_atoms_per_core)

def __str__(self):
return self.label
def __str__(self) -> str:
return str(self.label)

def __repr__(self):
def __repr__(self) -> str:
if self.get_fixed_location():
return (f"ApplicationVertex({self.label},"
f" at{self.get_fixed_location()})")
Expand All @@ -117,7 +117,7 @@ def splitter(self) -> AbstractSplitterCommon[Self]:
return s

@splitter.setter
def splitter(self, new_value: AbstractSplitterCommon[Self]):
def splitter(self, new_value: AbstractSplitterCommon[Self]) -> None:
"""
Sets the splitter object. Does not allow repeated settings.
Expand All @@ -134,7 +134,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: MV) -> None:
"""
Adds the machine vertex to the iterable returned by machine_vertices
Expand Down Expand Up @@ -202,7 +202,8 @@ def machine_vertices(self) -> Collection[MV]:
"""
return self._machine_vertices

def __check_atoms_per_core(self):
def __check_atoms_per_core(self) -> None:
assert self._max_atoms_per_dimension_per_core is not None
if (len(self._max_atoms_per_dimension_per_core) !=
len(self.atoms_shape)):
raise ValueError(
Expand Down Expand Up @@ -248,7 +249,7 @@ def get_max_atoms_per_dimension_per_core(self) -> Tuple[int, ...]:
return self._max_atoms_per_dimension_per_core

def _set_max_atoms_per_dimension_per_core(
self, new_value: Optional[Union[int, Tuple[int, ...]]]):
self, new_value: Optional[Union[int, Tuple[int, ...]]]) -> None:
"""
Set the maximum number of atoms per dimension per core.
Expand All @@ -273,7 +274,7 @@ def _set_max_atoms_per_dimension_per_core(
self._max_atoms_per_dimension_per_core = max_atoms_tuple

def set_max_atoms_per_dimension_per_core(
self, new_value: Optional[Union[int, Tuple[int, ...]]]):
self, new_value: Optional[Union[int, Tuple[int, ...]]]) -> None:
"""
Set the maximum number of atoms per dimension per core.
Expand Down Expand Up @@ -336,8 +337,8 @@ def get_fixed_key_and_mask(
# pylint: disable=unused-argument
return None

def add_incoming_edge(
self, edge: ApplicationEdge, partition: ApplicationEdgePartition):
def add_incoming_edge(self, edge: ApplicationEdge,
partition: ApplicationEdgePartition) -> None:
"""
Add an edge incoming to this vertex. This is ignored by default,
but could be used to track incoming edges, and/or report faults.
Expand All @@ -349,7 +350,8 @@ def add_incoming_edge(
~pacman.model.graphs.application.ApplicationEdgePartition
"""

def get_key_ordered_indices(self, indices=None):
def get_key_ordered_indices(
self, indices: Optional[numpy.ndarray] = None) -> numpy.ndarray:
"""
Get indices of the vertex in the order that atoms appear when the
vertex is split into cores as determined by max_atoms_per_core. When
Expand All @@ -373,7 +375,7 @@ def get_key_ordered_indices(self, indices=None):
atoms_per_core = self.get_max_atoms_per_dimension_per_core()
remainders = numpy.array(indices)
cum_per_core = 1
cum_cores_per_dim = 1
cum_cores_per_dim = 1.0
core_index = numpy.zeros(len(indices))
atom_index = numpy.zeros(len(indices))
for n in range(n_dims):
Expand Down Expand Up @@ -403,7 +405,8 @@ def get_key_ordered_indices(self, indices=None):
return ((core_index * self.get_max_atoms_per_core()) +
atom_index).astype(numpy.uint32)

def get_raster_ordered_indices(self, indices):
def get_raster_ordered_indices(
self, indices: numpy.ndarray) -> numpy.ndarray:
"""
Convert indices from key order to raster order.
Expand Down Expand Up @@ -441,7 +444,7 @@ def get_raster_ordered_indices(self, indices):

return global_index.astype(numpy.uint32)

def has_fixed_location(self):
def has_fixed_location(self) -> bool:
"""
Check if this vertex or any machine vertex has a fixed location.
Expand Down
6 changes: 3 additions & 3 deletions pacman/model/graphs/common/chip_and_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional
from typing import Any, Optional


class ChipAndCore(object):
Expand Down Expand Up @@ -72,12 +72,12 @@ def __repr__(self) -> str:
else:
return f"X:{self._x},Y:{self._y},P:{self._p}"

def __eq__(self, other) -> bool:
def __eq__(self, other: Any) -> bool:
if not isinstance(other, ChipAndCore):
return False
return (self._x, self._y, self._p) == (other.x, other.y, other.p)

def __ne__(self, other) -> bool:
def __ne__(self, other: Any) -> bool:
return not self.__eq__(other)

def __hash__(self) -> int:
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 @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from typing import Tuple, Union
from typing import Any, Tuple, Union
import numpy
from numpy.typing import NDArray
from spinn_utilities.overrides import overrides
Expand Down Expand Up @@ -119,7 +119,7 @@ def __str__(self) -> str:
value += f"({a_slice.start}:{a_slice.stop})"
return f"{self.lo_atom}{self._atoms_shape}{value}"

def __eq__(self, other) -> bool:
def __eq__(self, other: Any) -> bool:
if not isinstance(other, MDSlice):
return False
if not super().__eq__(other):
Expand Down
8 changes: 4 additions & 4 deletions pacman/model/graphs/common/slice.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 __future__ import annotations
from typing import Tuple, Union
from typing import Any, Tuple, Union
import numpy
from numpy.typing import NDArray
from pacman.exceptions import PacmanValueError, PacmanTypeError
Expand Down Expand Up @@ -170,10 +170,10 @@ def get_raster_ids(self) -> NDArray[numpy.integer]:
"""
return numpy.array(range(self._lo_atom, self._lo_atom + self._n_atoms))

def __str__(self):
def __str__(self) -> str:
return (f"({self.lo_atom}:{self.hi_atom})")

def __eq__(self, other):
def __eq__(self, other: Any) -> bool:
if not isinstance(other, Slice):
return False
if self._lo_atom != other.lo_atom:
Expand All @@ -185,7 +185,7 @@ def __eq__(self, other):
return False
return self._n_atoms == other.n_atoms

def __hash__(self):
def __hash__(self) -> int:
# Slices will generally only be hashed in sets for the same Vertex
return self._lo_atom

Expand Down
2 changes: 1 addition & 1 deletion pacman/model/graphs/machine/abstract_sdram_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def sdram_base_address(self) -> int:
raise NotImplementedError

@sdram_base_address.setter
def sdram_base_address(self, new_value: int):
def sdram_base_address(self, new_value: int) -> None:
raise NotImplementedError

@abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion pacman/model/graphs/machine/machine_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def post_vertex(self) -> MachineVertex:
"""
return self._post_vertex

def __repr__(self):
def __repr__(self) -> str:
return (
f"MachineEdge(pre_vertex={self._pre_vertex}, "
f"post_vertex={self._post_vertex}, label={self.label})")
6 changes: 3 additions & 3 deletions pacman/model/graphs/machine/machine_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,18 @@ def index(self) -> int:
return self._index if self._index is not None else 0

@index.setter
def index(self, value: int):
def index(self, value: int) -> None:
"""
The index into the collection of machine vertices for an
application vertex.
"""
self._index = value

def __str__(self):
def __str__(self) -> str:
_l = self.label
return self.__repr__() if _l is None else _l

def __repr__(self):
def __repr__(self) -> str:
if self.get_fixed_location():
return (f"MachineVertex({self.label}, "
f"at{self.get_fixed_location()})")
Expand Down
2 changes: 1 addition & 1 deletion pacman/model/graphs/machine/multicast_edge_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ def __init__(self, pre_vertex: MachineVertex, identifier: str):
pre_vertex=pre_vertex, identifier=identifier,
allowed_edge_types=MachineEdge)

def __repr__(self):
def __repr__(self) -> str:
return (f"MulticastEdgePartition(pre_vertex={self.pre_vertex},"
f" identifier={self.identifier})")
14 changes: 9 additions & 5 deletions pacman/model/graphs/machine/simple_machine_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

from spinn_utilities.overrides import overrides

from pacman.model.graphs.common import Slice
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
Expand All @@ -33,11 +35,12 @@ class SimpleMachineVertex(MachineVertex):
"""
__slots__ = ("_iptags", "_reverse_iptags", "_sdram")

def __init__(self, sdram, label=None,
app_vertex=None, vertex_slice=None,
iptags: Optional[Iterable[IPtagResource]] = None,
reverse_iptags: Optional[Iterable[ReverseIPtagResource]]
= None):
def __init__(
self, sdram: Optional[AbstractSDRAM], label:Optional[str]=None,
app_vertex: Optional[ApplicationVertex] = None,
vertex_slice: Optional[Slice]=None,
iptags: Optional[Iterable[IPtagResource]] = None,
reverse_iptags: Optional[Iterable[ReverseIPtagResource]] = None):
super().__init__(
label=label, app_vertex=app_vertex, vertex_slice=vertex_slice)
self._sdram = sdram
Expand All @@ -51,6 +54,7 @@ def __init__(self, sdram, label=None,
@property
@overrides(MachineVertex.sdram_required)
def sdram_required(self) -> AbstractSDRAM:
assert self._sdram is not None
return self._sdram

@property
Expand Down
4 changes: 2 additions & 2 deletions pacman/model/resources/abstract_sdram.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __eq__(self, other: Any) -> bool:

@abstractmethod
def report(self, timesteps: Optional[int], indent: str = "",
preamble: str = "", target: Optional[TextIO] = None):
preamble: str = "", target: Optional[TextIO] = None) -> None:
"""
Writes a description of this SDRAM to the target.
Expand All @@ -91,5 +91,5 @@ def short_str(self) -> str:
"""
raise NotImplementedError

def __str__(self):
def __str__(self) -> str:
return f"SDRAM:{self.short_str}"
2 changes: 1 addition & 1 deletion pacman/model/resources/constant_sdram.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __add__(self, other: AbstractSDRAM) -> AbstractSDRAM:

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

@property
Expand Down
10 changes: 5 additions & 5 deletions pacman/model/resources/iptag_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional
from typing import Any, List, Optional


class IPtagResource(object):
Expand Down Expand Up @@ -112,13 +112,13 @@ def get_value(self):
self._traffic_identifier
]

def __repr__(self):
def __repr__(self) -> str:
return (
f"IPTagResource(ip_address={self._ip_address}, port={self._port}, "
f"strip_sdp={self._strip_sdp}, tag={self._tag}, "
f"traffic_identifier={self._traffic_identifier})")

def __eq__(self, other):
def __eq__(self, other: Any) ->bool:
"""
For unit tests *only* so __hash__ and __eq__ pairing not done!
"""
Expand All @@ -128,10 +128,10 @@ def __eq__(self, other):
self._tag == other._tag and
self._traffic_identifier == other._traffic_identifier)

def __hash__(self):
def __hash__(self) -> int:
return hash((
self._ip_address, self._port, self._strip_sdp, self._tag,
self._traffic_identifier))

def __ne__(self, other):
def __ne__(self, other: Any) -> bool:
return not self.__eq__(other)
Loading

0 comments on commit c7c1409

Please sign in to comment.