From 0f797d794c2fd9bb92f4fa5bf4d8d9836b729bc0 Mon Sep 17 00:00:00 2001 From: James Butler Date: Wed, 23 Oct 2024 17:55:42 -0400 Subject: [PATCH] Apply pyupgrade for Python 3.9+ syntax This should have been included in https://github.com/Project-MONAI/MONAI/commit/14b086b553693f5d344ff054f37d12ce6839da06 when Python 3.8 support was dropped. Changes were automatically applied by running: pre_commit run --all-files Signed-off-by: James Butler --- .pre-commit-config.yaml | 2 +- monai/apps/detection/networks/retinanet_network.py | 4 ++-- monai/apps/detection/transforms/array.py | 4 +++- monai/apps/detection/utils/anchor_utils.py | 7 ++++--- .../apps/generation/maisi/networks/autoencoderkl_maisi.py | 2 +- monai/apps/generation/maisi/networks/controlnet_maisi.py | 2 +- monai/apps/pathology/engines/utils.py | 4 +++- monai/apps/pathology/inferers/inferer.py | 4 +++- monai/apps/pathology/metrics/lesion_froc.py | 4 +++- monai/apps/pathology/transforms/post/array.py | 4 +++- monai/apps/tcia/utils.py | 2 +- monai/apps/utils.py | 5 +---- monai/apps/vista3d/transforms.py | 2 +- monai/bundle/reference_resolver.py | 4 +++- monai/bundle/workflows.py | 4 +++- monai/config/type_definitions.py | 4 +++- monai/data/meta_obj.py | 4 +++- monai/data/meta_tensor.py | 4 +++- monai/engines/evaluator.py | 4 +++- monai/engines/trainer.py | 4 +++- monai/engines/utils.py | 4 +++- monai/handlers/clearml_handlers.py | 4 +++- monai/inferers/utils.py | 4 +++- monai/metrics/utils.py | 4 +++- monai/transforms/intensity/dictionary.py | 4 +++- monai/transforms/lazy/functional.py | 4 +++- monai/transforms/spatial/array.py | 6 ++++-- monai/transforms/utility/dictionary.py | 4 +++- monai/transforms/utils_morphological_ops.py | 2 +- monai/utils/component_store.py | 4 +++- monai/utils/decorators.py | 4 +++- monai/utils/module.py | 4 +++- monai/utils/state_cacher.py | 4 +++- tests/test_compute_panoptic_quality.py | 3 +-- tests/test_dynunet.py | 4 +++- tests/test_network_consistency.py | 2 +- 36 files changed, 91 insertions(+), 44 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5ca30c3784..f217ea7e2e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -37,7 +37,7 @@ repos: rev: v3.19.0 hooks: - id: pyupgrade - args: [--py38-plus, --keep-runtime-typing] + args: [--py39-plus, --keep-runtime-typing] name: Upgrade code with exceptions exclude: | (?x)( diff --git a/monai/apps/detection/networks/retinanet_network.py b/monai/apps/detection/networks/retinanet_network.py index ec86c3b0e9..ca6a8f5c19 100644 --- a/monai/apps/detection/networks/retinanet_network.py +++ b/monai/apps/detection/networks/retinanet_network.py @@ -42,7 +42,7 @@ import math import warnings from collections.abc import Callable, Sequence -from typing import Any, Dict +from typing import Any import torch from torch import Tensor, nn @@ -330,7 +330,7 @@ def forward(self, images: Tensor) -> Any: features = self.feature_extractor(images) if isinstance(features, Tensor): feature_maps = [features] - elif torch.jit.isinstance(features, Dict[str, Tensor]): + elif torch.jit.isinstance(features, dict[str, Tensor]): feature_maps = list(features.values()) else: feature_maps = list(features) diff --git a/monai/apps/detection/transforms/array.py b/monai/apps/detection/transforms/array.py index d8ffce4584..6b92572954 100644 --- a/monai/apps/detection/transforms/array.py +++ b/monai/apps/detection/transforms/array.py @@ -15,7 +15,9 @@ from __future__ import annotations -from typing import Any, Sequence +from typing import Any + +from collections.abc import Sequence import numpy as np import torch diff --git a/monai/apps/detection/utils/anchor_utils.py b/monai/apps/detection/utils/anchor_utils.py index cbde3ebae9..145350853a 100644 --- a/monai/apps/detection/utils/anchor_utils.py +++ b/monai/apps/detection/utils/anchor_utils.py @@ -39,7 +39,8 @@ from __future__ import annotations -from typing import List, Sequence + +from collections.abc import Sequence import torch from torch import Tensor, nn @@ -106,7 +107,7 @@ class AnchorGenerator(nn.Module): anchor_generator = AnchorGenerator(sizes, aspect_ratios) """ - __annotations__ = {"cell_anchors": List[torch.Tensor]} + __annotations__ = {"cell_anchors": list[torch.Tensor]} def __init__( self, @@ -364,7 +365,7 @@ class AnchorGeneratorWithAnchorShape(AnchorGenerator): anchor_generator = AnchorGeneratorWithAnchorShape(feature_map_scales, base_anchor_shapes) """ - __annotations__ = {"cell_anchors": List[torch.Tensor]} + __annotations__ = {"cell_anchors": list[torch.Tensor]} def __init__( self, diff --git a/monai/apps/generation/maisi/networks/autoencoderkl_maisi.py b/monai/apps/generation/maisi/networks/autoencoderkl_maisi.py index a52274b24a..6251ea8e83 100644 --- a/monai/apps/generation/maisi/networks/autoencoderkl_maisi.py +++ b/monai/apps/generation/maisi/networks/autoencoderkl_maisi.py @@ -13,7 +13,7 @@ import gc import logging -from typing import Sequence +from collections.abc import Sequence import torch import torch.nn as nn diff --git a/monai/apps/generation/maisi/networks/controlnet_maisi.py b/monai/apps/generation/maisi/networks/controlnet_maisi.py index 269086d971..7c13fd7bc6 100644 --- a/monai/apps/generation/maisi/networks/controlnet_maisi.py +++ b/monai/apps/generation/maisi/networks/controlnet_maisi.py @@ -11,7 +11,7 @@ from __future__ import annotations -from typing import Sequence +from collections.abc import Sequence import torch diff --git a/monai/apps/pathology/engines/utils.py b/monai/apps/pathology/engines/utils.py index 02249ed640..c704dc7469 100644 --- a/monai/apps/pathology/engines/utils.py +++ b/monai/apps/pathology/engines/utils.py @@ -11,7 +11,9 @@ from __future__ import annotations -from typing import Any, Sequence +from typing import Any + +from collections.abc import Sequence import torch diff --git a/monai/apps/pathology/inferers/inferer.py b/monai/apps/pathology/inferers/inferer.py index 71259ca7df..4f034f0ad1 100644 --- a/monai/apps/pathology/inferers/inferer.py +++ b/monai/apps/pathology/inferers/inferer.py @@ -11,7 +11,9 @@ from __future__ import annotations -from typing import Any, Callable, Sequence +from typing import Any, Callable + +from collections.abc import Sequence import numpy as np import torch diff --git a/monai/apps/pathology/metrics/lesion_froc.py b/monai/apps/pathology/metrics/lesion_froc.py index f4bf51ab28..bbd7bf4905 100644 --- a/monai/apps/pathology/metrics/lesion_froc.py +++ b/monai/apps/pathology/metrics/lesion_froc.py @@ -11,7 +11,9 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Iterable +from typing import TYPE_CHECKING, Any + +from collections.abc import Iterable import numpy as np diff --git a/monai/apps/pathology/transforms/post/array.py b/monai/apps/pathology/transforms/post/array.py index 035bce2c69..bb50ee4fd9 100644 --- a/monai/apps/pathology/transforms/post/array.py +++ b/monai/apps/pathology/transforms/post/array.py @@ -12,7 +12,9 @@ from __future__ import annotations import warnings -from typing import Callable, Sequence +from typing import Callable + +from collections.abc import Sequence import numpy as np import torch diff --git a/monai/apps/tcia/utils.py b/monai/apps/tcia/utils.py index 5524b488e9..f023cdbc87 100644 --- a/monai/apps/tcia/utils.py +++ b/monai/apps/tcia/utils.py @@ -12,7 +12,7 @@ from __future__ import annotations import os -from typing import Iterable +from collections.abc import Iterable import monai from monai.config.type_definitions import PathLike diff --git a/monai/apps/utils.py b/monai/apps/utils.py index 0c998146a3..c2e17d3247 100644 --- a/monai/apps/utils.py +++ b/monai/apps/utils.py @@ -136,10 +136,7 @@ def check_hash(filepath: PathLike, val: str | None = None, hash_type: str = "md5 return True actual_hash_func = look_up_option(hash_type.lower(), SUPPORTED_HASH_TYPES) - if sys.version_info >= (3, 9): - actual_hash = actual_hash_func(usedforsecurity=False) # allows checks on FIPS enabled machines - else: - actual_hash = actual_hash_func() + actual_hash = actual_hash_func(usedforsecurity=False) # allows checks on FIPS enabled machines try: with open(filepath, "rb") as f: diff --git a/monai/apps/vista3d/transforms.py b/monai/apps/vista3d/transforms.py index 3e8145cd80..bd7fb19493 100644 --- a/monai/apps/vista3d/transforms.py +++ b/monai/apps/vista3d/transforms.py @@ -12,7 +12,7 @@ from __future__ import annotations import warnings -from typing import Sequence +from collections.abc import Sequence import numpy as np import torch diff --git a/monai/bundle/reference_resolver.py b/monai/bundle/reference_resolver.py index 050cd75fa7..6c5ca776f5 100644 --- a/monai/bundle/reference_resolver.py +++ b/monai/bundle/reference_resolver.py @@ -14,7 +14,9 @@ import re import warnings from collections.abc import Sequence -from typing import Any, Iterator +from typing import Any + +from collections.abc import Iterator from monai.bundle.config_item import ConfigComponent, ConfigExpression, ConfigItem from monai.bundle.utils import DEPRECATED_ID_MAPPING, ID_REF_KEY, ID_SEP_KEY diff --git a/monai/bundle/workflows.py b/monai/bundle/workflows.py index ccdb08e208..f425d8fd28 100644 --- a/monai/bundle/workflows.py +++ b/monai/bundle/workflows.py @@ -19,7 +19,9 @@ from copy import copy from logging.config import fileConfig from pathlib import Path -from typing import Any, Sequence +from typing import Any + +from collections.abc import Sequence from monai.apps.utils import get_logger from monai.bundle.config_parser import ConfigParser diff --git a/monai/config/type_definitions.py b/monai/config/type_definitions.py index 57454a94e1..cf6a2957ee 100644 --- a/monai/config/type_definitions.py +++ b/monai/config/type_definitions.py @@ -12,7 +12,9 @@ from __future__ import annotations import os -from typing import Collection, Hashable, Iterable, Sequence, TypeVar, Union +from typing import TypeVar, Union + +from collections.abc import Collection, Hashable, Iterable, Sequence import numpy as np import torch diff --git a/monai/data/meta_obj.py b/monai/data/meta_obj.py index 0dccaa9e1c..d951d69b04 100644 --- a/monai/data/meta_obj.py +++ b/monai/data/meta_obj.py @@ -14,7 +14,9 @@ import itertools import pprint from copy import deepcopy -from typing import Any, Iterable +from typing import Any + +from collections.abc import Iterable import numpy as np import torch diff --git a/monai/data/meta_tensor.py b/monai/data/meta_tensor.py index 2df4da4a35..88e55c74da 100644 --- a/monai/data/meta_tensor.py +++ b/monai/data/meta_tensor.py @@ -14,7 +14,9 @@ import functools import warnings from copy import deepcopy -from typing import Any, Sequence +from typing import Any + +from collections.abc import Sequence import numpy as np import torch diff --git a/monai/engines/evaluator.py b/monai/engines/evaluator.py index 523c3dcbf6..8c0972ae7f 100644 --- a/monai/engines/evaluator.py +++ b/monai/engines/evaluator.py @@ -12,7 +12,9 @@ from __future__ import annotations import warnings -from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence +from typing import TYPE_CHECKING, Any, Callable + +from collections.abc import Iterable, Sequence import torch from torch.utils.data import DataLoader diff --git a/monai/engines/trainer.py b/monai/engines/trainer.py index bbcc9c880b..0ca9d40a26 100644 --- a/monai/engines/trainer.py +++ b/monai/engines/trainer.py @@ -12,7 +12,9 @@ from __future__ import annotations import warnings -from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence +from typing import TYPE_CHECKING, Any, Callable + +from collections.abc import Iterable, Sequence import torch from torch.optim.optimizer import Optimizer diff --git a/monai/engines/utils.py b/monai/engines/utils.py index 11a0000989..74612f7116 100644 --- a/monai/engines/utils.py +++ b/monai/engines/utils.py @@ -13,7 +13,9 @@ from abc import ABC, abstractmethod from collections.abc import Callable, Sequence -from typing import TYPE_CHECKING, Any, Mapping, cast +from typing import TYPE_CHECKING, Any, cast + +from collections.abc import Mapping import torch import torch.nn as nn diff --git a/monai/handlers/clearml_handlers.py b/monai/handlers/clearml_handlers.py index 1cfd6a33fb..11f9dca24f 100644 --- a/monai/handlers/clearml_handlers.py +++ b/monai/handlers/clearml_handlers.py @@ -11,7 +11,9 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Any, Mapping, Sequence +from typing import TYPE_CHECKING, Any + +from collections.abc import Mapping, Sequence from monai.utils import optional_import diff --git a/monai/inferers/utils.py b/monai/inferers/utils.py index bd99765348..8bb2941529 100644 --- a/monai/inferers/utils.py +++ b/monai/inferers/utils.py @@ -13,7 +13,9 @@ import itertools from collections.abc import Callable, Mapping, Sequence -from typing import Any, Iterable +from typing import Any + +from collections.abc import Iterable import numpy as np import torch diff --git a/monai/metrics/utils.py b/monai/metrics/utils.py index 340e54a1d7..43bd5a3483 100644 --- a/monai/metrics/utils.py +++ b/monai/metrics/utils.py @@ -14,7 +14,9 @@ import warnings from functools import lru_cache, partial from types import ModuleType -from typing import Any, Iterable, Sequence +from typing import Any + +from collections.abc import Iterable, Sequence import numpy as np import torch diff --git a/monai/transforms/intensity/dictionary.py b/monai/transforms/intensity/dictionary.py index 5dbac485fe..c5e7add79d 100644 --- a/monai/transforms/intensity/dictionary.py +++ b/monai/transforms/intensity/dictionary.py @@ -17,7 +17,9 @@ from __future__ import annotations -from typing import Callable, Hashable, Mapping, Sequence +from typing import Callable + +from collections.abc import Hashable, Mapping, Sequence import numpy as np diff --git a/monai/transforms/lazy/functional.py b/monai/transforms/lazy/functional.py index 6b95027832..518c74e414 100644 --- a/monai/transforms/lazy/functional.py +++ b/monai/transforms/lazy/functional.py @@ -11,7 +11,9 @@ from __future__ import annotations -from typing import Any, Mapping, Sequence +from typing import Any + +from collections.abc import Mapping, Sequence import torch diff --git a/monai/transforms/spatial/array.py b/monai/transforms/spatial/array.py index 6e39fb2e19..18ea0da23b 100644 --- a/monai/transforms/spatial/array.py +++ b/monai/transforms/spatial/array.py @@ -18,7 +18,9 @@ from collections.abc import Callable from copy import deepcopy from itertools import zip_longest -from typing import Any, Optional, Sequence, Tuple, Union, cast +from typing import Any, Optional, Union, cast + +from collections.abc import Sequence import numpy as np import torch @@ -116,7 +118,7 @@ "RandSimulateLowResolution", ] -RandRange = Optional[Union[Sequence[Union[Tuple[float, float], float]], float]] +RandRange = Optional[Union[Sequence[Union[tuple[float, float], float]], float]] class SpatialResample(InvertibleTransform, LazyTransform): diff --git a/monai/transforms/utility/dictionary.py b/monai/transforms/utility/dictionary.py index 79d0be522d..da43914aba 100644 --- a/monai/transforms/utility/dictionary.py +++ b/monai/transforms/utility/dictionary.py @@ -20,7 +20,9 @@ import re from collections.abc import Callable, Hashable, Mapping from copy import deepcopy -from typing import Any, Sequence, cast +from typing import Any, cast + +from collections.abc import Sequence import numpy as np import torch diff --git a/monai/transforms/utils_morphological_ops.py b/monai/transforms/utils_morphological_ops.py index b3134c1865..61d3c5b858 100644 --- a/monai/transforms/utils_morphological_ops.py +++ b/monai/transforms/utils_morphological_ops.py @@ -11,7 +11,7 @@ from __future__ import annotations -from typing import Sequence +from collections.abc import Sequence import torch import torch.nn.functional as F diff --git a/monai/utils/component_store.py b/monai/utils/component_store.py index d1e71eaebf..44be6efe94 100644 --- a/monai/utils/component_store.py +++ b/monai/utils/component_store.py @@ -14,7 +14,9 @@ from collections import namedtuple from keyword import iskeyword from textwrap import dedent, indent -from typing import Any, Callable, Iterable, TypeVar +from typing import Any, Callable, TypeVar + +from collections.abc import Iterable T = TypeVar("T") diff --git a/monai/utils/decorators.py b/monai/utils/decorators.py index 1c064468e8..0c3bf6ba6f 100644 --- a/monai/utils/decorators.py +++ b/monai/utils/decorators.py @@ -15,7 +15,9 @@ __all__ = ["RestartGenerator", "MethodReplacer"] -from typing import Callable, Generator +from typing import Callable + +from collections.abc import Generator class RestartGenerator: diff --git a/monai/utils/module.py b/monai/utils/module.py index df5fe873ae..f2acb28119 100644 --- a/monai/utils/module.py +++ b/monai/utils/module.py @@ -25,7 +25,9 @@ from pydoc import locate from re import match from types import FunctionType, ModuleType -from typing import Any, Iterable, cast +from typing import Any, cast + +from collections.abc import Iterable import torch diff --git a/monai/utils/state_cacher.py b/monai/utils/state_cacher.py index d37e7abde4..255e15a7e4 100644 --- a/monai/utils/state_cacher.py +++ b/monai/utils/state_cacher.py @@ -16,7 +16,9 @@ import pickle import tempfile from types import ModuleType -from typing import Any, Hashable +from typing import Any + +from collections.abc import Hashable import torch from torch.serialization import DEFAULT_PROTOCOL diff --git a/tests/test_compute_panoptic_quality.py b/tests/test_compute_panoptic_quality.py index a916ea32b2..a915eb64f9 100644 --- a/tests/test_compute_panoptic_quality.py +++ b/tests/test_compute_panoptic_quality.py @@ -12,7 +12,6 @@ from __future__ import annotations import unittest -from typing import List import numpy as np import torch @@ -104,7 +103,7 @@ def test_value_class(self, input_params, y_pred, y_gt, expected_value): metric = PanopticQualityMetric(**input_params) metric(y_pred, y_gt) outputs = metric.aggregate() - if isinstance(outputs, List): + if isinstance(outputs, list): for output, value in zip(outputs, expected_value): np.testing.assert_allclose(output.cpu().numpy(), np.asarray(value), atol=1e-4) else: diff --git a/tests/test_dynunet.py b/tests/test_dynunet.py index f3c982056c..e173049a95 100644 --- a/tests/test_dynunet.py +++ b/tests/test_dynunet.py @@ -13,7 +13,9 @@ import platform import unittest -from typing import Any, Sequence +from typing import Any + +from collections.abc import Sequence import torch from parameterized import parameterized diff --git a/tests/test_network_consistency.py b/tests/test_network_consistency.py index 4182501808..6949bb359f 100644 --- a/tests/test_network_consistency.py +++ b/tests/test_network_consistency.py @@ -15,7 +15,7 @@ import os import unittest from glob import glob -from typing import Sequence +from collections.abc import Sequence from unittest.case import skipIf import torch