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 15, 2024
1 parent 2a2a1fb commit d14d662
Show file tree
Hide file tree
Showing 52 changed files with 144 additions and 92 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ repos:
rev: v3.18.0
hooks:
- id: pyupgrade
args: [--py38-plus]
args: [--py39-plus]
name: Upgrade code excluding monai networks
exclude: |
(?x)(
Expand All @@ -49,7 +49,7 @@ repos:
^monai/data/grid_dataset.py
)
- id: pyupgrade
args: [--py38-plus, --keep-runtime-typing]
args: [--py39-plus, --keep-runtime-typing]
name: Upgrade monai networks
files: (?x)(
^monai/networks/|
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
3 changes: 1 addition & 2 deletions monai/networks/blocks/attention_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from __future__ import annotations

from typing import Tuple

import torch
import torch.nn.functional as F
Expand Down Expand Up @@ -50,7 +49,7 @@ def get_rel_pos(q_size: int, k_size: int, rel_pos: torch.Tensor) -> torch.Tensor


def add_decomposed_rel_pos(
attn: torch.Tensor, q: torch.Tensor, rel_pos_lst: nn.ParameterList, q_size: Tuple, k_size: Tuple
attn: torch.Tensor, q: torch.Tensor, rel_pos_lst: nn.ParameterList, q_size: tuple, k_size: tuple
) -> torch.Tensor:
r"""
Calculate decomposed Relative Positional Embeddings from mvitv2 implementation:
Expand Down
4 changes: 2 additions & 2 deletions monai/networks/blocks/crossattention.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from __future__ import annotations

from typing import Optional, Tuple
from typing import Optional

import torch
import torch.nn as nn
Expand Down Expand Up @@ -42,7 +42,7 @@ def __init__(
causal: bool = False,
sequence_length: int | None = None,
rel_pos_embedding: Optional[str] = None,
input_size: Optional[Tuple] = None,
input_size: Optional[tuple] = None,
attention_dtype: Optional[torch.dtype] = None,
use_flash_attention: bool = False,
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion monai/networks/blocks/denseblock.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 as nn
Expand Down
4 changes: 2 additions & 2 deletions monai/networks/blocks/pos_embed_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import collections.abc
from itertools import repeat
from typing import List, Union
from typing import Union

import torch
import torch.nn as nn
Expand All @@ -33,7 +33,7 @@ def parse(x):


def build_sincos_position_embedding(
grid_size: Union[int, List[int]], embed_dim: int, spatial_dims: int = 3, temperature: float = 10000.0
grid_size: Union[int, list[int]], embed_dim: int, spatial_dims: int = 3, temperature: float = 10000.0
) -> torch.nn.Parameter:
"""
Builds a sin-cos position embedding based on the given grid size, embed dimension, spatial dimensions, and temperature.
Expand Down
5 changes: 3 additions & 2 deletions monai/networks/blocks/rel_pos_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

from __future__ import annotations

from typing import Iterable, Tuple

from collections.abc import Iterable

import torch
from torch import nn
Expand All @@ -19,7 +20,7 @@


class DecomposedRelativePosEmbedding(nn.Module):
def __init__(self, s_input_dims: Tuple[int, int] | Tuple[int, int, int], c_dim: int, num_heads: int) -> None:
def __init__(self, s_input_dims: tuple[int, int] | tuple[int, int, int], c_dim: int, num_heads: int) -> None:
"""
Args:
s_input_dims (Tuple): input spatial dimension. (H, W) or (H, W, D)
Expand Down
4 changes: 2 additions & 2 deletions monai/networks/blocks/selfattention.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from __future__ import annotations

from typing import Tuple, Union
from typing import Union

import torch
import torch.nn as nn
Expand Down Expand Up @@ -41,7 +41,7 @@ def __init__(
causal: bool = False,
sequence_length: int | None = None,
rel_pos_embedding: str | None = None,
input_size: Tuple | None = None,
input_size: tuple | None = None,
attention_dtype: torch.dtype | None = None,
include_fc: bool = True,
use_combined_linear: bool = True,
Expand Down
2 changes: 1 addition & 1 deletion monai/networks/layers/simplelayers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import math
from copy import deepcopy
from typing import Sequence
from collections.abc import Sequence

import torch
import torch.nn.functional as F
Expand Down
Loading

0 comments on commit d14d662

Please sign in to comment.