Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mypy errors (transpiler) #8266

Merged
merged 13 commits into from
May 22, 2023
Merged

Fix mypy errors (transpiler) #8266

merged 13 commits into from
May 22, 2023

Conversation

Randl
Copy link
Contributor

@Randl Randl commented Jun 29, 2022

Summary

Following discussion, I'm splitting #8187 by module.

Details and comments

There are some ignores added: part to assignment to non-existing attribute, and part to decorated properties. Both appear to be justified for me. There are also some common mistakes -- e.g., using np.array instead of np.ndarray in annotation.

The are ~90 errors left:

All error list

qiskit/transpiler/instruction_durations.py:79: error: "Backend" has no attribute "properties"  [attr-defined]
qiskit/transpiler/instruction_durations.py:84: error: "Backend" has no attribute "properties"  [attr-defined]
qiskit/transpiler/instruction_durations.py:90: error: "Backend" has no attribute "configuration"  [attr-defined]
qiskit/transpiler/instruction_durations.py:128: error: Incompatible types in assignment (expression has type "Tuple[Union[float, str, Iterable[float]], ...]", target has type "Tuple[str, Optional[Iterable[int]], float, Optional[Iterable[float]], str]")  [assignment]
qiskit/transpiler/instruction_durations.py:128: error: Incompatible types in assignment (expression has type "Tuple[Union[float, str, Iterable[float]], ...]", target has type "Tuple[str, Optional[Iterable[int]], float, Optional[Iterable[float]]]")  [assignment]
qiskit/transpiler/instruction_durations.py:128: error: Incompatible types in assignment (expression has type "Tuple[Union[float, str, Iterable[float]], ...]", target has type "Tuple[str, Optional[Iterable[int]], float, str]")  [assignment]
qiskit/transpiler/instruction_durations.py:128: error: Incompatible types in assignment (expression has type "Tuple[Union[float, str, Iterable[float]], ...]", target has type "Tuple[str, Optional[Iterable[int]], float]")  [assignment]
qiskit/transpiler/instruction_durations.py:130: error: Incompatible types in assignment (expression has type "Tuple[Union[float, str, Iterable[float]], ...]", target has type "Tuple[str, Optional[Iterable[int]], float, Optional[Iterable[float]], str]")  [assignment]
qiskit/transpiler/instruction_durations.py:130: error: Incompatible types in assignment (expression has type "Tuple[Union[float, str, Iterable[float]], ...]", target has type "Tuple[str, Optional[Iterable[int]], float, Optional[Iterable[float]]]")  [assignment]
qiskit/transpiler/instruction_durations.py:130: error: Incompatible types in assignment (expression has type "Tuple[Union[float, str, Iterable[float]], ...]", target has type "Tuple[str, Optional[Iterable[int]], float, str]")  [assignment]
qiskit/transpiler/instruction_durations.py:130: error: Incompatible types in assignment (expression has type "Tuple[Union[float, str, Iterable[float]], ...]", target has type "Tuple[str, Optional[Iterable[int]], float]")  [assignment]
qiskit/transpiler/instruction_durations.py:146: error: Need more than 4 values to unpack (5 expected)  [misc]
qiskit/transpiler/instruction_durations.py:146: error: Need more than 3 values to unpack (5 expected)  [misc]
qiskit/transpiler/instruction_durations.py:208: error: Item "int" of "Union[int, Qubit]" has no attribute "index"  [union-attr]
qiskit/transpiler/instruction_durations.py:211: error: Argument 2 to "_get" of "InstructionDurations" has incompatible type "Union[List[int], List[Qubit], List[Union[int, Qubit]]]"; expected "List[int]"  [arg-type]
qiskit/transpiler/instruction_durations.py:231: error: Incompatible types in assignment (expression has type "Tuple[str, Tuple[int, ...]]", variable has type "Tuple[str, Tuple[int, ...], Tuple[float, ...]]")  [assignment]
qiskit/transpiler/instruction_durations.py:236: error: Invalid index type "Tuple[str, Tuple[int, ...], Tuple[float, ...]]" for "Dict[Tuple[str, Tuple[int, ...]], Tuple[float, str]]"; expected type "Tuple[str, Tuple[int, ...]]"  [index]
qiskit/transpiler/instruction_durations.py:246: error: Incompatible types in assignment (expression has type "Union[float, ParameterExpression]", variable has type "float")  [assignment]
qiskit/transpiler/passes/basis/decompose.py:46: error: Incompatible types in assignment (expression has type "Union[Type[Gate], List[Type[Gate]], List[str], str, None]", variable has type "Type[Gate]")  [assignment]
qiskit/transpiler/passes/calibration/builders.py:236: error: Item "Instruction" of "Union[Instruction, Any]" has no attribute "channel"  [union-attr]
qiskit/transpiler/passes/calibration/builders.py:237: error: Item "Instruction" of "Union[Instruction, Any]" has no attribute "pulse"  [union-attr]
qiskit/transpiler/passes/calibration/builders.py:239: error: Item "Instruction" of "Union[Instruction, Any]" has no attribute "channel"  [union-attr]
qiskit/transpiler/passes/calibration/builders.py:258: error: Argument 1 to "rescale_cr_inst" of "RZXCalibrationBuilder" has incompatible type "Union[Instruction, Any]"; expected "Play"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:259: error: Argument 1 to "rescale_cr_inst" of "RZXCalibrationBuilder" has incompatible type "Union[Instruction, Any]"; expected "Play"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:272: error: Argument 1 to "insert" of "Schedule" has incompatible type "Union[int, ParameterExpression]"; expected "int"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:272: error: Argument 2 to "insert" of "Schedule" has incompatible type "Union[Schedule, ScheduleBlock]"; expected "Union[Schedule, Instruction]"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:280: error: Argument 2 to "insert" of "Schedule" has incompatible type "Union[Schedule, ScheduleBlock]"; expected "Union[Schedule, Instruction]"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:288: error: List item 0 has incompatible type "Union[int, ParameterExpression]"; expected "int"  [list-item]
qiskit/transpiler/passes/calibration/builders.py:289: error: List item 0 has incompatible type "Union[int, ParameterExpression]"; expected "int"  [list-item]
qiskit/transpiler/passes/calibration/builders.py:291: error: Argument 2 to "insert" of "Schedule" has incompatible type "Union[Schedule, ScheduleBlock]"; expected "Union[Schedule, Instruction]"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:292: error: Argument 2 to "insert" of "Schedule" has incompatible type "Union[Schedule, ScheduleBlock]"; expected "Union[Schedule, Instruction]"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:293: error: Argument 2 to "insert" of "Schedule" has incompatible type "Union[Schedule, ScheduleBlock]"; expected "Union[Schedule, Instruction]"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:294: error: Argument 2 to "insert" of "Schedule" has incompatible type "Union[Schedule, ScheduleBlock]"; expected "Union[Schedule, Instruction]"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:295: error: Argument 2 to "insert" of "Schedule" has incompatible type "Union[Schedule, ScheduleBlock]"; expected "Union[Schedule, Instruction]"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:296: error: Argument 2 to "insert" of "Schedule" has incompatible type "Union[Schedule, ScheduleBlock]"; expected "Union[Schedule, Instruction]"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:391: error: Item "Instruction" of "Union[Instruction, Any]" has no attribute "channel"  [union-attr]
qiskit/transpiler/passes/calibration/builders.py:407: error: Argument 1 to "filter" of "ScheduleBlock" has incompatible type "*List[Callable[[Tuple[int, Union[Schedule, Instruction]]], bool]]"; expected "List[Callable[..., Any]]"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:407: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
qiskit/transpiler/passes/calibration/builders.py:407: note: Consider using "Sequence" instead, which is covariant
qiskit/transpiler/passes/calibration/builders.py:408: error: Argument 1 to "filter" of "ScheduleBlock" has incompatible type "*List[Callable[[Tuple[int, Union[Schedule, Instruction]]], bool]]"; expected "List[Callable[..., Any]]"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:408: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
qiskit/transpiler/passes/calibration/builders.py:408: note: Consider using "Sequence" instead, which is covariant
qiskit/transpiler/passes/calibration/builders.py:411: error: Argument 1 to "rescale_cr_inst" of "RZXCalibrationBuilder" has incompatible type "Union[Instruction, Any]"; expected "Play"  [arg-type]
qiskit/transpiler/passes/calibration/builders.py:412: error: Argument 1 to "rescale_cr_inst" of "RZXCalibrationBuilder" has incompatible type "Union[Instruction, Any]"; expected "Play"  [arg-type]
qiskit/transpiler/passes/routing/algorithms/token_swapper.py:123: error: Incompatible types in assignment (expression has type "Iterator[List[int]]", variable has type "Generator[List[Tuple[int, int]], None, None]")  [assignment]
qiskit/transpiler/passes/routing/algorithms/token_swapper.py:123: error: Argument 1 to "take_until_zero" has incompatible type "Generator[List[Tuple[int, int]], None, None]"; expected "Iterable[List[int]]"  [arg-type]
qiskit/transpiler/passes/routing/algorithms/util.py:100: error: Argument 2 to "PermutationCircuit" has incompatible type "Dict[_V, Any]"; expected "Dict[Union[int, Qubit], Qubit]"  [arg-type]
qiskit/transpiler/passes/routing/commuting_2q_gate_routing/commuting_2q_block.py:37: error: Need type annotation for "qubits" (hint: "qubits: Set[<type>] = ...")  [var-annotated]
qiskit/transpiler/passes/routing/commuting_2q_gate_routing/commuting_2q_block.py:37: error: Need type annotation for "cbits" (hint: "cbits: Set[<type>] = ...")  [var-annotated]
qiskit/transpiler/passes/routing/commuting_2q_gate_routing/commuting_2q_gate_router.py:238: error: Need type annotation for "blocked_vertices" (hint: "blocked_vertices: Set[<type>] = ...")  [var-annotated]
qiskit/transpiler/passes/scheduling/alignments/align_measures.py:123: error: Argument 3 to "_check_alignment_required" has incompatible type "Type[Measure]"; expected "Union[Instruction, List[Instruction]]"  [arg-type]
qiskit/transpiler/passes/scheduling/alignments/align_measures.py:180: error: Argument 1 to "pad_with_delays" has incompatible type "Optional[Tuple[Qubit, ...]]"; expected "List[int]"  [arg-type]
qiskit/transpiler/passes/scheduling/alignments/pulse_gate_validation.py:82: error: Unsupported operand types for % ("ParameterExpression" and "int")  [operator]
qiskit/transpiler/passes/scheduling/alignments/pulse_gate_validation.py:82: note: Left operand is of type "Union[int, ParameterExpression]"
qiskit/transpiler/passes/scheduling/alignments/pulse_gate_validation.py:90: error: Unsupported operand types for > ("int" and "ParameterExpression")  [operator]
qiskit/transpiler/passes/scheduling/alignments/pulse_gate_validation.py:90: note: Left operand is of type "Union[int, ParameterExpression]"
qiskit/transpiler/passes/scheduling/padding/base_padding.py:123: error: Argument 4 to "_apply_scheduled_op" of "BasePadding" has incompatible type "Optional[Tuple[Qubit, ...]]"; expected "Union[Qubit, List[Qubit]]"  [arg-type]
qiskit/transpiler/passes/scheduling/padding/base_padding.py:123: error: Argument 5 to "_apply_scheduled_op" of "BasePadding" has incompatible type "Optional[Tuple[Clbit, ...]]"; expected "Union[Clbit, List[Clbit], None]"  [arg-type]
qiskit/transpiler/passes/scheduling/padding/dynamical_decoupling.py:273: error: "DAGNode" has no attribute "op"  [attr-defined]
qiskit/transpiler/passes/scheduling/padding/dynamical_decoupling.py:349: error: Incompatible types in assignment (expression has type "float", variable has type "int")  [assignment]
qiskit/transpiler/passes/synthesis/unitary_synthesis.py:282: error: Incompatible types in assignment (expression has type "Set[str]", target has type "Dict[Any, Any]")  [assignment]
qiskit/transpiler/passes/synthesis/unitary_synthesis.py:286: error: Incompatible types in assignment (expression has type "Optional[bool]", target has type "Dict[Any, Any]")  [assignment]
qiskit/transpiler/passes/synthesis/unitary_synthesis.py:288: error: Incompatible types in assignment (expression has type "Optional[bool]", target has type "Dict[Any, Any]")  [assignment]
qiskit/transpiler/passes/synthesis/unitary_synthesis.py:301: error: Incompatible types in assignment (expression has type "Optional[Target]", target has type "Dict[Any, Any]")  [assignment]
qiskit/transpiler/passes/synthesis/unitary_synthesis.py:325: error: Incompatible types in assignment (expression has type "Tuple[Optional[CouplingMap], List[int]]", target has type "Dict[Any, Any]")  [assignment]
qiskit/transpiler/passmanager.py:169: error: List item 0 has incompatible type "BasePass"; expected "TBasePass"  [list-item]
qiskit/transpiler/passmanager.py:455: error: Argument 1 of "append" is incompatible with supertype "PassManager"; supertype defines the argument type as "Union[BasePass, List[TBasePass]]"  [override]
qiskit/transpiler/passmanager.py:455: note: This violates the Liskov substitution principle
qiskit/transpiler/passmanager.py:455: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
qiskit/transpiler/passmanager.py:463: error: Argument 2 of "replace" is incompatible with supertype "PassManager"; supertype defines the argument type as "Union[BasePass, List[TBasePass]]"  [override]
qiskit/transpiler/passmanager.py:463: note: This violates the Liskov substitution principle
qiskit/transpiler/passmanager.py:463: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
qiskit/transpiler/preset_passmanagers/level1.py:208: error: Argument 1 to "append" of "PassManager" has incompatible type "object"; expected "Union[BasePass, List[<nothing>]]"  [arg-type]
qiskit/transpiler/preset_passmanagers/level1.py:250: error: Unsupported operand types for + ("List[TransformationPass]" and "List[AnalysisPass]")  [operator]
qiskit/transpiler/preset_passmanagers/level2.py:193: error: Argument 1 to "append" of "PassManager" has incompatible type "object"; expected "Union[BasePass, List[<nothing>]]"  [arg-type]
qiskit/transpiler/preset_passmanagers/level2.py:233: error: Unsupported operand types for + ("List[TransformationPass]" and "List[AnalysisPass]")  [operator]
qiskit/transpiler/preset_passmanagers/level3.py:178: error: Type variable "qiskit.transpiler.basepasses.TBasePass" is unbound  [valid-type]
qiskit/transpiler/preset_passmanagers/level3.py:178: note: (Hint: Use "Generic[TBasePass]" or "Protocol[TBasePass]" base class to bind "TBasePass" inside a class)
qiskit/transpiler/preset_passmanagers/level3.py:178: note: (Hint: Use "TBasePass" in function signature to bind "TBasePass" inside a function)
qiskit/transpiler/preset_passmanagers/level3.py:179: error: Type variable "qiskit.transpiler.basepasses.TBasePass" is unbound  [valid-type]
qiskit/transpiler/preset_passmanagers/level3.py:179: note: (Hint: Use "Generic[TBasePass]" or "Protocol[TBasePass]" base class to bind "TBasePass" inside a class)
qiskit/transpiler/preset_passmanagers/level3.py:179: note: (Hint: Use "TBasePass" in function signature to bind "TBasePass" inside a function)
qiskit/transpiler/preset_passmanagers/level3.py:214: error: Argument 1 to "append" of "PassManager" has incompatible type "object"; expected "Union[BasePass, List[<nothing>]]"  [arg-type]
qiskit/transpiler/synthesis/aqc/aqc.py:99: error: Argument "fun" to "minimize" of "Optimizer" has incompatible type "Callable[[ndarray[Any, Any]], float]"; expected "Callable[[Union[float, ndarray[Any, Any]]], float]"  [arg-type]
qiskit/transpiler/synthesis/aqc/aqc.py:101: error: Argument "jac" to "minimize" of "Optimizer" has incompatible type "Callable[[ndarray[Any, Any]], ndarray[Any, Any]]"; expected "Optional[Callable[[Union[float, ndarray[Any, Any]]], Union[float, ndarray[Any, Any]]]]"  [arg-type]
qiskit/transpiler/synthesis/aqc/aqc.py:104: error: Argument 1 to "build" of "ApproximateCircuit" has incompatible type "Union[float, ndarray[Any, Any], Any]"; expected "ndarray[Any, Any]"  [arg-type]
qiskit/transpiler/synthesis/aqc/cnot_unit_objective.py:79: error: Return type "SupportsFloat" of "objective" incompatible with return type "float" in supertype "ApproximatingObjective"  [override]
qiskit/transpiler/target.py:955: error: Incompatible types in assignment (expression has type "List[Dict[str, Any]]", target has type "None")  [assignment]
qiskit/transpiler/target.py:959: error: Incompatible types in assignment (expression has type "List[Dict[str, Any]]", target has type "Sequence[str]")  [assignment]

@Randl Randl requested review from a team, manoelmarques and woodsp-ibm as code owners June 29, 2022 08:02
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

One or more of the the following people are requested to review this:

@HuangJunye HuangJunye added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jun 29, 2022
@1ucian0
Copy link
Member

1ucian0 commented Jul 6, 2022

I'll take this one

@1ucian0 1ucian0 self-assigned this Jul 6, 2022
@coveralls
Copy link

coveralls commented Aug 8, 2022

Pull Request Test Coverage Report for Build 5013002168

  • 150 of 151 (99.34%) changed or added relevant lines in 27 files are covered.
  • 31 unchanged lines in 7 files lost coverage.
  • Overall coverage decreased (-0.04%) to 85.867%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/transpiler/passes/layout/disjoint_utils.py 3 4 75.0%
Files with Coverage Reduction New Missed Lines %
qiskit/extensions/quantum_initializer/squ.py 2 80.0%
crates/accelerate/src/vf2_layout.rs 3 94.74%
crates/accelerate/src/sabre_swap/layer.rs 4 97.32%
crates/qasm2/src/lex.rs 4 90.63%
qiskit/circuit/parameterexpression.py 5 87.84%
crates/qasm2/src/parse.rs 6 97.58%
qiskit/circuit/library/data_preparation/pauli_feature_map.py 7 90.59%
Totals Coverage Status
Change from base Build 4961195397: -0.04%
Covered Lines: 70853
Relevant Lines: 82515

💛 - Coveralls

@1ucian0 1ucian0 added the mypy Work related to support improving type hints in Qiskti code label Mar 30, 2023
@Randl Randl force-pushed the mypy-12 branch 2 times, most recently from e6b5e04 to 118debc Compare March 30, 2023 13:57
@Randl Randl requested a review from ElePT as a code owner April 4, 2023 07:53
@Randl
Copy link
Contributor Author

Randl commented Apr 4, 2023

@1ucian0 I've updated on current master

qiskit/transpiler/basepasses.py Outdated Show resolved Hide resolved
inst: Union[str, Instruction],
qubits: Union[int, List[int], Qubit, List[Qubit]],
inst: str
| qiskit.circuit.Instruction, # FIXME: https://github.com/sphinx-doc/sphinx/issues/4961
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sphinx seems to do the right thing currently https://qiskit.org/documentation/stubs/qiskit.transpiler.InstructionDurations.get.html

If this change is necessary for typechecking, I would prefer to leave it as it was. It looks confusing otherwise and just move the tech debt from one place to the other.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what is the problem, but even after reverting this particular change, the doc build fails with

2023-04-16T09:39:29.4167382Z /home/vsts/work/1/s/.tox/docs/lib/python3.9/site-packages/qiskit/transpiler/instruction_durations.py:docstring of qiskit.transpiler.instruction_durations.InstructionDurations.get:1: WARNING: more than one target found for cross-reference 'Instruction': qiskit.pulse.instructions.Instruction, qiskit.pulse.instructions.instruction.Instruction, qiskit.circuit.Instruction, qiskit.circuit.instruction.Instruction

not sure why.

qiskit/transpiler/instruction_durations.py Show resolved Hide resolved
qiskit/transpiler/layout.py Show resolved Hide resolved
Randl and others added 2 commits May 18, 2023 11:47
@1ucian0
Copy link
Member

1ucian0 commented May 22, 2023

Thanks for the patience! LGTM!

@1ucian0 1ucian0 added this pull request to the merge queue May 22, 2023
Merged via the queue into Qiskit:main with commit e1056ee May 22, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community PR PRs from contributors that are not 'members' of the Qiskit repo mypy Work related to support improving type hints in Qiskti code
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

5 participants