Skip to content

Commit

Permalink
typing for hv2ir get_notices() (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
dromer authored Dec 6, 2024
1 parent 4ee909b commit ec41869
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions hvcc/core/hv2ir/HeavyException.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from typing import Dict
from hvcc.types.compiler import CompilerNotif


class HeavyException(Exception):
Expand All @@ -24,4 +24,4 @@ class HeavyException(Exception):
def __init__(self, message: str = "") -> None:
super(Exception, self).__init__(message)
self.message = message
self.notes: Dict = {}
self.notes: CompilerNotif = CompilerNotif()
12 changes: 7 additions & 5 deletions hvcc/core/hv2ir/HeavyGraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from .HIrReceive import HIrReceive
from .HeavyLangObject import HeavyLangObject

from hvcc.types.compiler import CompilerNotif


class HeavyGraph(HeavyIrObject):
""" Represents a graph. Subclasses HeavyIrObject for functionality.
Expand Down Expand Up @@ -377,12 +379,12 @@ def get_output_channel_set(self, recursive: bool = False) -> set:
else:
return self.output_channel_set

def get_notices(self) -> Dict:
def get_notices(self) -> CompilerNotif:
notices = HeavyLangObject.get_notices(self)
for o in self.objs.values():
n = o.get_notices()
notices["warnings"].extend(n["warnings"])
notices["errors"].extend(n["errors"])
notices.warnings.extend(n.warnings)
notices.errors.extend(n.errors)
return notices

def get_objects_for_type(self, obj_type: str, recursive: bool = False) -> List:
Expand Down Expand Up @@ -460,8 +462,8 @@ def prepare(self) -> None:
self.assign_signal_buffers()
except HeavyException as e:
e.notes = self.get_notices()
e.notes["has_error"] = True
e.notes["exception"] = e
e.notes.has_error = True
e.notes.exception = e
raise e

def _remove_unused_inlet_connections(self) -> None:
Expand Down
21 changes: 12 additions & 9 deletions hvcc/core/hv2ir/HeavyLangObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from .Connection import Connection
from .HeavyException import HeavyException

from hvcc.types.compiler import CompilerMsg, CompilerNotif

if TYPE_CHECKING:
from .HeavyGraph import HeavyGraph
from .HeavyIrObject import HeavyIrObject
Expand Down Expand Up @@ -68,8 +70,8 @@ def __init__(
self.annotations: Dict = annotations or {}

# a list of locally generated warnings and errors (notifications)
self.warnings: List = []
self.errors: List = []
self.warnings: List[CompilerMsg] = []
self.errors: List[CompilerMsg] = []

# resolve arguments and fill in missing defaults for HeavyLang objects
self.__resolve_default_lang_args()
Expand Down Expand Up @@ -127,21 +129,22 @@ def name_for_arg(self, index: int = 0) -> str:
def add_warning(self, warning: str) -> None:
""" Add a warning to this object.
"""
self.warnings.append({"message": warning})
self.warnings.append(CompilerMsg(message=warning))

def add_error(self, error: str) -> None:
""" Add an error to this object and raise an exception.
"""
self.errors.append({"message": error})
self.errors.append(CompilerMsg(message=error))
raise HeavyException(error)

def get_notices(self) -> Dict:
def get_notices(self) -> CompilerNotif:
""" Returns a dictionary of all warnings and errors at this object.
"""
return {
"warnings": [{"message": f"{self}: {n['message']}"} for n in self.warnings],
"errors": [{"message": f"{self}: {n['message']}"} for n in self.errors],
}
return CompilerNotif(
has_error=len(self.errors) > 0,
warnings=[CompilerMsg(message=f"{self}: {n.message}") for n in self.warnings],
errors=[CompilerMsg(message=f"{self}: {n.message}") for n in self.errors],
)

@classmethod
def force_arg_type(cls, value: Any, value_type: Optional[str] = None, graph: Optional['HeavyGraph'] = None) -> Any:
Expand Down
4 changes: 2 additions & 2 deletions hvcc/core/hv2ir/HeavyParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ def graph_from_object(
if g.is_root_graph():
# add the notification dictionary at the top level
e.notes = g.get_notices()
e.notes["has_error"] = True
e.notes["exception"] = e
e.notes.has_error = True
e.notes.exception = e
raise e

if (g.graph is None) or (g.graph.file != g.file):
Expand Down
2 changes: 1 addition & 1 deletion hvcc/core/hv2ir/hv2ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def compile(
return CompilerResp(
stage="hv2ir",
compile_time=time.time() - tick, # record the total compile time
notifs=CompilerNotif(**hv_graph.get_notices()),
notifs=hv_graph.get_notices(),
in_file=os.path.basename(hv_file),
in_dir=os.path.dirname(hv_file),
out_file=os.path.basename(ir_file),
Expand Down

0 comments on commit ec41869

Please sign in to comment.