Skip to content
Snippets Groups Projects
Commit 45c7bb78 authored by Frans Skarman's avatar Frans Skarman :tropical_fish: Committed by Oscar Gustafsson
Browse files

Fix type errors

parent 7c45e4c3
No related branches found
No related tags found
1 merge request!176Name literals
Pipeline #89474 passed
......@@ -4,7 +4,6 @@ B-ASIC Core Operations Module.
Contains some of the most commonly used mathematical operations.
"""
from numbers import Number
from typing import Dict, Optional
from numpy import abs as np_abs
......@@ -13,6 +12,7 @@ from numpy import conjugate, sqrt
from b_asic.graph_component import Name, TypeName
from b_asic.operation import AbstractOperation
from b_asic.port import SignalSourceProvider
from b_asic.types import Num
class Constant(AbstractOperation):
......@@ -35,12 +35,12 @@ class Constant(AbstractOperation):
_execution_time = 0
def __init__(self, value: Number = 0, name: Name = Name("")):
def __init__(self, value: Num = 0, name: Name = ""):
"""Construct a Constant operation with the given value."""
super().__init__(
input_count=0,
output_count=1,
name=Name(name),
name=name,
latency_offsets={"out0": 0},
)
self.set_param("value", value)
......@@ -53,12 +53,12 @@ class Constant(AbstractOperation):
return self.param("value")
@property
def value(self) -> Number:
def value(self) -> Num:
"""Get the constant value of this operation."""
return self.param("value")
@value.setter
def value(self, value: Number) -> None:
def value(self, value: Num) -> None:
"""Set the constant value of this operation."""
self.set_param("value", value)
......@@ -257,7 +257,7 @@ class AddSub(AbstractOperation):
return a + b if self.is_add else a - b
@property
def is_add(self) -> Number:
def is_add(self) -> Num:
"""Get if operation is add."""
return self.param("is_add")
......@@ -582,7 +582,7 @@ class ConstantMultiplication(AbstractOperation):
def __init__(
self,
value: Number = 0,
value: Num = 0,
src0: Optional[SignalSourceProvider] = None,
name: Name = Name(""),
latency: Optional[int] = None,
......@@ -610,12 +610,12 @@ class ConstantMultiplication(AbstractOperation):
return a * self.param("value")
@property
def value(self) -> Number:
def value(self) -> Num:
"""Get the constant value of this operation."""
return self.param("value")
@value.setter
def value(self, value: Number) -> None:
def value(self, value: Num) -> None:
"""Set the constant value of this operation."""
self.set_param("value", value)
......@@ -714,7 +714,7 @@ class SymmetricTwoportAdaptor(AbstractOperation):
def __init__(
self,
value: Number = 0,
value: Num = 0,
src0: Optional[SignalSourceProvider] = None,
src1: Optional[SignalSourceProvider] = None,
name: Name = Name(""),
......@@ -743,12 +743,12 @@ class SymmetricTwoportAdaptor(AbstractOperation):
return b + tmp, a + tmp
@property
def value(self) -> Number:
def value(self) -> Num:
"""Get the constant value of this operation."""
return self.param("value")
@value.setter
def value(self, value: Number) -> None:
def value(self, value: Num) -> None:
"""Set the constant value of this operation."""
self.set_param("value", value)
......
......@@ -7,12 +7,9 @@ Contains the base for all components with an ID in a signal flow graph.
from abc import ABC, abstractmethod
from collections import deque
from copy import copy, deepcopy
from typing import Any, Dict, Generator, Iterable, Mapping, NewType, cast
from typing import Any, Dict, Generator, Iterable, Mapping, cast
Name = NewType("Name", str)
TypeName = NewType("TypeName", str)
GraphID = NewType("GraphID", str)
GraphIDNumber = NewType("GraphIDNumber", int)
from b_asic.types import GraphID, GraphIDNumber, Name, Num, TypeName
class GraphComponent(ABC):
......
This diff is collapsed.
......@@ -131,7 +131,7 @@ class AbstractPort(Port):
return self._latency_offset
@latency_offset.setter
def latency_offset(self, latency_offset: int):
def latency_offset(self, latency_offset: Optional[int]):
self._latency_offset = latency_offset
......
......@@ -5,7 +5,6 @@ Contains operations with special purposes that may be treated differently from
normal operations in an SFG.
"""
from numbers import Number
from typing import List, Optional, Sequence, Tuple
from b_asic.graph_component import Name, TypeName
......@@ -16,6 +15,7 @@ from b_asic.operation import (
MutableResultMap,
)
from b_asic.port import SignalSourceProvider
from b_asic.types import Name, Num, TypeName
class Input(AbstractOperation):
......@@ -28,12 +28,12 @@ class Input(AbstractOperation):
_execution_time = 0
def __init__(self, name: Name = Name("")):
def __init__(self, name: Name = ""):
"""Construct an Input operation."""
super().__init__(
input_count=0,
output_count=1,
name=Name(name),
name=name,
latency_offsets={"out0": 0},
)
self.set_param("value", 0)
......@@ -46,12 +46,12 @@ class Input(AbstractOperation):
return self.param("value")
@property
def value(self) -> Number:
def value(self) -> Num:
"""Get the current value of this input."""
return self.param("value")
@value.setter
def value(self, value: Number) -> None:
def value(self, value: Num) -> None:
"""Set the current value of this input."""
self.set_param("value", value)
......@@ -152,7 +152,7 @@ class Delay(AbstractOperation):
def __init__(
self,
src0: Optional[SignalSourceProvider] = None,
initial_value: Number = 0,
initial_value: Num = 0,
name: Name = Name(""),
):
"""Construct a Delay operation."""
......@@ -173,7 +173,7 @@ class Delay(AbstractOperation):
def current_output(
self, index: int, delays: Optional[DelayMap] = None, prefix: str = ""
) -> Optional[Number]:
) -> Optional[Num]:
if delays is not None:
return delays.get(
self.key(index, prefix), self.param("initial_value")
......@@ -183,13 +183,13 @@ class Delay(AbstractOperation):
def evaluate_output(
self,
index: int,
input_values: Sequence[Number],
input_values: Sequence[Num],
results: Optional[MutableResultMap] = None,
delays: Optional[MutableDelayMap] = None,
prefix: str = "",
bits_override: Optional[int] = None,
truncate: bool = True,
) -> Number:
) -> Num:
if index != 0:
raise IndexError(
f"Output index out of range (expected 0-0, got {index})"
......@@ -214,11 +214,11 @@ class Delay(AbstractOperation):
return value
@property
def initial_value(self) -> Number:
def initial_value(self) -> Num:
"""Get the initial value of this delay."""
return self.param("initial_value")
@initial_value.setter
def initial_value(self, value: Number) -> None:
def initial_value(self, value: Num) -> None:
"""Set the initial value of this delay."""
self.set_param("initial_value", value)
from typing import NewType, Union
# https://stackoverflow.com/questions/69334475/how-to-hint-at-number-types-i-e-subclasses-of-number-not-numbers-themselv
Num = Union[int, float, complex]
NumRuntime = (complex, float, int)
Name = str
# # We want to be able to initialize Name with String literals, but still have the
# # benefit of static type checking that we don't pass non-names to name locations.
# # However, until python 3.11 a string literal type was not available. In those versions,
# # we'll fall back on just aliasing `str` => Name.
# if sys.version_info >= (3, 11):
# from typing import LiteralString
# Name: TypeAlias = NewType("Name", str) | LiteralString
# else:
# Name = str
TypeName = NewType("TypeName", str)
GraphID = NewType("GraphID", str)
GraphIDNumber = NewType("GraphIDNumber", int)
......@@ -318,7 +318,9 @@ class TestIOCoordinates:
bfly = Butterfly()
bfly.set_latency_offsets({"in0": 3, "out1": 5})
with pytest.raises(ValueError, match="All latencies must be set:"):
with pytest.raises(
ValueError, match="Missing latencies for inputs \\[1\\]"
):
bfly.get_io_coordinates()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment