Skip to content

Commit

Permalink
python 3.9 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
akissinger committed Sep 28, 2024
1 parent 1eee635 commit 1812f9d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
9 changes: 6 additions & 3 deletions zxlive/custom_rule.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import json
from fractions import Fraction
from typing import TYPE_CHECKING, Callable, Optional, Sequence, Dict, Union
from typing import TYPE_CHECKING, Callable, Optional, Sequence, Dict, Union, Any

import networkx as nx
import numpy as np
Expand Down Expand Up @@ -175,8 +175,11 @@ def to_json(self) -> str:
})

@classmethod
def from_json(cls, json_str: str) -> "CustomRule":
d = json.loads(json_str)
def from_json(cls, json_str: Union[str,Dict[str,Any]]) -> "CustomRule":
if isinstance(json_str, str):
d = json.loads(json_str)
else:
d = json_str
lhs_graph = GraphT.from_json(d['lhs_graph'])
rhs_graph = GraphT.from_json(d['rhs_graph'])
# Mypy issue: https://github.com/python/mypy/issues/11673
Expand Down
10 changes: 5 additions & 5 deletions zxlive/proof.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from typing import TYPE_CHECKING, Any, NamedTuple, Optional, Union
from typing import TYPE_CHECKING, Any, NamedTuple, Optional, Union, Dict

if TYPE_CHECKING:
from .proof_panel import ProofPanel
Expand All @@ -24,7 +24,7 @@ class Rewrite(NamedTuple):
graph: GraphT # New graph after applying the rewrite
grouped_rewrites: Optional[list['Rewrite']] = None # Optional field to store the grouped rewrites

def to_dict(self) -> dict[str, Any]:
def to_dict(self) -> Dict[str, Any]:
"""Serializes the rewrite to Python dictionary."""
return {
"display_name": self.display_name,
Expand All @@ -38,7 +38,7 @@ def to_json(self) -> str:
return json.dumps(self.to_dict())

@staticmethod
def from_json(json_str: str|dict[str, Any]) -> "Rewrite":
def from_json(json_str: Union[str,Dict[str,Any]]) -> "Rewrite":
"""Deserializes the rewrite from JSON or Python dict."""
if isinstance(json_str, str):
d = json.loads(json_str)
Expand Down Expand Up @@ -190,7 +190,7 @@ def ungroup_steps(self, index: int) -> None:
self.createIndex(index + len(individual_steps), 0),
[])

def to_dict(self) -> dict[str,Any]:
def to_dict(self) -> Dict[str,Any]:
"""Serializes the model to Python dict."""
initial_graph = self.initial_graph.to_dict()
proof_steps = [step.to_dict() for step in self.steps]
Expand All @@ -205,7 +205,7 @@ def to_json(self) -> str:
return json.dumps(self.to_dict())

@staticmethod
def from_json(json_str: str|dict[str,Any]) -> "ProofModel":
def from_json(json_str: Union[str,Dict[str,Any]]) -> "ProofModel":
"""Deserializes the model from JSON or Python dict."""
if isinstance(json_str, str):
d = json.loads(json_str)
Expand Down

0 comments on commit 1812f9d

Please sign in to comment.