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

mypy: even more strict #2241

Merged
merged 6 commits into from
Aug 23, 2024
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
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"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't particularly useful, what we really need is python/mypy#12286

# 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 @@ -367,7 +367,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 @@ -385,7 +387,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 @@ -405,7 +409,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 @@ -426,7 +430,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 @@ -448,7 +452,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 @@ -473,7 +477,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]]:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be a sequence, this was a bug

"""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')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bug was introduced in #1082


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