Skip to content

Commit

Permalink
fixed lots of mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
RazinShaikh committed Aug 30, 2024
1 parent 3b4b9ab commit 46cce4e
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
4 changes: 3 additions & 1 deletion zxlive/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
4 changes: 2 additions & 2 deletions zxlive/graphview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)`,
Expand Down
9 changes: 7 additions & 2 deletions zxlive/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
11 changes: 6 additions & 5 deletions zxlive/proof.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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"""
Expand Down
12 changes: 8 additions & 4 deletions zxlive/rewrite_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 1 addition & 8 deletions zxlive/rewrite_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 46cce4e

Please sign in to comment.