Skip to content

Commit

Permalink
[python] Enhance type-checking configuration. (#103)
Browse files Browse the repository at this point in the history
- Require setting codes on type-ignore comments.
- Warn on unnecessary typecasts.
- Check functions without type information.
  • Loading branch information
thetorpedodog authored Jan 26, 2023
1 parent 9a957ee commit 1b9200b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ force_single_line = true
known_first_party = ["somacore"]
single_line_exclusions = ["typing", "typing_extensions"]

[tool.mypy]
check_untyped_defs = true
enable_error_code = ["ignore-without-code"]
warn_redundant_casts = true
# We want to enable this but it won't work when running locally due to the
# presence of _version.py (which invalidates the ignore, which causes an error).
#
# warn_unused_ignores = true

[[tool.mypy.overrides]]
# These dependencies do not currently have canonical type stubs.
module = ["anndata", "numba", "pandas", "pyarrow", "scipy"]
Expand Down
8 changes: 4 additions & 4 deletions python-spec/src/somacore/query/_fast_csr.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ def finalize(self) -> _CSRAccumulatorFinalResult:
)


@numba.jit(nopython=True, nogil=True) # type: ignore
@numba.jit(nopython=True, nogil=True) # type: ignore[attr-defined]
def _accum_row_length(
row_length: npt.NDArray[np.int64], row_ind: npt.NDArray[np.int64]
) -> None:
for rind in row_ind:
row_length[rind] += 1


@numba.jit(nopython=True, nogil=True) # type: ignore
@numba.jit(nopython=True, nogil=True) # type: ignore[attr-defined]
def _copy_chunk_range(
row_ind_chunk: npt.NDArray[np.signedinteger],
col_ind_chunk: npt.NDArray[np.signedinteger],
Expand All @@ -194,7 +194,7 @@ def _copy_chunk_range(
indptr[row] += 1


@numba.jit(nopython=True, nogil=True) # type: ignore
@numba.jit(nopython=True, nogil=True) # type: ignore[attr-defined]
def _copy_chunklist_range(
chunk_list: numba.typed.List,
data: npt.NDArray[np.number],
Expand All @@ -219,7 +219,7 @@ def _copy_chunklist_range(
)


@numba.jit(nopython=True, nogil=True) # type: ignore
@numba.jit(nopython=True, nogil=True) # type: ignore[attr-defined]
def _finalize_indptr(indptr: npt.NDArray[np.signedinteger]):
prev = 0
for r in range(len(indptr)):
Expand Down
5 changes: 3 additions & 2 deletions python-spec/testing/test_collection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
from typing import Any

from somacore import collection
from somacore import experiment
Expand All @@ -10,8 +11,8 @@ def test_basic(self):
# Since the SimpleCollection implementation is straightforward this is
# just to ensure that we actually fulfill everything.

coll = collection.SimpleCollection()
entry_a = collection.SimpleCollection()
coll = collection.SimpleCollection[Any]()
entry_a = collection.SimpleCollection[Any]()
coll["a"] = entry_a
self.assertIs(entry_a, coll["a"])
del coll["a"]
Expand Down

0 comments on commit 1b9200b

Please sign in to comment.