Fix mypy annotations in fermion_partitioning (#1282)#1349
Conversation
First slice for quantumlib#1282: correct generator return types and local variable annotations in measurements/fermion_partitioning.py. No runtime or API behavior changes.
mhucka
left a comment
There was a problem hiding this comment.
Thank you for doing this!
There is one change I would recommend. Although Any is valid, it is better to define so-called generic parameter types, along the following lines:
from typing import TypeVar
T = TypeVar('T')
def pair_within(labels: list[T]) -> Generator[tuple[T, ...], None, None]:
"""Generates pairings of labels that contain each pair at least once."""
...Applying this across other matching functions (like pair_between and pair_within_simultaneously) allows static analyzers to check that the output tuples contain elements of the exact same type as the input.
Would you be able to try to make the appropriate changes to this PR?
Replace Any with generic T and a Pairing type alias that matches runtime: tuples of (T, T) pairs (plus occasional bare T leftovers). Internal iterator combiners stay on Any. No behavior changes.
|
@mhucka reworked based on suggestion, mypy is clean on this file and all tests pass. Let me know how it looks, and thanks for the tip. <3 |
mhucka
left a comment
There was a problem hiding this comment.
I'm not done with the review on the update, but here's a start for a minor detail.
Co-authored-by: Michael Hucka <mhucka@google.com>
Co-authored-by: Michael Hucka <mhucka@google.com>
Co-authored-by: Michael Hucka <mhucka@google.com>
Co-authored-by: Michael Hucka <mhucka@google.com>
Co-authored-by: Michael Hucka <mhucka@google.com>
Co-authored-by: Michael Hucka <mhucka@google.com>
Co-authored-by: Michael Hucka <mhucka@google.com>
After Pairing[T] annotations, strict mypy flagged the internal None sentinel branch. Use cast and isinstance narrowing so yielded types stay Pairing[T] without changing runtime behavior.
|
@mhucka applied all the |
mhucka
left a comment
There was a problem hiding this comment.
Looks good. Thank you for working on this so diligently!
|
@rosspeili Thank you for your work on this. I can't think of a reason to prefer any particular order, so it's up to you. |
First slice for #1282: correct generator return types and local variable annotations in measurements/fermion_partitioning.py. No runtime or API behavior changes.