Skip to content

Commit

Permalink
Apply pyupgrade for Python 3.9+ syntax
Browse files Browse the repository at this point in the history
This should have been included in 14b086b when Python 3.8 support was dropped.

Changes were automatically applied by running:
pre_commit run --all-files

Signed-off-by: James Butler <[email protected]>
  • Loading branch information
jamesobutler committed Oct 23, 2024
1 parent a2be24a commit 0f797d7
Show file tree
Hide file tree
Showing 36 changed files with 91 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)(
Expand Down
4 changes: 2 additions & 2 deletions monai/apps/detection/networks/retinanet_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion monai/apps/detection/transforms/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions monai/apps/detection/utils/anchor_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import gc
import logging
from typing import Sequence
from collections.abc import Sequence

import torch
import torch.nn as nn
Expand Down
2 changes: 1 addition & 1 deletion monai/apps/generation/maisi/networks/controlnet_maisi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from __future__ import annotations

from typing import Sequence
from collections.abc import Sequence

import torch

Expand Down
4 changes: 3 additions & 1 deletion monai/apps/pathology/engines/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

from __future__ import annotations

from typing import Any, Sequence
from typing import Any

from collections.abc import Sequence

import torch

Expand Down
4 changes: 3 additions & 1 deletion monai/apps/pathology/inferers/inferer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/apps/pathology/metrics/lesion_froc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion monai/apps/pathology/transforms/post/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion monai/apps/tcia/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions monai/apps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion monai/apps/vista3d/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/bundle/reference_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/bundle/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/config/type_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/data/meta_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/data/meta_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/engines/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/engines/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/engines/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/handlers/clearml_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion monai/inferers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/metrics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/transforms/intensity/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion monai/transforms/lazy/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions monai/transforms/spatial/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion monai/transforms/utility/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion monai/transforms/utils_morphological_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion monai/utils/component_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 3 additions & 1 deletion monai/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

__all__ = ["RestartGenerator", "MethodReplacer"]

from typing import Callable, Generator
from typing import Callable

from collections.abc import Generator


class RestartGenerator:
Expand Down
4 changes: 3 additions & 1 deletion monai/utils/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion monai/utils/state_cacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 0f797d7

Please sign in to comment.