Skip to content

Improve mypy type checking #82

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

Merged
merged 2 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ repos:
rev: v1.2.0
hooks:
- id: mypy
args: ["--disallow-incomplete-defs", "--ignore-missing-imports"]

- repo: https://github.com/charliermarsh/ruff-pre-commit
# Ruff version.
Expand Down
23 changes: 23 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,26 @@ fix = true

[tool.ruff.pydocstyle]
convention = "numpy"

[tool.mypy]
# Block below are checks that form part of mypy 'strict' mode
warn_unused_configs = true
warn_redundant_casts = true
warn_unused_ignores = true
strict_equality = true
strict_concatenate = true
check_untyped_defs = true
disallow_subclassing_any = false # TODO: fix
disallow_untyped_decorators = true
disallow_any_generics = true
disallow_untyped_calls = true
disallow_incomplete_defs = true
disallow_untyped_defs = true
no_implicit_reexport = true
warn_return_any = false # TODO: fix

[[tool.mypy.overrides]]
module = [
"napari_matplotlib/tests/*",
]
disallow_untyped_defs = false
6 changes: 3 additions & 3 deletions src/napari_matplotlib/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def draw(self) -> None:
This is a no-op, and is intended for derived classes to override.
"""

def apply_napari_colorscheme(self):
def apply_napari_colorscheme(self) -> None:
"""Apply napari-compatible colorscheme to the axes object."""
if self.axes is None:
return
Expand Down Expand Up @@ -153,7 +153,7 @@ def _on_update_layers(self) -> None:
This is a no-op, and is intended for derived classes to override.
"""

def _replace_toolbar_icons(self):
def _replace_toolbar_icons(self) -> None:
# Modify toolbar icons and some tooltips
for action in self.toolbar.actions():
text = action.text()
Expand All @@ -175,7 +175,7 @@ def _replace_toolbar_icons(self):
class NapariNavigationToolbar(NavigationToolbar2QT):
"""Custom Toolbar style for Napari."""

def _update_buttons_checked(self):
def _update_buttons_checked(self) -> None:
"""Update toggle tool icons when selected/unselected."""
super()._update_buttons_checked()
# changes pan/zoom icons depending on state (checked or not)
Expand Down
2 changes: 1 addition & 1 deletion src/napari_matplotlib/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, napari_viewer: napari.viewer.Viewer):
self.update_layers(None)

@property
def layer(self):
def layer(self) -> napari.layers.Layer:
"""
Layer being plotted.
"""
Expand Down
6 changes: 5 additions & 1 deletion src/napari_matplotlib/tests/test_scatter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, Tuple

import numpy as np

from napari_matplotlib import FeaturesScatterWidget, ScatterWidget
Expand All @@ -19,7 +21,9 @@ def test_features_scatter_widget(make_napari_viewer):
FeaturesScatterWidget(viewer)


def make_labels_layer_with_features():
def make_labels_layer_with_features() -> (
Tuple[np.ndarray, Dict[str, Tuple[Any]]]
):
label_image = np.zeros((100, 100), dtype=np.uint16)
for label_value, start_index in enumerate([10, 30, 50], start=1):
end_index = start_index + 10
Expand Down
2 changes: 1 addition & 1 deletion src/napari_matplotlib/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def test_interval():
assert 10 not in interval

with pytest.raises(ValueError, match="must be an integer"):
"string" in interval
"string" in interval # type: ignore