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 (primitives) #8263

Merged
merged 3 commits into from
Mar 29, 2023
Merged

Fix mypy errors (primitives) #8263

merged 3 commits into from
Mar 29, 2023

Conversation

Randl
Copy link
Contributor

@Randl Randl commented Jun 29, 2022

Summary

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

Details and comments

Previous content The are ~20 errors left: ``` qiskit/primitives/base_estimator.py:322: error: Invalid index type "Union[int, QuantumCircuit]" for "Tuple[QuantumCircuit, ...]"; expected type "SupportsIndex" [index] qiskit/primitives/base_estimator.py:342: error: Invalid index type "Union[int, QuantumCircuit]" for "Tuple[ParameterView, ...]"; expected type "SupportsIndex" [index] qiskit/primitives/base_estimator.py:345: error: Invalid index type "Union[int, QuantumCircuit]" for "Tuple[ParameterView, ...]"; expected type "SupportsIndex" [index] qiskit/primitives/base_estimator.py:349: error: Invalid index type "Union[int, QuantumCircuit]" for "Tuple[QuantumCircuit, ...]"; expected type "SupportsIndex" [index] qiskit/primitives/base_estimator.py:350: error: Invalid index type "Union[int, SparsePauliOp]" for "Tuple[SparsePauliOp, ...]"; expected type "SupportsIndex" [index] qiskit/primitives/base_estimator.py:358: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "Union[int, QuantumCircuit]" [type-var] qiskit/primitives/base_estimator.py:358: error: Unsupported operand types for <= ("int" and "QuantumCircuit") [operator] qiskit/primitives/base_estimator.py:358: note: Left operand is of type "Union[int, QuantumCircuit]" qiskit/primitives/base_estimator.py:360: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "Union[int, QuantumCircuit]" [type-var] qiskit/primitives/base_estimator.py:363: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "Union[int, SparsePauliOp]" [type-var] qiskit/primitives/base_estimator.py:363: error: Unsupported operand types for <= ("int" and "SparsePauliOp") [operator] qiskit/primitives/base_estimator.py:363: note: Left operand is of type "Union[int, SparsePauliOp]" qiskit/primitives/base_estimator.py:365: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "Union[int, SparsePauliOp]" [type-var] qiskit/primitives/base_estimator.py:370: error: Argument "circuits" to "_call" of "BaseEstimator" has incompatible type "List[Union[int, QuantumCircuit]]"; expected "Sequence[int]" [arg-type] qiskit/primitives/base_estimator.py:371: error: Argument "observables" to "_call" of "BaseEstimator" has incompatible type "List[Union[int, SparsePauliOp]]"; expected "Sequence[int]" [arg-type] qiskit/primitives/base_sampler.py:243: error: Invalid index type "Union[int, QuantumCircuit]" for "Tuple[QuantumCircuit, ...]"; expected type "SupportsIndex" [index] qiskit/primitives/base_sampler.py:258: error: Invalid index type "Union[int, QuantumCircuit]" for "Tuple[ParameterView, ...]"; expected type "SupportsIndex" [index] qiskit/primitives/base_sampler.py:261: error: Invalid index type "Union[int, QuantumCircuit]" for "Tuple[ParameterView, ...]"; expected type "SupportsIndex" [index] qiskit/primitives/base_sampler.py:264: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "Union[int, QuantumCircuit]" [type-var] qiskit/primitives/base_sampler.py:264: error: Unsupported operand types for <= ("int" and "QuantumCircuit") [operator] qiskit/primitives/base_sampler.py:264: note: Left operand is of type "Union[int, QuantumCircuit]" qiskit/primitives/base_sampler.py:266: error: Value of type variable "SupportsRichComparisonT" of "max" cannot be "Union[int, QuantumCircuit]" [type-var] qiskit/primitives/base_sampler.py:271: error: Argument "circuits" to "_call" of "BaseSampler" has incompatible type "List[Union[int, QuantumCircuit]]"; expected "Sequence[int]" [arg-type] ```

Large part of those errors can be fixed if the type cast is made explicit (I've added todo in corresponding places).

Edit (04.10.22): This fixes all remaining primitives errors.

@Randl Randl requested review from a team, ikkoham and t-imamichi as code owners June 29, 2022 07:36
@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
@rht
Copy link
Contributor

rht commented Oct 4, 2022

I'm not a qiskit-terra maintainer, so my review does not reflect of the maintainers' preference. But in general, you can use list, dict directly instead of having to import List, Dict. As stated, the oldest supported Python version is 3.7, and so it will work fine. This applies to the other Mypy PRs you have.

@rht
Copy link
Contributor

rht commented Jan 27, 2023

The content LGTM.

@1ucian0
Copy link
Member

1ucian0 commented Mar 21, 2023

Thanks @rht for your review!
@ElePT , do you mind also having a look?

@Randl Randl force-pushed the mypy-9 branch 2 times, most recently from 8480bc1 to 788f69d Compare March 21, 2023 15:24
@coveralls
Copy link

coveralls commented Mar 21, 2023

Pull Request Test Coverage Report for Build 4490399008

  • 4 of 4 (100.0%) changed or added relevant lines in 3 files are covered.
  • 5 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.004%) to 85.39%

Files with Coverage Reduction New Missed Lines %
qiskit/circuit/tools/pi_check.py 1 91.23%
qiskit/visualization/circuit/text.py 1 94.96%
qiskit/pulse/library/waveform.py 3 91.67%
Totals Coverage Status
Change from base Build 4474816332: -0.004%
Covered Lines: 68015
Relevant Lines: 79652

💛 - Coveralls

Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

Hi @Randl, thanks for opening this PR, I have a few general questions plus some more specific comments I have left in the code:

  1. I think that making sure we have correct typehints is very valuable, but given that we currently we don't run any mypy checks, I am not convinced about the value added by the constant use of cast (which I know, is sometimes the only way to please mypy without making significant changes in the code). My take here would be to add the typehints but not the casts, as they obfuscate the code and make it less readable.
  2. I see that you fixed the merge conflicts, but you also reverted your changes on base_sampler.py and base_estimator.py, was this intentional? I think that they should be included for completeness.

qiskit/primitives/base/base_primitive.py Outdated Show resolved Hide resolved
qiskit/primitives/base/base_primitive.py Outdated Show resolved Hide resolved
qiskit/primitives/base/base_primitive.py Outdated Show resolved Hide resolved
@Randl
Copy link
Contributor Author

Randl commented Mar 21, 2023

@ElePT I think that some code I annotated got removed, that's why it appears that some changes are reverted

Copy link
Contributor

@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

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

Great! I have a minor comment. I think Sequence is too broad here.

qiskit/primitives/backend_estimator.py Outdated Show resolved Hide resolved
qiskit/primitives/backend_estimator.py Outdated Show resolved Hide resolved
qiskit/primitives/backend_sampler.py Outdated Show resolved Hide resolved
@Randl Randl requested review from ikkoham and removed request for jyu00 March 22, 2023 11:38
Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>
Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

Thanks for applying the suggestions!!

@ElePT I think that some code I annotated got removed, that's why it appears that some changes are reverted

Sorry, I misread your comment earlier. The code did not get removed, it got moved, so those files are still part of the repo (instead of being primitives.base_estimator.py, now they are in primitives.base.base_estimator.py). Given that you had already looked at them, could you re-apply the changes?

@Randl
Copy link
Contributor Author

Randl commented Mar 23, 2023

I'm not sure what exactly you're reffering to. I've checked again and the remaining errors


qiskit/primitives/base/base_primitive.py:88: error: Incompatible types in assignment (expression has type "Tuple[Union[Sequence[float], float], ...]", variable has type "Union[Sequence[Sequence[float]], Sequence[float], float, None]")  [assignment]
qiskit/primitives/base/base_primitive.py:89: error: Item "Sequence[float]" of "Union[Sequence[float], float]" has no attribute "tolist"  [union-attr]
qiskit/primitives/base/base_primitive.py:89: error: Item "float" of "Union[Sequence[float], float]" has no attribute "tolist"  [union-attr]
qiskit/primitives/base/base_primitive.py:95: error: Incompatible types in assignment (expression has type "Tuple[Tuple[Union[Sequence[Sequence[float]], Sequence[float], float]]]", variable has type "Union[Sequence[Sequence[float]], Sequence[float], float, None]")  [assignment]
qiskit/primitives/base/base_primitive.py:99: error: Incompatible types in assignment (expression has type "Tuple[Union[Sequence[Sequence[float]], Sequence[float]]]", variable has type "Union[Sequence[Sequence[float]], Sequence[float], float, None]")  [assignment]
qiskit/primitives/base/base_primitive.py:105: error: Item "float" of "Union[Sequence[float], float]" has no attribute "__iter__" (not iterable)  [union-attr]
qiskit/primitives/base/base_primitive.py:109: error: Item "float" of "Union[Sequence[float], float]" has no attribute "__iter__" (not iterable)  [union-attr]

can't be fixed without type casting as far as I understand

Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

Never mind, I think that I was looking at an outdated version of the PR locally. LGTM.

@Randl
Copy link
Contributor Author

Randl commented Mar 24, 2023

@ikkoham can you approve please?

Copy link
Member

@t-imamichi t-imamichi left a comment

Choose a reason for hiding this comment

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

Thanks. LGTM

@t-imamichi t-imamichi added mod: primitives Related to the Primitives module mypy Work related to support improving type hints in Qiskti code labels Mar 24, 2023
@Randl
Copy link
Contributor Author

Randl commented Mar 28, 2023

So anything else required from me before it is merged?

@ElePT ElePT enabled auto-merge March 28, 2023 12:11
@ElePT
Copy link
Contributor

ElePT commented Mar 28, 2023

@ikkoham any further changes? if not, can you approve the requested changes?

Copy link
Contributor

@ikkoham ikkoham left a comment

Choose a reason for hiding this comment

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

Sorry for late. Thank you for achieving my comments. LGTM!

@ElePT Thank you for asking!

@ElePT ElePT added this pull request to the merge queue Mar 29, 2023
Merged via the queue into Qiskit:main with commit f0e90f2 Mar 29, 2023
ElePT pushed a commit to ElePT/qiskit that referenced this pull request Apr 5, 2023
* Fix primitives mypy errors

* Remove casts, fix types

* Apply suggestions from code review

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>

---------

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>
giacomoRanieri pushed a commit to giacomoRanieri/qiskit-terra that referenced this pull request Apr 16, 2023
* Fix primitives mypy errors

* Remove casts, fix types

* Apply suggestions from code review

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>

---------

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>
king-p3nguin pushed a commit to king-p3nguin/qiskit-terra that referenced this pull request May 22, 2023
* Fix primitives mypy errors

* Remove casts, fix types

* Apply suggestions from code review

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>

---------

Co-authored-by: Ikko Hamamura <ikkoham@users.noreply.github.com>
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 mod: primitives Related to the Primitives module 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

9 participants