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 (utils) #8264

Merged
merged 1 commit into from
Sep 25, 2023
Merged

Fix mypy errors (utils) #8264

merged 1 commit into from
Sep 25, 2023

Conversation

Randl
Copy link
Contributor

@Randl Randl commented Jun 29, 2022

Summary

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

Details and comments

I've moved a setter and getter to be one after another (as per python/mypy#1465)

The are ~40 errors left:

All error list
qiskit/utils/measurement_error_mitigation.py:154: error: Argument "qubit_list" to "complete_meas_cal" has incompatible type "range"; expected "Optional[List[int]]"  [arg-type]
qiskit/utils/measurement_error_mitigation.py:158: error: Incompatible types in assignment (expression has type "List[List[int]]", variable has type "List[str]")  [assignment]
qiskit/utils/measurement_error_mitigation.py:174: error: Argument "qubit_list" to "complete_meas_cal" has incompatible type "range"; expected "Optional[List[int]]"  [arg-type]
qiskit/utils/measurement_error_mitigation.py:177: error: Incompatible types in assignment (expression has type "List[List[int]]", variable has type "List[str]")  [assignment]
qiskit/utils/measurement_error_mitigation.py:191: error: Incompatible return value type (got "Tuple[Union[QuantumCircuit, List[QuantumCircuit]], List[str], str]", expected "Tuple[QuantumCircuit, List[str], List[str]]")  [return-value]
qiskit/utils/measurement_error_mitigation.py:233: error: Argument "qubit_list" to "complete_meas_cal" has incompatible type "range"; expected "Optional[List[int]]"  [arg-type]
qiskit/utils/measurement_error_mitigation.py:236: error: Incompatible types in assignment (expression has type "List[List[int]]", variable has type "List[str]")  [assignment]
qiskit/utils/measurement_error_mitigation.py:253: error: Incompatible return value type (got "Tuple[Qobj, List[str], str]", expected "Tuple[QasmQobj, List[str], List[str]]")  [return-value]
qiskit/utils/mitigation/circuits.py:94: error: Incompatible types in assignment (expression has type "QuantumRegister", variable has type "Union[int, List[Any], None]")  [assignment]
qiskit/utils/mitigation/circuits.py:97: error: Incompatible types in assignment (expression has type "QuantumRegister", variable has type "Union[int, List[Any], None]")  [assignment]
qiskit/utils/mitigation/circuits.py:100: error: Incompatible types in assignment (expression has type "range", variable has type "Optional[List[int]]")  [assignment]
qiskit/utils/mitigation/circuits.py:100: error: Argument 1 to "len" has incompatible type "Union[int, List[Any]]"; expected "Sized"  [arg-type]
qiskit/utils/mitigation/circuits.py:103: error: Incompatible types in assignment (expression has type "ClassicalRegister", variable has type "Union[int, List[Any], None]")  [assignment]
qiskit/utils/mitigation/circuits.py:184: error: Incompatible types in assignment (expression has type "range", variable has type "List[int]")  [assignment]
qiskit/utils/mitigation/circuits.py:210: error: Argument 1 to "QuantumCircuit" has incompatible type "Union[QuantumRegister, Collection[Any]]"; expected "Union[Register, int, Sequence[Bit]]"  [arg-type]
qiskit/utils/mitigation/circuits.py:210: error: Argument 2 to "QuantumCircuit" has incompatible type "Union[ClassicalRegister, Collection[Any]]"; expected "Union[Register, int, Sequence[Bit]]"  [arg-type]
qiskit/utils/mitigation/circuits.py:220: error: Value of type "Union[QuantumRegister, Collection[Any]]" is not indexable  [index]
qiskit/utils/mitigation/circuits.py:224: error: Argument 1 to "barrier" of "QuantumCircuit" has incompatible type "Union[QuantumRegister, Collection[Any]]"; expected "Union[Qubit, QuantumRegister, int, slice, Sequence[Union[Qubit, int]]]"  [arg-type]
qiskit/utils/mitigation/circuits.py:231: error: Value of type "Union[QuantumRegister, Collection[Any]]" is not indexable  [index]
qiskit/utils/mitigation/circuits.py:231: error: Value of type "Union[ClassicalRegister, Collection[Any]]" is not indexable  [index]
qiskit/utils/run_circuits.py:58: error: "object" has no attribute "__iter__"; maybe "__str__" or "__dir__"? (not iterable)  [attr-defined]
qiskit/utils/run_circuits.py:85: error: "Job" has no attribute "status"  [attr-defined]
qiskit/utils/run_circuits.py:155: error: "Backend" has no attribute "configuration"  [attr-defined]
qiskit/utils/run_circuits.py:157: error: "Backend" has no attribute "max_circuits"  [attr-defined]
qiskit/utils/run_circuits.py:158: error: "Backend" has no attribute "max_circuits"  [attr-defined]
qiskit/utils/run_circuits.py:171: error: Argument 1 to "_safe_submit_circuits" has incompatible type "Union[List[CircuitInstruction], List[QuantumCircuit]]"; expected "Union[QuantumCircuit, List[QuantumCircuit]]"  [arg-type]
qiskit/utils/run_circuits.py:194: error: List item 0 has incompatible type "Union[QuantumCircuit, List[QuantumCircuit]]"; expected "Union[List[CircuitInstruction], List[QuantumCircuit]]"  [list-item]
qiskit/utils/run_circuits.py:197: error: "Backend" has no attribute "status"  [attr-defined]
qiskit/utils/run_circuits.py:202: error: "Backend" has no attribute "status"  [attr-defined]
qiskit/utils/run_circuits.py:220: error: "Job" has no attribute "queue_position"  [attr-defined]
qiskit/utils/run_circuits.py:231: error: "Job" has no attribute "result"  [attr-defined]
qiskit/utils/run_circuits.py:242: error: "Backend" has no attribute "retrieve_job"  [attr-defined]
qiskit/utils/run_circuits.py:259: error: "Job" has no attribute "error_message"  [attr-defined]
qiskit/utils/run_circuits.py:268: error: Argument 1 to "_safe_submit_circuits" has incompatible type "Union[List[CircuitInstruction], List[QuantumCircuit]]"; expected "Union[QuantumCircuit, List[QuantumCircuit]]"  [arg-type]
qiskit/utils/run_circuits.py:283: error: "Job" has no attribute "result"  [attr-defined]
qiskit/utils/run_circuits.py:325: error: "Job" has no attribute "job_id"  [attr-defined]
qiskit/utils/run_circuits.py:340: error: "Backend" has no attribute "jobs"  [attr-defined]
qiskit/utils/run_circuits.py:400: error: "Backend" has no attribute "options"  [attr-defined]
qiskit/utils/run_circuits.py:403: error: "Backend" has no attribute "run"  [attr-defined]

Large chunk of errors is due to Backend/Job not having properties. There is also set of errors due to casting in qiskit/utils/mitigation/circuits.py

@Randl Randl requested review from a team, manoelmarques and woodsp-ibm as code owners June 29, 2022 07:43
@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
@@ -60,6 +60,11 @@ def cal_matrix(self):
"""Return cal_matrix."""
return self._cal_matrix

@cal_matrix.setter
def cal_matrix(self, new_cal_matrix):
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason why this method definition is moved here? Looks like a merge conflict due to an old code to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reason is this: python/mypy#1465
It may be better just to leave a comment since its a mypy bug, but the change is cheap, so I just went for it (admittedly, I did it before I found out it was a bug)

qiskit/utils/units.py Outdated Show resolved Hide resolved
@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-10 branch 2 times, most recently from 03fadc0 to 2ed4b7d Compare May 23, 2023 14:31
@coveralls
Copy link

coveralls commented May 23, 2023

Pull Request Test Coverage Report for Build 5079651444

  • 48 of 56 (85.71%) changed or added relevant lines in 8 files are covered.
  • 19 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.008%) to 85.931%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/utils/measurement_error_mitigation.py 4 5 80.0%
qiskit/utils/mitigation/_filters.py 10 11 90.91%
qiskit/utils/run_circuits.py 3 4 75.0%
qiskit/utils/mitigation/fitters.py 13 15 86.67%
qiskit/utils/mitigation/circuits.py 6 9 66.67%
Files with Coverage Reduction New Missed Lines %
qiskit/extensions/quantum_initializer/squ.py 2 80.0%
crates/qasm2/src/lex.rs 5 90.63%
crates/qasm2/src/parse.rs 12 97.11%
Totals Coverage Status
Change from base Build 5073178665: -0.008%
Covered Lines: 71290
Relevant Lines: 82962

💛 - Coveralls

if pending:
category = PendingDeprecationWarning
category: Type[DeprecationWarning] | Type[
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be clearer to declare the type for category before the if pending line.

@@ -229,7 +228,7 @@ class TensoredFilter:
since="0.24.0",
additional_msg="For code migration guidelines, visit https://qisk.it/qi_migration.",
)
def __init__(self, cal_matrices: np.matrix, substate_labels_list: list, mit_pattern: list):
def __init__(self, cal_matrices: np.matrix, substate_labels_list: list[str], mit_pattern: list):
Copy link
Contributor

Choose a reason for hiding this comment

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

The docstring indicates that it's not just a list of strings:

a list of the states (as strings, states in the subspace)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I understand this means "as strings (states in the subspace)" not "as strings or states in the subspace"

@woodsp-ibm
Copy link
Member

Except for deprecation.py and units.py all the other files (Quantum Instance, run circuits that it uses, and the error mitigation it uses) are already deprecated and will be removed. I would say for these they need not be changed for that reason. These were used by the older algorithms (non-primitive) and opflow - the PR sorting mypy for opflow was just closed right for the same reason where it was all deprecated files - not saying to close this just that it can be reduced.

Copy link
Member

@woodsp-ibm woodsp-ibm left a comment

Choose a reason for hiding this comment

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

It looks fine to me.

@jakelishman jakelishman added the Changelog: None Do not include in changelog label Sep 25, 2023
@jakelishman jakelishman added this pull request to the merge queue Sep 25, 2023
Merged via the queue into Qiskit:main with commit 6e80e89 Sep 25, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog 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

8 participants