Skip to content

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
@edoaltamura edoaltamura added stable backport potential The bug might be minimal and/or import enough to be port to stable labels Jun 16, 2025
mergify bot pushed a commit that referenced this pull request Jun 16, 2025
* Fix errors raised by latest  mypy (1.16.0)

* fix comment

* Added type ignore to avoid mypy failure

* Fix date that somehow I messed up

* Make ignore cover the 3.9 and > 3.9 cases

* Fix lint too long line (why was black ok with it?)

(cherry picked from commit 40fc9ab)
edoaltamura pushed a commit that referenced this pull request Jun 16, 2025
* Fix errors raised by latest  mypy (1.16.0)

* fix comment

* Added type ignore to avoid mypy failure

* Fix date that somehow I messed up

* Make ignore cover the 3.9 and > 3.9 cases

* Fix lint too long line (why was black ok with it?)

(cherry picked from commit 40fc9ab)

Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stable backport potential The bug might be minimal and/or import enough to be port to stable

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants