diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f773c67dd9f..6fe5cdaf8b1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,7 +30,7 @@ repos: additional_dependencies: ["toml"] - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.991 + rev: v1.0.0 hooks: - id: mypy args: [--strict, --ignore-missing-imports, --show-error-codes] diff --git a/docs/conf.py b/docs/conf.py index 06e12f31f5e..5a94d8d6e51 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -58,6 +58,7 @@ nitpick_ignore = [ # Undocumented classes ("py:class", "kornia.augmentation._2d.intensity.base.IntensityAugmentationBase2D"), + ("py:class", "kornia.augmentation.base._AugmentationBase"), ("py:class", "segmentation_models_pytorch.base.model.SegmentationModel"), ("py:class", "timm.models.resnet.ResNet"), ("py:class", "timm.models.vision_transformer.VisionTransformer"), diff --git a/evaluate.py b/evaluate.py index 1ec227ecc3a..04105f63f70 100755 --- a/evaluate.py +++ b/evaluate.py @@ -119,7 +119,7 @@ def run_eval_loop( } for i in range(len(batch["image"])) ] - with torch.inference_mode(): + with torch.inference_mode(): # type: ignore[no-untyped-call] y_pred = model(x) metrics(y_pred, y) results = metrics.compute() @@ -143,7 +143,6 @@ def main(args: argparse.Namespace) -> None: # Loads the saved model from checkpoint based on the `args.task` name that was # passed as input model = TASK.load_from_checkpoint(args.input_checkpoint) - model = cast(pl.LightningModule, model) model.freeze() model.eval() diff --git a/requirements/tests.txt b/requirements/tests.txt index 3c5c75212b3..4dd079e5fce 100644 --- a/requirements/tests.txt +++ b/requirements/tests.txt @@ -1,5 +1,5 @@ # tests -mypy==0.991 +mypy==1.0.0 nbmake==1.3.5 pytest==7.2.1 pytest-cov==4.0.0 diff --git a/setup.cfg b/setup.cfg index 1b38630529d..d0af5569e19 100644 --- a/setup.cfg +++ b/setup.cfg @@ -121,7 +121,7 @@ style = pyupgrade>=2.4,<4 tests = # mypy 0.900+ required for pyproject.toml support - mypy>=0.900,<0.992 + mypy>=0.900,<2 # nbmake 0.1+ required to fix path_source bug nbmake>=0.1,<2 # pytest 6.1.2+ required by nbmake diff --git a/tests/models/test_changestar.py b/tests/models/test_changestar.py index 85f8485067d..0c5a737a9dd 100644 --- a/tests/models/test_changestar.py +++ b/tests/models/test_changestar.py @@ -17,7 +17,7 @@ class TestChangeStar: - @torch.no_grad() # type: ignore[misc] + @torch.no_grad() def test_changestar_farseg_classes(self) -> None: model = ChangeStarFarSeg( classes=4, backbone="resnet50", backbone_pretrained=False @@ -27,7 +27,7 @@ def test_changestar_farseg_classes(self) -> None: assert y["bi_seg_logit"].shape[2] == 4 - @torch.no_grad() # type: ignore[misc] + @torch.no_grad() def test_changestar_farseg_output_size(self) -> None: model = ChangeStarFarSeg( classes=4, backbone="resnet50", backbone_pretrained=False @@ -55,7 +55,7 @@ def test_invalid_changestar_farseg_backbone(self) -> None: with pytest.raises(ValueError, match=match): ChangeStarFarSeg(classes=4, backbone="anynet", backbone_pretrained=False) - @torch.no_grad() # type: ignore[misc] + @torch.no_grad() @pytest.mark.parametrize( "inc,innerc,nc,sf", list(itertools.product(IN_CHANNELS, INNNR_CHANNELS, NC, SF)) ) @@ -70,7 +70,7 @@ def test_changemixin_output_size( assert y[0].shape == y[1].shape assert y[0].shape == (3, 1, 32 * sf, 32 * sf) - @torch.no_grad() # type: ignore[misc] + @torch.no_grad() def test_changestar(self) -> None: dense_feature_extractor = nn.modules.Sequential( nn.modules.Conv2d(3, 32, 3, 1, 1), @@ -97,7 +97,7 @@ def test_changestar(self) -> None: assert y["bi_seg_logit"].shape == (3, 2, 2, 64, 64) assert y["change_prob"].shape == (3, 1, 64, 64) - @torch.no_grad() # type: ignore[misc] + @torch.no_grad() def test_changestar_invalid_inference_mode(self) -> None: dense_feature_extractor = nn.modules.Sequential( nn.modules.Conv2d(3, 32, 3, 1, 1), @@ -122,7 +122,7 @@ def test_changestar_invalid_inference_mode(self) -> None: inference_mode="random", ) - @torch.no_grad() # type: ignore[misc] + @torch.no_grad() @pytest.mark.parametrize("inference_mode", ["t1t2", "t2t1", "mean"]) def test_changestar_inference_output_size(self, inference_mode: str) -> None: dense_feature_extractor = nn.modules.Sequential( diff --git a/tests/models/test_farseg.py b/tests/models/test_farseg.py index 3afa178d442..c87a172a378 100644 --- a/tests/models/test_farseg.py +++ b/tests/models/test_farseg.py @@ -9,7 +9,7 @@ class TestFarSeg: - @torch.no_grad() # type: ignore[misc] + @torch.no_grad() @pytest.mark.parametrize( "backbone,pretrained", [ diff --git a/tests/models/test_fcsiam.py b/tests/models/test_fcsiam.py index 0482eb6714e..954931e2be6 100644 --- a/tests/models/test_fcsiam.py +++ b/tests/models/test_fcsiam.py @@ -14,7 +14,7 @@ class TestFCSiamConc: - @torch.no_grad() # type: ignore[misc] + @torch.no_grad() @pytest.mark.parametrize("b, c", list(itertools.product(BATCH_SIZE, CHANNELS))) def test_in_channels(self, b: int, c: int) -> None: classes = 2 @@ -24,7 +24,7 @@ def test_in_channels(self, b: int, c: int) -> None: y = model(x) assert y.shape == (b, classes, h, w) - @torch.no_grad() # type: ignore[misc] + @torch.no_grad() @pytest.mark.parametrize("b, classes", list(itertools.product(BATCH_SIZE, CLASSES))) def test_classes(self, b: int, classes: int) -> None: t, c, h, w = 2, 3, 64, 64 @@ -35,7 +35,7 @@ def test_classes(self, b: int, classes: int) -> None: class TestFCSiamDiff: - @torch.no_grad() # type: ignore[misc] + @torch.no_grad() @pytest.mark.parametrize("b, c", list(itertools.product(BATCH_SIZE, CHANNELS))) def test_in_channels(self, b: int, c: int) -> None: classes = 2 @@ -45,7 +45,7 @@ def test_in_channels(self, b: int, c: int) -> None: y = model(x) assert y.shape == (b, classes, h, w) - @torch.no_grad() # type: ignore[misc] + @torch.no_grad() @pytest.mark.parametrize("b, classes", list(itertools.product(BATCH_SIZE, CLASSES))) def test_classes(self, b: int, classes: int) -> None: t, c, h, w = 2, 3, 64, 64 diff --git a/torchgeo/datasets/utils.py b/torchgeo/datasets/utils.py index 3270356f022..add1f13ff64 100644 --- a/torchgeo/datasets/utils.py +++ b/torchgeo/datasets/utils.py @@ -89,7 +89,7 @@ def __enter__(self) -> Any: except ImportError: # Only supports normal zip files # https://github.com/python/mypy/issues/1153 - import zipfile # type: ignore[no-redef] + import zipfile return zipfile.ZipFile(*self.args, **self.kwargs) diff --git a/torchgeo/trainers/byol.py b/torchgeo/trainers/byol.py index 3b9e8d0db3e..9c7cf41501f 100644 --- a/torchgeo/trainers/byol.py +++ b/torchgeo/trainers/byol.py @@ -337,7 +337,7 @@ def __init__(self, **kwargs: Any) -> None: super().__init__() # Creates `self.hparams` from kwargs - self.save_hyperparameters() # type: ignore[operator] + self.save_hyperparameters() self.hyperparams = cast(Dict[str, Any], self.hparams) self.config_task() diff --git a/torchgeo/trainers/classification.py b/torchgeo/trainers/classification.py index e8af2dcb70f..c94549d7a15 100644 --- a/torchgeo/trainers/classification.py +++ b/torchgeo/trainers/classification.py @@ -97,7 +97,7 @@ def __init__(self, **kwargs: Any) -> None: super().__init__() # Creates `self.hparams` from kwargs - self.save_hyperparameters() # type: ignore[operator] + self.save_hyperparameters() self.hyperparams = cast(Dict[str, Any], self.hparams) self.config_task() diff --git a/torchgeo/trainers/detection.py b/torchgeo/trainers/detection.py index 7cf89171fa7..939ae761b2d 100644 --- a/torchgeo/trainers/detection.py +++ b/torchgeo/trainers/detection.py @@ -164,7 +164,7 @@ def __init__(self, **kwargs: Any) -> None: """ super().__init__() # Creates `self.hparams` from kwargs - self.save_hyperparameters() # type: ignore[operator] + self.save_hyperparameters() self.hyperparams = cast(Dict[str, Any], self.hparams) self.config_task() diff --git a/torchgeo/trainers/regression.py b/torchgeo/trainers/regression.py index 98234bdec5d..7e6137f226a 100644 --- a/torchgeo/trainers/regression.py +++ b/torchgeo/trainers/regression.py @@ -75,7 +75,7 @@ def __init__(self, **kwargs: Any) -> None: super().__init__() # Creates `self.hparams` from kwargs - self.save_hyperparameters() # type: ignore[operator] + self.save_hyperparameters() self.hyperparams = cast(Dict[str, Any], self.hparams) self.config_task() diff --git a/torchgeo/trainers/segmentation.py b/torchgeo/trainers/segmentation.py index b5868196fc2..7459fa8a953 100644 --- a/torchgeo/trainers/segmentation.py +++ b/torchgeo/trainers/segmentation.py @@ -104,7 +104,7 @@ def __init__(self, **kwargs: Any) -> None: super().__init__() # Creates `self.hparams` from kwargs - self.save_hyperparameters() # type: ignore[operator] + self.save_hyperparameters() self.hyperparams = cast(Dict[str, Any], self.hparams) if not isinstance(kwargs["ignore_index"], (int, type(None))): diff --git a/torchgeo/transforms/transforms.py b/torchgeo/transforms/transforms.py index c202a8859d2..13574a958f0 100644 --- a/torchgeo/transforms/transforms.py +++ b/torchgeo/transforms/transforms.py @@ -5,11 +5,10 @@ from typing import Any, Dict, List, Optional, Tuple, Union -import kornia +import kornia.augmentation as K import torch from einops import rearrange -from kornia.augmentation import GeometricAugmentationBase2D -from kornia.augmentation.random_generator import CropGenerator +from kornia.constants import DataKey from kornia.geometry import crop_by_indices from torch import Tensor from torch.nn.modules import Module @@ -23,17 +22,21 @@ class AugmentationSequential(Module): Use :class:`kornia.augmentation.container.AugmentationSequential` instead. """ - def __init__(self, *args: Module, data_keys: List[str]) -> None: + def __init__( + self, + *args: Union[K.base._AugmentationBase, K.ImageSequential], + data_keys: List[str], + ) -> None: """Initialize a new augmentation sequential instance. Args: *args: Sequence of kornia augmentations - data_keys: List of inputs to augment (e.g. ["image", "mask", "boxes"]) + data_keys: List of inputs to augment (e.g., ["image", "mask", "boxes"]) """ super().__init__() self.data_keys = data_keys - keys = [] + keys: List[Union[str, int, DataKey]] = [] for key in data_keys: if key == "image": keys.append("input") @@ -42,7 +45,7 @@ def __init__(self, *args: Module, data_keys: List[str]) -> None: else: keys.append(key) - self.augs = kornia.augmentation.AugmentationSequential(*args, data_keys=keys) + self.augs = K.AugmentationSequential(*args, data_keys=keys) def forward(self, batch: Dict[str, Tensor]) -> Dict[str, Tensor]: """Perform augmentations and update data dict. @@ -84,7 +87,7 @@ def forward(self, batch: Dict[str, Tensor]) -> Dict[str, Tensor]: return batch -class _RandomNCrop(GeometricAugmentationBase2D): +class _RandomNCrop(K.GeometricAugmentationBase2D): """Take N random crops of a tensor.""" def __init__(self, size: Tuple[int, int], num: int) -> None: @@ -138,7 +141,7 @@ def apply_transform( return torch.cat(out) -class _NCropGenerator(CropGenerator): +class _NCropGenerator(K.random_generator.CropGenerator): """Generate N random crops.""" def __init__(self, size: Union[Tuple[int, int], Tensor], num: int) -> None: