From 9575125dde4aad4dbbdb4d80d440606f7eeeae76 Mon Sep 17 00:00:00 2001 From: John van de Wetering Date: Wed, 15 Nov 2023 11:38:51 +0000 Subject: [PATCH] Make mypy happy with Python 3.8 --- pyzx/symbolic.py | 38 +++++++++++++++++++------------------- setup.py | 2 +- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/pyzx/symbolic.py b/pyzx/symbolic.py index a0d16466..152b8889 100644 --- a/pyzx/symbolic.py +++ b/pyzx/symbolic.py @@ -21,7 +21,7 @@ Lark is used to define a parser that can translate a string into a Poly. """ -from typing import Any, Callable, Union, Optional +from typing import Any, Callable, Union, Optional, Dict, List, Tuple, Set from lark import Lark, Transformer from functools import reduce from operator import add, mul @@ -31,9 +31,9 @@ class Var: name: str _is_bool: bool - _types_dict: Optional[Union[bool, dict[str, bool]]] + _types_dict: Optional[Union[bool, Dict[str, bool]]] - def __init__(self, name: str, data: Union[bool, dict[str, bool]]): + def __init__(self, name: str, data: Union[bool, Dict[str, bool]]): self.name = name if isinstance(data, dict): self._types_dict = data @@ -87,16 +87,16 @@ def __deepcopy__(self, _memo: object) -> 'Var': return self.__copy__() class Term: - vars: list[tuple[Var, int]] + vars: List[Tuple[Var, int]] - def __init__(self, vars: list[tuple[Var,int]]) -> None: + def __init__(self, vars: List[Tuple[Var,int]]) -> None: self.vars = vars def freeze(self) -> None: for var, _ in self.vars: var.freeze() - def free_vars(self) -> set[Var]: + def free_vars(self) -> Set[Var]: return set(var for var, _ in self.vars) def __repr__(self) -> str: @@ -132,7 +132,7 @@ def __lt__(self, other: 'Term') -> bool: if c1 != c2: return c1 < c2 return False - def substitute(self, var_map: dict[Var, Union[float, complex, 'Fraction']]) -> tuple[Union[float, complex, 'Fraction'], 'Term']: + def substitute(self, var_map: Dict[Var, Union[float, complex, 'Fraction']]) -> Tuple[Union[float, complex, 'Fraction'], 'Term']: """Substitute variables in the term with the given values. Returns a tuple of the coefficient and the new term. """ @@ -147,16 +147,16 @@ def substitute(self, var_map: dict[Var, Union[float, complex, 'Fraction']]) -> t class Poly: - terms: list[tuple[Union[int, float, complex, Fraction], Term]] + terms: List[Tuple[Union[int, float, complex, Fraction], Term]] - def __init__(self, terms: list[tuple[Union[int, float, complex, Fraction], Term]]) -> None: + def __init__(self, terms: List[Tuple[Union[int, float, complex, Fraction], Term]]) -> None: self.terms = terms def freeze(self) -> None: for _, term in self.terms: term.freeze() - def free_vars(self) -> set[Var]: + def free_vars(self) -> Set[Var]: output = set() for _, term in self.terms: output.update(term.free_vars()) @@ -319,7 +319,7 @@ def denominator(self) -> int: def copy(self) -> 'Poly': return Poly([(c, t) for c, t in self.terms]) - def substitute(self, var_map: dict[Var, Union[float, complex, 'Fraction']]) -> 'Poly': + def substitute(self, var_map: Dict[Var, Union[float, complex, 'Fraction']]) -> 'Poly': """Substitute variables in the polynomial with the given values.""" p = Poly([]) for c, t in self.terms: @@ -327,7 +327,7 @@ def substitute(self, var_map: dict[Var, Union[float, complex, 'Fraction']]) -> ' p += Poly([(c * coeff, term)]) return p -def new_var(name: str, types_dict: Union[bool, dict[str, bool]]) -> Poly: +def new_var(name: str, types_dict: Union[bool, Dict[str, bool]]) -> Poly: return Poly([(1, Term([(Var(name, types_dict), 1)]))]) def new_const(coeff: Union[int, Fraction]) -> Poly: @@ -358,26 +358,26 @@ def __init__(self, new_var: Callable[[str], Poly]): self._new_var = new_var - def start(self, items: list[Poly]) -> Poly: + def start(self, items: List[Poly]) -> Poly: return reduce(add, items) - def term(self, items: list[Poly]) -> Poly: + def term(self, items: List[Poly]) -> Poly: return reduce(mul, items) - def var(self, items: list[Any]) -> Poly: + def var(self, items: List[Any]) -> Poly: v = str(items[0]) return self._new_var(v) - def pi(self, _: list[Any]) -> Poly: + def pi(self, _: List[Any]) -> Poly: return new_const(1) - def intf(self, items: list[Any]) -> Poly: + def intf(self, items: List[Any]) -> Poly: return new_const(int(items[0])) - def frac(self, items: list[Any]) -> Poly: + def frac(self, items: List[Any]) -> Poly: return new_const(Fraction(int(items[0]), int(items[1]))) - def pifrac(self, items: list[Any]) -> Poly: + def pifrac(self, items: List[Any]) -> Poly: numerator = int(items[0]) if items[0] else 1 return new_const(Fraction(numerator, int(items[2]))) diff --git a/setup.py b/setup.py index 510a2e8e..eb6121a9 100644 --- a/setup.py +++ b/setup.py @@ -39,7 +39,7 @@ "numpy>=1.14", "pyperclip>=1.8.1", "tqdm>=4.56.0", - "ipywidgets>=7.5,<8" + "ipywidgets>=7.5,<8", "lark~=1.1.7"], include_package_data=True, )