Skip to content

Commit

Permalink
Mypy compliance
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-koch committed Nov 13, 2023
1 parent a5c88cc commit 1fa641c
Show file tree
Hide file tree
Showing 17 changed files with 57 additions and 43 deletions.
3 changes: 2 additions & 1 deletion zxlive/animations.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ def fuse(dragged: VItem, target: VItem, meet_halfway: bool = False) -> QAbstract
if not meet_halfway:
group.addAnimation(move(dragged, target=target.pos(), duration=100, ease=QEasingCurve(QEasingCurve.Type.OutQuad)))
else:
halfway_pos = (dragged.pos() + target.pos()) / 2
sum_pos = dragged.pos() + target.pos()
halfway_pos = QPointF(sum_pos.x() / 2, sum_pos.y() / 2)
group.addAnimation(move(dragged, target=halfway_pos, duration=100, ease=QEasingCurve(QEasingCurve.Type.OutQuad)))
group.addAnimation(move(target, target=halfway_pos, duration=100, ease=QEasingCurve(QEasingCurve.Type.OutQuad)))
group.addAnimation(scale(target, target=1, duration=100, ease=QEasingCurve(QEasingCurve.Type.InBack)))
Expand Down
2 changes: 1 addition & 1 deletion zxlive/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
if os.name == 'nt':
import ctypes
myappid = 'quantomatic.zxlive.zxlive.1.0.0' # arbitrary string
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid)
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid) # type: ignore


class ZXLive(QApplication):
Expand Down
3 changes: 2 additions & 1 deletion zxlive/base_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def deselect_all(self) -> None:
def copy_selection(self) -> GraphT:
selection = list(self.graph_scene.selected_vertices)
copied_graph = self.graph.subgraph_from_vertices(selection)
assert isinstance(copied_graph, GraphT)
# Mypy issue: https://github.com/python/mypy/issues/11673
assert isinstance(copied_graph, GraphT) # type: ignore
return copied_graph

def update_colors(self) -> None:
Expand Down
5 changes: 3 additions & 2 deletions zxlive/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from .common import ET, VT, W_INPUT_OFFSET, GraphT
from .graphview import GraphView
from .poly import Poly
from .proof import ProofModel, Rewrite


Expand Down Expand Up @@ -304,9 +305,9 @@ def redo(self) -> None:
class ChangePhase(BaseCommand):
"""Updates the phase of a spider."""
v: VT
new_phase: Union[Fraction, int]
new_phase: Union[Fraction, Poly, complex]

_old_phase: Optional[Union[Fraction, int]] = field(default=None, init=False)
_old_phase: Optional[Union[Fraction, Poly, complex]] = field(default=None, init=False)

def undo(self) -> None:
assert self._old_phase is not None
Expand Down
2 changes: 1 addition & 1 deletion zxlive/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def _get_synonyms(key: str, default: list[str]) -> list[str]:


def to_tikz(g: GraphT) -> str:
return pyzx.tikz.to_tikz(g)
return pyzx.tikz.to_tikz(g) # type: ignore

def from_tikz(s: str) -> GraphT:
try:
Expand Down
4 changes: 2 additions & 2 deletions zxlive/custom_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def from_json(cls, json_str: str) -> "CustomRule":
d = json.loads(json_str)
lhs_graph = GraphT.from_json(d['lhs_graph'])
rhs_graph = GraphT.from_json(d['rhs_graph'])
assert (isinstance(lhs_graph, GraphT) and
isinstance(rhs_graph, GraphT))
# Mypy issue: https://github.com/python/mypy/issues/11673
assert (isinstance(lhs_graph, GraphT) and isinstance(rhs_graph, GraphT)) # type: ignore
return cls(lhs_graph, rhs_graph, d['name'], d['description'])

def to_proof_action(self) -> "ProofAction":
Expand Down
12 changes: 6 additions & 6 deletions zxlive/editor_base_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ShapeType(Enum):

class DrawPanelNodeType(TypedDict):
text: str
icon: tuple[ShapeType, str]
icon: tuple[ShapeType, QColor]


def vertices_data() -> dict[VertexType.Type, DrawPanelNodeType]:
Expand All @@ -54,8 +54,8 @@ def vertices_data() -> dict[VertexType.Type, DrawPanelNodeType]:

def edges_data() -> dict[EdgeType.Type, DrawPanelNodeType]:
return {
EdgeType.SIMPLE: {"text": "Simple", "icon": (ShapeType.LINE, BLACK)},
EdgeType.HADAMARD: {"text": "Hadamard", "icon": (ShapeType.DASHED_LINE, HAD_EDGE_BLUE)},
EdgeType.SIMPLE: {"text": "Simple", "icon": (ShapeType.LINE, QColor(BLACK))},
EdgeType.HADAMARD: {"text": "Hadamard", "icon": (ShapeType.DASHED_LINE, QColor(HAD_EDGE_BLUE))},
}


Expand Down Expand Up @@ -211,7 +211,7 @@ def __init__(self, variable_types: dict[str, bool]) -> None:
self._variable_types = variable_types

self._widget = QWidget()
lpal = QApplication.palette("QListWidget")
lpal = QApplication.palette("QListWidget") # type: ignore
palette = QPalette()
palette.setBrush(QPalette.ColorRole.Window, lpal.base())
self._widget.setAutoFillBackground(True)
Expand Down Expand Up @@ -354,14 +354,14 @@ def populate_list_widget(list_widget: QListWidget,
list_widget.setCurrentRow(row)


def create_icon(shape: ShapeType, color: str) -> QIcon:
def create_icon(shape: ShapeType, color: QColor) -> QIcon:
icon = QIcon()
pixmap = QPixmap(64, 64)
pixmap.fill(Qt.GlobalColor.transparent)
painter = QPainter(pixmap)
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
painter.setPen(QPen(QColor(BLACK), 6))
painter.setBrush(QColor(color))
painter.setBrush(color)
if shape == ShapeType.CIRCLE:
painter.drawEllipse(4, 4, 56, 56)
elif shape == ShapeType.SQUARE:
Expand Down
1 change: 1 addition & 0 deletions zxlive/eitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: Opt
# By default, Qt draws a dashed rectangle around selected items.
# We have our own implementation to draw selected vertices, so
# we intercept the selected option here.
assert hasattr(option, "state")
option.state &= ~QStyle.StateFlag.State_Selected
super().paint(painter, option, widget)

Expand Down
3 changes: 2 additions & 1 deletion zxlive/graphscene.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ def update_graph(self, new: GraphT, select_new: bool = False) -> None:
self.removeItem(e_item)

new_g = diff.apply_diff(self.g)
assert isinstance(new_g, GraphT)
# Mypy issue: https://github.com/python/mypy/issues/11673
assert isinstance(new_g, GraphT) # type: ignore
self.g = new_g
# g now contains the new graph,
# but we still need to update the scene
Expand Down
6 changes: 4 additions & 2 deletions zxlive/graphview.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def mousePressEvent(self, e: QMouseEvent) -> None:
elif self.tool == GraphTool.MagicWand:
pos = self.mapToScene(e.pos())
shift = e.modifiers() & Qt.KeyboardModifier.ShiftModifier
self.wand_trace = WandTrace(pos, shift)
self.wand_trace = WandTrace(pos, bool(shift))
self.wand_path = QGraphicsPathItem()
self.graph_scene.addItem(self.wand_path)
pen = QPen(QColor(WAND_COLOR), WAND_WIDTH)
Expand Down Expand Up @@ -302,6 +302,7 @@ def mousePressEvent(self, e: QMouseEvent) -> None:


class Sparkles(QObject):

def __init__(self, graph_scene: GraphScene) -> None:
super().__init__()
self.graph_scene = graph_scene
Expand All @@ -314,7 +315,7 @@ def __init__(self, graph_scene: GraphScene) -> None:
vx = speed * math.cos(angle) / SPARKLE_STEPS
vy = speed * math.sin(angle) / SPARKLE_STEPS
self.sparkle_deltas.append((vx, vy))
self.timer_id = None
self.timer_id: Optional[int] = None

def emit_sparkles(self, pos: QPointF, mult: int) -> None:
if not self.timer_id:
Expand All @@ -335,6 +336,7 @@ def timerEvent(self, event: QTimerEvent) -> None:
sparkle.timer_step()

def stop(self) -> None:
assert self.timer_id is not None
self.killTimer(self.timer_id)
self.timer_id = None
for sparkle in reversed(self.sparkles):
Expand Down
4 changes: 2 additions & 2 deletions zxlive/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def _new_action(self, name: str, trigger: Callable, shortcut: QKeySequence | QKe
if not action.shortcuts():
action.setShortcut(alt_shortcut)
elif alt_shortcut not in action.shortcuts():
action.setShortcuts([shortcut, alt_shortcut])
action.setShortcuts([shortcut, alt_shortcut]) # type: ignore
return action

@property
Expand Down Expand Up @@ -501,7 +501,7 @@ def format_str(c: complex) -> str:
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
entry = QTableWidgetItem(format_str(matrix[i, j]))
entry.setFlags(entry.flags() & ~Qt.ItemIsEditable)
entry.setFlags(entry.flags() & ~Qt.ItemFlag.ItemIsEditable)
table.setItem(i, j, entry)
table.resizeColumnsToContents()
table.resizeRowsToContents()
Expand Down
2 changes: 1 addition & 1 deletion zxlive/parse_poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
parser='lalr',
maybe_placeholders=True)

class PolyTransformer(Transformer):
class PolyTransformer(Transformer[Poly]):
def __init__(self, new_var: Callable[[str], Poly]):
super().__init__()

Expand Down
9 changes: 6 additions & 3 deletions zxlive/proof.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def pop_rewrite(self) -> tuple[Rewrite, GraphT]:
def get_graph(self, index: int) -> GraphT:
"""Returns the grap at a given position in the proof."""
copy = self.graphs[index].copy()
assert isinstance(copy, GraphT)
# Mypy issue: https://github.com/python/mypy/issues/11673
assert isinstance(copy, GraphT) # type: ignore
return copy

def to_json(self) -> str:
Expand All @@ -132,11 +133,13 @@ def from_json(json_str: str) -> "ProofModel":
"""Deserializes the model from JSON."""
d = json.loads(json_str)
initial_graph = GraphT.from_tikz(d["initial_graph"])
assert isinstance(initial_graph, GraphT)
# Mypy issue: https://github.com/python/mypy/issues/11673
assert isinstance(initial_graph, GraphT) # type: ignore
model = ProofModel(initial_graph)
for step in d["proof_steps"]:
rewrite = Rewrite.from_json(step)
rewritten_graph = rewrite.diff.apply_diff(model.graphs[-1])
assert isinstance(rewritten_graph, GraphT)
# Mypy issue: https://github.com/python/mypy/issues/11673
assert isinstance(rewritten_graph, GraphT) # type: ignore
model.add_rewrite(rewrite, rewritten_graph)
return model
2 changes: 1 addition & 1 deletion zxlive/proof_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def rule(g: GraphT, matches: list) -> pyzx.rules.RewriteOutputType[ET,VT]:
return ({}, [], [], True)
return rule

def _extract_circuit(graph, matches):
def _extract_circuit(graph: GraphT, matches: list) -> GraphT:
graph.auto_detect_io()
simplify.full_reduce(graph)
return extract_circuit(graph).to_graph()
Expand Down
14 changes: 8 additions & 6 deletions zxlive/proof_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(self, graph: GraphT, *actions: QAction) -> None:
self.graph_view.set_graph(graph)

self.actions_bar = QTabWidget(self)
self.layout().insertWidget(1, self.actions_bar)
self.layout().insertWidget(1, self.actions_bar) # type: ignore
self.init_action_groups()
self.actions_bar.currentChanged.connect(self.update_on_selection)

Expand Down Expand Up @@ -131,7 +131,7 @@ def init_action_groups(self) -> None:

widget = QWidget()
widget.setLayout(hlayout)
widget.action_group = group
setattr(widget, "action_group", group)
self.actions_bar.addTab(widget, group.name)

def parse_selection(self) -> tuple[list[VT], list[ET]]:
Expand All @@ -148,7 +148,8 @@ def parse_selection(self) -> tuple[list[VT], list[ET]]:
def update_on_selection(self) -> None:
selection, edges = self.parse_selection()
g = self.graph_scene.g
self.actions_bar.currentWidget().action_group.update_active(g, selection, edges)
action_group = getattr(self.actions_bar.currentWidget(), "action_group")
action_group.update_active(g, selection, edges)

def _vert_moved(self, vs: list[tuple[VT, float, float]]) -> None:
cmd = MoveNodeInStep(self.graph_view, vs, self.step_view)
Expand Down Expand Up @@ -244,7 +245,7 @@ def cross(a: QPointF, b: QPointF) -> float:
if not ok:
return False
try:
def new_var(_):
def new_var(_: str) -> Poly:
raise ValueError()
phase = string_to_complex(text) if phase_is_complex else string_to_fraction(text, new_var)
except ValueError:
Expand Down Expand Up @@ -367,7 +368,7 @@ def _proof_step_selected(self, selected: QItemSelection, deselected: QItemSelect
cmd = GoToRewriteStep(self.graph_view, self.step_view, deselected.first().topLeft().row(), selected.first().topLeft().row())
self.undo_stack.push(cmd)

def _refresh_rules(self):
def _refresh_rules(self) -> None:
self.actions_bar.removeTab(self.actions_bar.count() - 1)
custom_rules = []
for root, dirs, files in os.walk(get_custom_rules_path()):
Expand All @@ -386,7 +387,7 @@ def _refresh_rules(self):
hlayout.addStretch()
widget = QWidget()
widget.setLayout(hlayout)
widget.action_group = group
setattr(widget, "action_group", group)
self.actions_bar.addTab(widget, group.name)


Expand All @@ -406,6 +407,7 @@ class ProofStepItemDelegate(QStyledItemDelegate):

def paint(self, painter: QPainter, option: QStyleOptionViewItem, index: Union[QModelIndex, QPersistentModelIndex]) -> None:
painter.save()
assert hasattr(option, "state") and hasattr(option, "rect") and hasattr(option, "font")

# Draw background
painter.setPen(Qt.GlobalColor.transparent)
Expand Down
27 changes: 14 additions & 13 deletions zxlive/settings_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@
}

class SettingsDialog(QDialog):
def __init__(self, parent: MainWindow) -> None:
super().__init__(parent)
self.parent = parent
def __init__(self, main_window: MainWindow) -> None:
super().__init__(main_window)
self.main_window = main_window
self.setWindowTitle("Settings")
self.settings = QSettings("zxlive", "zxlive")
self.value_dict: Dict[str,QWidget] = {}
Expand Down Expand Up @@ -173,19 +173,18 @@ def __init__(self, parent: MainWindow) -> None:

def add_setting(self,form:QFormLayout, name:str, label:str, ty:str, data:Any=None) -> None:
val = self.settings.value(name)
widget: QWidget
if val is None: val = defaults[name]
if ty == 'str':
widget = QLineEdit()
val = str(val)
widget.setText(val)
elif ty == 'int':
widget = QSpinBox()
val = int(val)
widget.setValue(val)
widget.setValue(int(val)) # type: ignore
elif ty == 'float':
widget = QDoubleSpinBox()
val = float(val)
widget.setValue(val)
widget.setValue(float(val)) # type: ignore
elif ty == 'folder':
widget = QWidget()
hlayout = QHBoxLayout()
Expand All @@ -194,10 +193,10 @@ def add_setting(self,form:QFormLayout, name:str, label:str, ty:str, data:Any=Non
val = str(val)
widget_line.setText(val)
def browse() -> None:
directory = QFileDialog.getExistingDirectory(self,"Pick folder",options=QFileDialog.ShowDirsOnly)
directory = QFileDialog.getExistingDirectory(self,"Pick folder",options=QFileDialog.Option.ShowDirsOnly)
if directory:
widget_line.setText(directory)
widget.text_value = directory
setattr(widget, "text_value", directory)
hlayout.addWidget(widget_line)
button = QPushButton("Browse")
button.clicked.connect(browse)
Expand All @@ -206,9 +205,9 @@ def browse() -> None:
widget = QComboBox()
val = str(val)
assert isinstance(data, dict)
widget.addItems(data.values())
widget.addItems(list(data.values()))
widget.setCurrentText(data[val])
widget.data = data
setattr(widget, "data", data)


form.addRow(label, widget)
Expand All @@ -231,8 +230,10 @@ def okay(self) -> None:
self.settings.setValue(name, widget.text_value)
set_pyzx_tikz_settings()
if self.settings.value("color-scheme") != self.prev_color_scheme:
colors.set_color_scheme(self.settings.value("color-scheme"))
self.parent.update_colors()
theme = self.settings.value("color-scheme")
assert isinstance(theme, str)
colors.set_color_scheme(theme)
self.main_window.update_colors()
self.accept()

def cancel(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions zxlive/vitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: Opt
# By default, Qt draws a dashed rectangle around selected items.
# We have our own implementation to draw selected vertices, so
# we intercept the selected option here.
assert hasattr(option, "state")
option.state &= ~QStyle.StateFlag.State_Selected
super().paint(painter, option, widget)

Expand Down

0 comments on commit 1fa641c

Please sign in to comment.