From d5ea6ef76a1b7fd2a7c7e37a928aa532a8d47f5e Mon Sep 17 00:00:00 2001 From: Laurent AJDNIK <83899250+LaurentAjdnik@users.noreply.github.com> Date: Wed, 22 Jun 2022 00:41:08 +0200 Subject: [PATCH] Fix type hinting errors (#133) --- .github/workflows/ci.yml | 17 --------- eng/psakefile.ps1 | 2 +- .../pyqir/evaluator/_nonadaptiveevaluator.py | 2 +- pyqir-parser/pyqir/parser/_parser.py | 37 +++++++++---------- 4 files changed, 20 insertions(+), 38 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e102b6d0..93e8efeb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -145,20 +145,3 @@ jobs: branch: gh-pages folder: docs/_build/html if: ${{ github.event_name == 'push' }} - mypy: - runs-on: "ubuntu-20.04" - needs: build - steps: - - uses: actions/checkout@v2 - with: - submodules: false - - - name: Download Build Artifacts - uses: actions/download-artifact@v2 - with: - name: manylinux-artifacts - path: target/wheels - - - name: Check type annotations and mypy stub files - run: ./build.ps1 -t checkmypy - shell: pwsh \ No newline at end of file diff --git a/eng/psakefile.ps1 b/eng/psakefile.ps1 index 01f94615..7b03d0e3 100644 --- a/eng/psakefile.ps1 +++ b/eng/psakefile.ps1 @@ -60,7 +60,7 @@ task manylinux -depends build-manylinux-container-image, run-manylinux-container task musllinux -depends build-musllinux-container-image, run-musllinux-container-image, run-examples-in-musl-containers -task checks -depends cargo-fmt, cargo-clippy +task checks -depends cargo-fmt, cargo-clippy, checkmypy task rebuild -depends qirlib, generator, evaluator, parser diff --git a/pyqir-evaluator/pyqir/evaluator/_nonadaptiveevaluator.py b/pyqir-evaluator/pyqir/evaluator/_nonadaptiveevaluator.py index 3df79d53..ddcd570d 100644 --- a/pyqir-evaluator/pyqir/evaluator/_nonadaptiveevaluator.py +++ b/pyqir-evaluator/pyqir/evaluator/_nonadaptiveevaluator.py @@ -2,7 +2,7 @@ # Licensed under the MIT License. from pyqir.evaluator._gateset import GateSet -from pyqir.evaluator._native import PyNonadaptiveJit +from pyqir.evaluator._native import PyNonadaptiveJit # type: ignore from typing import List, Optional diff --git a/pyqir-parser/pyqir/parser/_parser.py b/pyqir-parser/pyqir/parser/_parser.py index ab3d48a5..32e369d4 100644 --- a/pyqir-parser/pyqir/parser/_parser.py +++ b/pyqir-parser/pyqir/parser/_parser.py @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. -from pyqir.parser._native import ( +from pyqir.parser._native import ( # type: ignore PyQirModule, PyQirFunction, PyQirParameter, @@ -12,7 +12,7 @@ PyQirType, module_from_bitcode ) -from typing import List, Optional, Tuple +from typing import cast, List, Optional, Tuple __all__ = [ "QirType", @@ -587,7 +587,7 @@ def __new__(cls, instr: PyQirInstruction): def __init__(self, instr: PyQirInstruction): self.instr = instr - self._type = None + self._type: Optional[QirType] = None @property def output_name(self) -> Optional[str]: @@ -605,7 +605,7 @@ def type(self) -> QirType: """ if self._type == None: self._type = QirType(self.instr.type) - return self._type + return cast(QirType, self._type) class QirOpInstr(QirInstr): @@ -917,9 +917,9 @@ class QirBlock: def __init__(self, block: PyQirBasicBlock): self.block = block - self._instructions = None - self._terminator = None - self._phi_nodes = None + self._instructions: Optional[List[QirInstr]] = None + self._terminator: Optional[QirTerminator] = None + self._phi_nodes: Optional[List[QirPhiInstr]] = None @property def name(self) -> str: @@ -938,7 +938,7 @@ def instructions(self) -> List[QirInstr]: """ if self._instructions == None: self._instructions = [QirInstr(i) for i in self.block.instructions] - return self._instructions + return cast(List[QirInstr], self._instructions) @property def terminator(self) -> QirTerminator: @@ -948,7 +948,7 @@ def terminator(self) -> QirTerminator: """ if self._terminator == None: self._terminator = QirTerminator(self.block.terminator) - return self._terminator + return cast(QirTerminator, self._terminator) @property def phi_nodes(self) -> List[QirPhiInstr]: @@ -960,7 +960,7 @@ def phi_nodes(self) -> List[QirPhiInstr]: """ if self._phi_nodes == None: self._phi_nodes = [QirPhiInstr(i) for i in self.block.phi_nodes] - return self._phi_nodes + return cast(List[QirPhiInstr], self._phi_nodes) def get_phi_pairs_by_source_name(self, name: str) -> List[Tuple[str, QirOperand]]: """ @@ -979,7 +979,7 @@ class QirParameter: def __init__(self, param: PyQirParameter): self.param = param - self._type = None + self._type: Optional[QirType] = None @property def name(self) -> str: @@ -996,7 +996,7 @@ def type(self) -> QirType: """ if self._type == None: self._type = QirType(self.param.type) - return self._type + return cast(QirType, self._type) class QirFunction: @@ -1007,10 +1007,9 @@ class QirFunction: def __init__(self, func: PyQirFunction): self.func = func - self._parameters = None - self._parameters = None - self._return_type = None - self._blocks = None + self._parameters: Optional[List[QirParameter]] = None + self._return_type: Optional[QirType] = None + self._blocks: Optional[List[QirBlock]] = None @property def name(self) -> str: @@ -1026,7 +1025,7 @@ def parameters(self) -> List[QirParameter]: """ if self._parameters == None: self._parameters = [QirParameter(i) for i in self.func.parameters] - return self._parameters + return cast(List[QirParameter], self._parameters) @property def return_type(self) -> QirType: @@ -1035,7 +1034,7 @@ def return_type(self) -> QirType: """ if self._return_type == None: self._return_type = QirType(self.func.return_type) - return self._return_type + return cast(QirType, self._return_type) @property def blocks(self) -> List[QirBlock]: @@ -1044,7 +1043,7 @@ def blocks(self) -> List[QirBlock]: """ if self._blocks == None: self._blocks = [QirBlock(i) for i in self.func.blocks] - return self._blocks + return cast(List[QirBlock], self._blocks) @property def required_qubits(self) -> Optional[int]: