Fix/issue 1282 mypy batch3 part2#1386
Conversation
Resolve type narrowing in majorana_operator string parsing and remove incorrect Optional on g2d2map factor parameter.
Single-line g2d2map signature per black; use BosonOperator | FermionOperator instead of typing.Union in special_operators.
Correct DualBasisElement scalar/bias/tensor types at the source and narrow coefficient/Op types in special_operators so strict mypy passes with full follow-imports, not only --follow-imports=skip.
There was a problem hiding this comment.
Code Review
This pull request modernizes type annotations across several files by adopting PEP 604 union types (|) and standard collection types. The review feedback correctly points out that because the project targets Python 3.9, evaluating these new union types at runtime will raise a TypeError unless from __future__ import annotations is imported. The reviewer suggests adding this future import, simplifying redundant union types (e.g., using float instead of float | int), and annotating function parameters directly to eliminate the need for runtime typing.cast calls which would otherwise fail.
…). Type the coefficient parameter directly in the signature so mypy no longer needs typing.cast for the type-1 Majorana branch.
|
Pushed a small hotfix based on suggestions, annotating Not applying the rest of the gemini suggestions:
Mypy + tests still pass locally on all 3 files. @mhucka let me know if I am missing something or if I should apply other suggestions too. |
Fix mypy errors in batch 3 part 2 (#1282)
Typing-only, no runtime changes.
hamiltonians/special_operators.py,majorana_operatorstring parsing types,coefficientparameter annotation,number_operatorreturn/Optypescontrib/representability/constraints/spin_orbital_2pos_constraints.py, fix incorrectOptionalong2d2mapfactorcontrib/representability/_dualbasis.py, correctscalar/bias/tensor_elementstypes (follow-imports dependency for spin_orbital constraints)hartree_fock.pyremains deferred, will do in next PR.Test plan
ignore_errors=false, full follow-imports) on all 3 filespytestonspecial_operators_test.py,spin_orbital_2pos_constraints_test.py,_dualbasis_test.py