Skip to content

Enable type-checking for random_objects.py - #369

Merged
thierry-martinez merged 12 commits into
TeamGraphix:masterfrom
thierry-martinez:typing_random_objects
Nov 28, 2025
Merged

Enable type-checking for random_objects.py#369
thierry-martinez merged 12 commits into
TeamGraphix:masterfrom
thierry-martinez:typing_random_objects

Conversation

@thierry-martinez

Copy link
Copy Markdown
Collaborator

This commit adds type annotations to random_objects.py, along with its corresponding tests, enabling full type-checking with mypy while preserving existing functionality.

The exclusion of device_interface.py is removed from pyproject.toml since this module was removed in #261.

scipy-stubs is added to requirements-dev.txt since scipy is used in random_objects.py.

This commit adds type annotations to `random_objects.py`, along with
its corresponding tests, enabling full type-checking with mypy while
preserving existing functionality.
The exclusion of `device_interface.py` is removed from
`pyproject.toml` since this module was removed in TeamGraphix#261.
`scipy-stubs` is added to `requirements-dev.txt` since `scipy` is used
in `random_objects.py`.
**Related issue:**
This PR continues the work started in TeamGraphix#302, TeamGraphix#308, TeamGraphix#312 and TeamGraphix#347.
@codecov

codecovBot commented Nov 17, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.25%. Comparing base (1bc4b34) to head (b81fb90).
⚠️ Report is 1 commits behind head on master.

Files with missing linesPatch %Lines
graphix/random_objects.py94.44%1 Missing ⚠️
Additional details and impacted files
@@ Coverage Diff @@## master #369 +/- ##
==========================================
- Coverage 86.25% 86.25% -0.01% 
==========================================
Files 44 44 Lines 6163 6161 -2 ==========================================
- Hits 5316 5314 -2 
Misses 847 847 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment threadgraphix/random_objects.py Outdated


def rand_unit(sz: int, rng: Generator | None = None) -> npt.NDArray[np.complex128]:
def rand_unit(sz: IntLike, rng: Generator | None = None) -> npt.NDArray[np.float64]:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why np.float64 instead of np.complex128 ?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I added this comment in ffe24c2:

# unitary_group.rvs returns onp.Array3D[np.float64]# https://github.com/scipy/scipy-stubs/blob/3b629159e8da5cc3aa82b871135489d6d2fd5f8e/scipy-stubs/stats/_multivariate.pyi#L370

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this is a mistake in the stubs:

importnumpyasnpfromgraphix.random_objectsimportrand_unitassertrand_unit(3).dtype==np.complex128

Also, rand_unit(sz=1) clearly returns a complex number.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Oops, yes, you're right of course! I reported the issue upstream (scipy/scipy-stubs#987) and I temporarily fixed the annotation in 202788b by coercing the result with .astype(np.complex128, copy=False) (which should be a no-op, since the array at runtime is already np.complex128).

Comment threadgraphix/random_objects.py Outdated
Comment threadgraphix/random_objects.py Outdated
Comment threadgraphix/random_objects.py Outdated

@matulnimatulni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Small comment on the bug-fix, otherwise looks good to go!

Comment threadgraphix/random_objects.py Outdated

@emlynsgemlynsg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor comment, but shouldn't hold up merge.

Comment threadgraphix/random_objects.py
@thierry-martinez
thierry-martinez merged commit 8d9a477 into TeamGraphix:masterNov 28, 2025
21 checks passed
emlynsg pushed a commit that referenced this pull request Dec 5, 2025
This commit adds type annotations to `random_objects.py`, along with
its corresponding tests, enabling full type-checking with mypy while
preserving existing functionality.
The exclusion of `device_interface.py` is removed from
`pyproject.toml` since this module was removed in #261.
`scipy-stubs` is added to `requirements-dev.txt` since `scipy` is used
in `random_objects.py`.
Revealed bad type annotation for `unitary_group.rvs`: scipy/scipy-stubs#987
**Related issue:**
This PR continues the work started in #302, #308, #312 and #347.
---------
Co-authored-by: matulni <m.uldemolins@gmail.com>
Sign up for freeto 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

@thierry-martinez@emlynsg@matulni