Plan to add `mypy`
Opened this issue · 7 comments
Add a check for mypy in
Right now, ~/toqito$ mypy .
fails due to following:
build/lib/tests/test_matrices/test_gell_mann.py:4: error: Skipping analyzing "scipy.sparse": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/tests/test_matrices/test_gell_mann.py:4: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
build/lib/tests/test_matrices/test_pauli.py:4: error: Skipping analyzing "scipy.sparse": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/channels/reduction.py:4: error: Skipping analyzing "scipy.sparse": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/matrices/gell_mann.py:5: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/matrices/gen_gell_mann.py:6: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/matrices/iden.py:4: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/matrices/pauli.py:6: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/matrix_props/sk_norm.py:8: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/perms/antisymmetric_projection.py:6: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/perms/perm_sign.py:6: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/perms/permute_systems.py:9: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/perms/symmetric_projection.py:5: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/state_metrics/fidelity.py:4: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/state_metrics/matsumoto_fidelity.py:4: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/state_props/entanglement_of_formation.py:5: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/states/ghz.py:4: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/states/max_entangled.py:4: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/states/max_mixed.py:4: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
build/lib/toqito/states/w_state.py:4: error: Skipping analyzing "scipy": module is installed, but missing library stubs or py.typed marker [import-untyped]
toqito/channel_metrics/fidelity_of_separability.py: error: Source file found twice under different module names: "channel_metrics.fidelity_of_separability" and "toqito.channel_metrics.fidelity_of_separability"
Found 20 errors in 20 files (errors prevented further checking)
scipy
is not in typeshed
as suggested by mypy's
docs.
https://mypy.readthedocs.io/en/stable/config_file.html#config-file
https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-library-stubs-or-py-typed-marker
https://mypy.readthedocs.io/en/stable/stubs.html#stub-files
https://mypy.readthedocs.io/en/stable/error_code_list.html#error-codes-enabled-by-default
https://stackoverflow.com/a/64122820
https://stackoverflow.com/a/73390971
https://mypy.readthedocs.io/en/stable/config_file.html#using-a-pyproject-toml-file
It might be better to ignore scipy
related warnings/errors raised by mypy
.
The same issue in scipy
also applies to toqito
. So, we will have to
- option 1: ignore all modules in
toqito.same_module
or - option 2: generate stubs for each and every module in toqito.
https://mypy.readthedocs.io/en/stable/stubs.html#stub-files
How scipy
ignores all issues related to [import-untyped]
modules in their package: https://github.com/scipy/scipy/blob/fcf7b652bc27e47d215557bda61c84d19adc3aae/mypy.ini
To work on either of the above options, need to first fix toqito/channel_metrics/fidelity_of_separability.py: error: Source file found twice under different module names: "channel_metrics.fidelity_of_separability" and "toqito.channel_metrics.fidelity_of_separability"
. The same error is applied to all toqito.some_module
. Might be related to how mypy
finds the import path.
https://mypy.readthedocs.io/en/stable/running_mypy.html#how-imports-are-found
Edit: Ignoring all module is installed, but missing library stubs or py.typed marker
, issues that need to be fixed are in the attached file mypy_errors.txt. It might be better to create a PR per module or per function.
@vprusso FYI I am un-assigning myself from this issue.
I believe trying to fix some of the errors in the txt
file might require a major refactor considering quite a few functions allow multiple types of inputs and same variable is re-assigned a different value/type based on these inputs.
For example, the following lines are flagged by mypy
due to the redefinition. We disabled pylint
warnings for the same.
toqito/toqito/state_props/sk_vec_norm.py
Lines 60 to 64 in ffa9165
state_props/sk_vec_norm.py:61: error: Incompatible types in assignment (expression has type "ndarray[Any, dtype[Any]]", variable has type "int | list[int]") [assignment]
state_props/sk_vec_norm.py:62: error: Value of type "int" is not indexable [index]
state_props/sk_vec_norm.py:64: error: Unsupported target for indexed assignment ("int") [index]
state_props/sk_vec_norm.py:64: error: Value of type "int" is not indexable [index]
What if we defined one private method per allowed option for an input? Then we could call these in the main function sk_vector_norm
.
Sounds good, that's completely reasonable. Thanks for the heads up, @purva-thakre !
Great!
To close this issue:
- Add
mypy
to github workflows: FixSource file found twice under different module names
, ignore toqito'sstub
issues similar toscipy
, comment outmypy
lines in the workflow after fixing the quickly fixable errors. - Create a separate issue to add
mypy
back to the workflows after items 3 and 4 are fixed. - Create a separate issue for an itemized list of which functions require a refactor to fix
mypy
errors. Fix each item with a new PR. - Create a separate issue to generate stub files for all modules in toqito. Remove the file where
mypy
was ignoring import errors related to toqito's modules.
Figured out a nice workaround to understanding what type mypy expects.
In some .py
file, add from typing import reveal_type
as an import. Then use reveal_type
to print the type of the output. This makes sure we are using types consistent with what's expected by mypy.
Note: We will have to use --explicit-package-bases
when running mypy in some directory because it gets confused with two modules having similarly named files.
https://mypy.readthedocs.io/en/stable/command_line.html#import-discovery