From 46cce4e2a3515c7c72cb796144dd6ade69e81e82 Mon Sep 17 00:00:00 2001 From: Razin Shaikh Date: Fri, 30 Aug 2024 18:52:09 +0100 Subject: [PATCH] fixed lots of mypy errors --- zxlive/common.py | 4 +++- zxlive/graphview.py | 4 ++-- zxlive/mainwindow.py | 9 +++++++-- zxlive/proof.py | 11 ++++++----- zxlive/rewrite_action.py | 12 ++++++++---- zxlive/rewrite_data.py | 9 +-------- 6 files changed, 27 insertions(+), 22 deletions(-) diff --git a/zxlive/common.py b/zxlive/common.py index d24cc35..7955172 100644 --- a/zxlive/common.py +++ b/zxlive/common.py @@ -94,7 +94,9 @@ def to_tikz(g: GraphT) -> str: def from_tikz(s: str) -> Optional[GraphT]: try: - return pyzx.tikz.tikz_to_graph(s) + g = pyzx.tikz.tikz_to_graph(s, backend = 'multigraph') + assert isinstance(g, GraphT) + return g except Exception as e: from . import dialogs dialogs.show_error_msg("Tikz import error", f"Error while importing tikz: {e}") diff --git a/zxlive/graphview.py b/zxlive/graphview.py index 5d0541a..ad346c1 100644 --- a/zxlive/graphview.py +++ b/zxlive/graphview.py @@ -85,8 +85,8 @@ def __init__(self, graph_scene: GraphScene) -> None: # self.setResizeAnchor(QGraphicsView.ViewportAnchor.AnchorViewCenter) self.setResizeAnchor(QGraphicsView.ViewportAnchor.AnchorUnderMouse) #self.setDragMode(QGraphicsView.DragMode.ScrollHandDrag) # This has to be enabled based on keyboard shortcuts - self.setCacheMode(QGraphicsView.CacheModeFlag.CacheBackground); - self.setViewportUpdateMode(QGraphicsView.ViewportUpdateMode.BoundingRectViewportUpdate); + self.setCacheMode(QGraphicsView.CacheModeFlag.CacheBackground) + self.setViewportUpdateMode(QGraphicsView.ViewportUpdateMode.BoundingRectViewportUpdate) # We implement the rubberband logic ourselves. Note that there is also # the option to set `self.setDragMode(QGraphicsView.RubberBandDrag)`, diff --git a/zxlive/mainwindow.py b/zxlive/mainwindow.py index a4e7089..e3b61aa 100644 --- a/zxlive/mainwindow.py +++ b/zxlive/mainwindow.py @@ -47,6 +47,7 @@ from .rule_panel import RulePanel from .sfx import SFXEnum, load_sfx from .tikz import proof_to_tikz +from pyzx.graph.base import BaseGraph from pyzx.drawing import graphs_to_gif @@ -424,11 +425,15 @@ def handle_export_tikz_proof_action(self) -> bool: with open(path, "w") as f: f.write(proof_to_tikz(self.active_panel.proof_model)) return True - + def handle_export_gif_proof_action(self) -> bool: assert isinstance(self.active_panel, ProofPanel) path = export_gif_dialog(self) - graphs_to_gif(self.active_panel.proof_model.graphs(),path,1000) # 1000ms per frame + if path is None: + show_error_msg("Export failed", "Invalid path", parent=self) + return False + graphs: list[BaseGraph] = list(self.active_panel.proof_model.graphs()) + graphs_to_gif(graphs, path, 1000) # 1000ms per frame return True def cut_graph(self) -> None: diff --git a/zxlive/proof.py b/zxlive/proof.py index 64db6ca..c4e9e05 100644 --- a/zxlive/proof.py +++ b/zxlive/proof.py @@ -38,11 +38,13 @@ def from_json(json_str: str) -> "Rewrite": """Deserializes the rewrite from JSON.""" d = json.loads(json_str) grouped_rewrites = d.get("grouped_rewrites") + graph = GraphT.from_json(d["graph"]) + assert isinstance(graph, GraphT) return Rewrite( display_name=d.get("display_name", d["rule"]), # Old proofs may not have display names rule=d["rule"], - graph=GraphT.from_json(d["graph"]), + graph=graph, grouped_rewrites=[Rewrite.from_json(r) for r in grouped_rewrites] if grouped_rewrites else None ) @@ -136,12 +138,11 @@ def pop_rewrite(self, position: Optional[int] = None) -> tuple[Rewrite, GraphT]: def get_graph(self, index: int) -> GraphT: """Returns the graph at a given position in the proof.""" if index == 0: - return self.initial_graph.copy() + copy = self.initial_graph.copy() else: copy = self.steps[index-1].graph.copy() - # Mypy issue: https://github.com/python/mypy/issues/11673 - assert isinstance(copy, GraphT) # type: ignore - return copy + assert isinstance(copy, GraphT) + return copy def rename_step(self, index: int, name: str) -> None: """Change the display name""" diff --git a/zxlive/rewrite_action.py b/zxlive/rewrite_action.py index 8fa624b..6b13517 100644 --- a/zxlive/rewrite_action.py +++ b/zxlive/rewrite_action.py @@ -32,7 +32,7 @@ class RewriteAction: name: str matcher: Callable[[GraphT, Callable], list] - rule: Callable[[GraphT, list], pyzx.rules.RewriteOutputType[VT, ET]] + rule: Callable[[GraphT, list], pyzx.rules.RewriteOutputType[VT, ET]] | Callable[[GraphT, list], GraphT] match_type: MatchType tooltip_str: str picture_path: Optional[str] = field(default=None) @@ -88,11 +88,15 @@ def do_rewrite(self, panel: ProofPanel) -> None: panel.undo_stack.push(cmd, anim_before=anim_before, anim_after=anim_after) # TODO: Narrow down the type of the first return value. - def apply_rewrite(self, g: GraphT, matches: list) -> tuple[Any, list[VT]]: + def apply_rewrite(self, g: GraphT, matches: list) -> tuple[GraphT, list[VT]]: if self.returns_new_graph: - return self.rule(g, matches), [] + graph = self.rule(g, matches) + assert isinstance(graph, GraphT) + return graph, [] - etab, rem_verts, rem_edges, check_isolated_vertices = self.rule(g, matches) + rewrite = self.rule(g, matches) + assert isinstance(rewrite, tuple) and len(rewrite) == 4 + etab, rem_verts, rem_edges, check_isolated_vertices = rewrite g.remove_edges(rem_edges) g.remove_vertices(rem_verts) g.add_edge_table(etab) diff --git a/zxlive/rewrite_data.py b/zxlive/rewrite_data.py index 57b7ac4..bb31864 100644 --- a/zxlive/rewrite_data.py +++ b/zxlive/rewrite_data.py @@ -23,7 +23,7 @@ class RewriteData(TypedDict): text: str matcher: Callable[[GraphT, Callable], list] - rule: Callable[[GraphT, list], pyzx.rules.RewriteOutputType[VT, ET]] + rule: Callable[[GraphT, list], pyzx.rules.RewriteOutputType[VT, ET]] | Callable[[GraphT, list], GraphT] type: MatchType tooltip: str copy_first: NotRequired[bool] @@ -239,13 +239,6 @@ def ocm_rule(_graph: GraphT, _matches: list) -> pyzx.rules.RewriteOutputType[VT, "rule": apply_simplification(simplify.full_reduce), "type": MATCHES_VERTICES, }, - 'teleport_reduce': { - "text": "teleport reduce", - "tooltip": "teleport_reduce", - "matcher": const_true, - "rule": apply_simplification(simplify.teleport_reduce), - "type": MATCHES_VERTICES, - }, 'reduce_scalar': { "text": "reduce scalar", "tooltip": "reduce_scalar",