Skip to content
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

Add type hints #2327

Merged
merged 22 commits into from May 13, 2024
Merged

Add type hints #2327

merged 22 commits into from May 13, 2024

Conversation

Ericgig
Copy link
Member

@Ericgig Ericgig commented Feb 15, 2024

Description
Added hints to mesolve, sesolve, result, Qobj, QobjEvo.

The aim is not to pass static type checker, but to help the user with hints:

  • I added types to outside facing functions.
  • I sometime used hints that do not match the real implementation. For example:
    • Qobj.__mul__ is typed to support product with scalar, not other Qobj, (for which __matmul__ should be used.)
    • e_ops are hinted to dict but also support single operator, list, tuple...
    • solver.options claim it return a dict not _SolverOptions (which is a subclass of dict.)
  • I skipped the None type. (result.final_state type is Qobj event if it is None when not saved.)

I created alias types:

  • QobjEvoLike: Stuff that can be made into a QobjEvo.
  • CoefficientLike: Stuff that can be made into a Coefficient.
  • LayerType: Can be understood as our data layer type. (A dynamically generated Literal could be better.)
    Maybe some alias for dimensions object could be useful?

Knowing that we have extension modules, I used numbers.Number for complex hoping it support jax or tensorflow's scalars. Also Any used in some place where other type support can be added such as Qobj.__init__.

Some of the type hint features I used were only added in python 3.10. We still tests with python 3.9, but it's old enough that we can remove it (according to spec-0000).

@coveralls
Copy link

coveralls commented May 1, 2024

Coverage Status

coverage: 86.26% (+0.04%) from 86.22%
when pulling a8b95a9 on Ericgig:misc.type_hints
into 8ab189e on qutip:master.

@Ericgig Ericgig marked this pull request as ready for review May 1, 2024 19:47
@Ericgig Ericgig merged commit 4acc9f1 into qutip:master May 13, 2024
10 of 11 checks passed
@Ericgig Ericgig deleted the misc.type_hints branch May 13, 2024 20:43
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.

None yet

3 participants