-
Notifications
You must be signed in to change notification settings - Fork 385
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
mypy: even more strict #2241
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0847147
mypy: even more strict
adamjstewart 65972c8
dict is discouraged for arguments
adamjstewart e7ac629
Don't use Any in sample/batch utils
adamjstewart f07b9c0
Fix transforms
adamjstewart 0939822
Revert "Don't use Any in sample/batch utils"
adamjstewart 288c2ed
Fix remaining bugs
adamjstewart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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: | ||
|
@@ -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: | ||
|
@@ -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 | ||
|
@@ -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`. | ||
|
@@ -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`. | ||
|
@@ -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]]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -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') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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