Skip to content

Commit

Permalink
mypy: even more strict (#2241)
Browse files Browse the repository at this point in the history
* mypy: even more strict

* dict is discouraged for arguments

* Don't use Any in sample/batch utils

* Fix transforms

* Revert "Don't use Any in sample/batch utils"

This reverts commit e7ac629.

* Fix remaining bugs
  • Loading branch information
adamjstewart authored Aug 23, 2024
1 parent 9935f96 commit 95a1fec
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
31 changes: 23 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,27 +141,42 @@ exclude_lines = [
"@overload",
]

# https://mypy.readthedocs.io/en/stable/config_file.html
[tool.mypy]
python_version = "3.10"
# Import discovery
ignore_missing_imports = true
show_error_codes = true
exclude = "(build|data|dist|docs/src|images|logo|logs|output)/"
exclude = "(build|data|dist|docs/.*|images|logo|.*logs|output|requirements)/"

# Strict
warn_unused_configs = true
# Disallow dynamic typing (TODO: work in progress)
disallow_any_unimported = false
disallow_any_expr = false
disallow_any_decorated = false
disallow_any_explicit = false
disallow_any_generics = true
disallow_subclassing_any = true

# Untyped definitions and calls
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
check_untyped_defs = true
disallow_untyped_decorators = true
no_implicit_optional = true

# Configuring warnings
warn_redundant_casts = true
warn_unused_ignores = true
warn_no_return = true
warn_return_any = true
no_implicit_reexport = true
warn_unreachable = true

# Miscellaneous strictness flags
strict_equality = true
strict = true

# Configuring error messages
pretty = true

# Miscellaneous
warn_unused_configs = true

[tool.pytest.ini_options]
# Skip slow tests by default
Expand Down
18 changes: 11 additions & 7 deletions torchgeo/datasets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import shutil
import subprocess
import sys
from collections.abc import Iterable, Iterator, Sequence
from collections.abc import Iterable, Iterator, Mapping, MutableMapping, Sequence
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import Any, TypeAlias, cast, overload
Expand Down Expand Up @@ -366,7 +366,9 @@ def working_dir(dirname: Path, create: bool = False) -> Iterator[None]:
os.chdir(cwd)


def _list_dict_to_dict_list(samples: Iterable[dict[Any, Any]]) -> dict[Any, list[Any]]:
def _list_dict_to_dict_list(
samples: Iterable[Mapping[Any, Any]],
) -> dict[Any, list[Any]]:
"""Convert a list of dictionaries to a dictionary of lists.
Args:
Expand All @@ -384,7 +386,9 @@ def _list_dict_to_dict_list(samples: Iterable[dict[Any, Any]]) -> dict[Any, list
return collated


def _dict_list_to_list_dict(sample: dict[Any, Sequence[Any]]) -> list[dict[Any, Any]]:
def _dict_list_to_list_dict(
sample: Mapping[Any, Sequence[Any]],
) -> list[dict[Any, Any]]:
"""Convert a dictionary of lists to a list of dictionaries.
Args:
Expand All @@ -404,7 +408,7 @@ def _dict_list_to_list_dict(sample: dict[Any, Sequence[Any]]) -> list[dict[Any,
return uncollated


def stack_samples(samples: Iterable[dict[Any, Any]]) -> dict[Any, Any]:
def stack_samples(samples: Iterable[Mapping[Any, Any]]) -> dict[Any, Any]:
"""Stack a list of samples along a new axis.
Useful for forming a mini-batch of samples to pass to
Expand All @@ -425,7 +429,7 @@ def stack_samples(samples: Iterable[dict[Any, Any]]) -> dict[Any, Any]:
return collated


def concat_samples(samples: Iterable[dict[Any, Any]]) -> dict[Any, Any]:
def concat_samples(samples: Iterable[Mapping[Any, Any]]) -> dict[Any, Any]:
"""Concatenate a list of samples along an existing axis.
Useful for joining samples in a :class:`torchgeo.datasets.IntersectionDataset`.
Expand All @@ -447,7 +451,7 @@ def concat_samples(samples: Iterable[dict[Any, Any]]) -> dict[Any, Any]:
return collated


def merge_samples(samples: Iterable[dict[Any, Any]]) -> dict[Any, Any]:
def merge_samples(samples: Iterable[Mapping[Any, Any]]) -> dict[Any, Any]:
"""Merge a list of samples.
Useful for joining samples in a :class:`torchgeo.datasets.UnionDataset`.
Expand All @@ -472,7 +476,7 @@ def merge_samples(samples: Iterable[dict[Any, Any]]) -> dict[Any, Any]:
return collated


def unbind_samples(sample: dict[Any, Sequence[Any]]) -> list[dict[Any, Any]]:
def unbind_samples(sample: MutableMapping[Any, Any]) -> list[dict[Any, Any]]:
"""Reverse of :func:`stack_samples`.
Useful for turning a mini-batch of samples into a list of samples. These individual
Expand Down
4 changes: 2 additions & 2 deletions torchgeo/transforms/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(

self.augs = K.AugmentationSequential(*args, data_keys=keys, **kwargs) # type: ignore[arg-type]

def forward(self, batch: dict[str, Tensor]) -> dict[str, Tensor]:
def forward(self, batch: dict[str, Any]) -> dict[str, Any]:
"""Perform augmentations and update data dict.
Args:
Expand Down Expand Up @@ -99,7 +99,7 @@ def forward(self, batch: dict[str, Tensor]) -> dict[str, Tensor]:

# Convert boxes to default [N, 4]
if 'boxes' in batch:
batch['boxes'] = Boxes(batch['boxes']).to_tensor(mode='xyxy') # type:ignore[assignment]
batch['boxes'] = Boxes(batch['boxes']).to_tensor(mode='xyxy')

# Torchmetrics does not support masks with a channel dimension
if 'mask' in batch and batch['mask'].shape[1] == 1:
Expand Down

0 comments on commit 95a1fec

Please sign in to comment.