Skip to content

Fixes mypy failure with latest mypy 1.16.0 #944

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

Merged
merged 6 commits into from
May 30, 2025

Conversation

woodsp-ibm
Copy link
Member

@woodsp-ibm woodsp-ibm commented May 29, 2025

Summary

Fixes for latest mypy errors in CI

Details and comments

Was erroring as follows

qiskit_machine_learning/neural_networks/effective_dimension.py:72: error: Unused "type: ignore" comment  [unused-ignore]
qiskit_machine_learning/neural_networks/effective_dimension.py:74: error: Unused "type: ignore" comment  [unused-ignore]
qiskit_machine_learning/neural_networks/effective_dimension.py:86: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[floating[_64Bit]]]", variable has type "None")  [assignment]
qiskit_machine_learning/neural_networks/effective_dimension.py:98: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[Any]]", variable has type "None")  [assignment]
qiskit_machine_learning/neural_networks/effective_dimension.py:112: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[floating[_64Bit]]]", variable has type "None")  [assignment]
qiskit_machine_learning/neural_networks/effective_dimension.py:124: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[Any]]", variable has type "None")  [assignment]
qiskit_machine_learning/neural_networks/effective_dimension.py:150: error: Need type annotation for "param_set"  [var-annotated]
qiskit_machine_learning/neural_networks/effective_dimension.py:334: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[floating[_64Bit]]]", variable has type "None")  [assignment]
qiskit_machine_learning/neural_networks/effective_dimension.py:349: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[Any]]", variable has type "None")  [assignment]
qiskit_machine_learning/kernels/algorithms/quantum_kernel_trainer.py:221: error: Incompatible types in assignment (expression has type "Union[Any, float]", variable has type "int")  [assignment]
Found 10 errors in 2 files (checked 188 source files)

The last one above, in quantum_kernel_trainer I fixed it by correcting the type of a field it was accessing when creating the result where the field in question is part of the results parent class VariationalResult in variational_algorithm. The optimal_value had a type mismatch between the getter and the setter.

@woodsp-ibm
Copy link
Member Author

woodsp-ibm commented May 29, 2025

It seems to pass ok with Python 3.9, which is what I have locally and what I tested with, but in other versions of Python there is an additional failure

qiskit_machine_learning/gradients/lin_comb/lin_comb_estimator_gradient.py:236: error: Incompatible types in assignment (expression has type "ndarray[tuple[int, ...], dtype[Any]]", variable has type "ndarray[tuple[int], dtype[Any]]")  [assignment]
Found 1 error in 1 file (checked 186 source files)

that needs correcting.

Update: for this I simply added a type ignore.
Well it seems that this is fine for > 3.9 but under 3.9 it ends up with an unused ignore error - sigh!

qiskit_machine_learning/gradients/lin_comb/lin_comb_estimator_gradient.py:236: error: Unused "type: ignore" comment [unused-ignore]

Finally I updated the ignore to cover both cases so it ignores the fact that its unused under 3.9 as well as covers the assignment type aspect for > 3.9.

@woodsp-ibm woodsp-ibm changed the title Fixes mypy Fixes mypy failure with latest mypy 1.16.0 May 29, 2025
@coveralls
Copy link

Pull Request Test Coverage Report for Build 15329190616

Details

  • 7 of 7 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 90.821%

Totals Coverage Status
Change from base Build 15279272116: 0.0%
Covered Lines: 4492
Relevant Lines: 4946

💛 - Coveralls

@edoaltamura edoaltamura merged commit 40fc9ab into qiskit-community:main May 30, 2025
16 checks passed
@woodsp-ibm woodsp-ibm deleted the fixes_mypy branch May 30, 2025 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants