Skip to content

Commit d14d662

Browse files
committed
Apply pyupgrade for Python 3.9+ syntax
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]>
1 parent 2a2a1fb commit d14d662

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+144
-92
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ repos:
3737
rev: v3.18.0
3838
hooks:
3939
- id: pyupgrade
40-
args: [--py38-plus]
40+
args: [--py39-plus]
4141
name: Upgrade code excluding monai networks
4242
exclude: |
4343
(?x)(
@@ -49,7 +49,7 @@ repos:
4949
^monai/data/grid_dataset.py
5050
)
5151
- id: pyupgrade
52-
args: [--py38-plus, --keep-runtime-typing]
52+
args: [--py39-plus, --keep-runtime-typing]
5353
name: Upgrade monai networks
5454
files: (?x)(
5555
^monai/networks/|

monai/apps/detection/networks/retinanet_network.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import math
4343
import warnings
4444
from collections.abc import Callable, Sequence
45-
from typing import Any, Dict
45+
from typing import Any
4646

4747
import torch
4848
from torch import Tensor, nn
@@ -330,7 +330,7 @@ def forward(self, images: Tensor) -> Any:
330330
features = self.feature_extractor(images)
331331
if isinstance(features, Tensor):
332332
feature_maps = [features]
333-
elif torch.jit.isinstance(features, Dict[str, Tensor]):
333+
elif torch.jit.isinstance(features, dict[str, Tensor]):
334334
feature_maps = list(features.values())
335335
else:
336336
feature_maps = list(features)

monai/apps/detection/transforms/array.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
from __future__ import annotations
1717

18-
from typing import Any, Sequence
18+
from typing import Any
19+
20+
from collections.abc import Sequence
1921

2022
import numpy as np
2123
import torch

monai/apps/detection/utils/anchor_utils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939

4040
from __future__ import annotations
4141

42-
from typing import List, Sequence
42+
43+
from collections.abc import Sequence
4344

4445
import torch
4546
from torch import Tensor, nn
@@ -106,7 +107,7 @@ class AnchorGenerator(nn.Module):
106107
anchor_generator = AnchorGenerator(sizes, aspect_ratios)
107108
"""
108109

109-
__annotations__ = {"cell_anchors": List[torch.Tensor]}
110+
__annotations__ = {"cell_anchors": list[torch.Tensor]}
110111

111112
def __init__(
112113
self,
@@ -364,7 +365,7 @@ class AnchorGeneratorWithAnchorShape(AnchorGenerator):
364365
anchor_generator = AnchorGeneratorWithAnchorShape(feature_map_scales, base_anchor_shapes)
365366
"""
366367

367-
__annotations__ = {"cell_anchors": List[torch.Tensor]}
368+
__annotations__ = {"cell_anchors": list[torch.Tensor]}
368369

369370
def __init__(
370371
self,

monai/apps/generation/maisi/networks/autoencoderkl_maisi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import gc
1515
import logging
16-
from typing import Sequence
16+
from collections.abc import Sequence
1717

1818
import torch
1919
import torch.nn as nn

monai/apps/generation/maisi/networks/controlnet_maisi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import Sequence
14+
from collections.abc import Sequence
1515

1616
import torch
1717

monai/apps/pathology/engines/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import Any, Sequence
14+
from typing import Any
15+
16+
from collections.abc import Sequence
1517

1618
import torch
1719

monai/apps/pathology/inferers/inferer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import Any, Callable, Sequence
14+
from typing import Any, Callable
15+
16+
from collections.abc import Sequence
1517

1618
import numpy as np
1719
import torch

monai/apps/pathology/metrics/lesion_froc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import TYPE_CHECKING, Any, Iterable
14+
from typing import TYPE_CHECKING, Any
15+
16+
from collections.abc import Iterable
1517

1618
import numpy as np
1719

monai/apps/pathology/transforms/post/array.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
from __future__ import annotations
1313

1414
import warnings
15-
from typing import Callable, Sequence
15+
from typing import Callable
16+
17+
from collections.abc import Sequence
1618

1719
import numpy as np
1820
import torch

monai/apps/tcia/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from __future__ import annotations
1313

1414
import os
15-
from typing import Iterable
15+
from collections.abc import Iterable
1616

1717
import monai
1818
from monai.config.type_definitions import PathLike

monai/apps/utils.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,7 @@ def check_hash(filepath: PathLike, val: str | None = None, hash_type: str = "md5
136136
return True
137137
actual_hash_func = look_up_option(hash_type.lower(), SUPPORTED_HASH_TYPES)
138138

139-
if sys.version_info >= (3, 9):
140-
actual_hash = actual_hash_func(usedforsecurity=False) # allows checks on FIPS enabled machines
141-
else:
142-
actual_hash = actual_hash_func()
139+
actual_hash = actual_hash_func(usedforsecurity=False) # allows checks on FIPS enabled machines
143140

144141
try:
145142
with open(filepath, "rb") as f:

monai/apps/vista3d/transforms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from __future__ import annotations
1313

1414
import warnings
15-
from typing import Sequence
15+
from collections.abc import Sequence
1616

1717
import numpy as np
1818
import torch

monai/bundle/reference_resolver.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import re
1515
import warnings
1616
from collections.abc import Sequence
17-
from typing import Any, Iterator
17+
from typing import Any
18+
19+
from collections.abc import Iterator
1820

1921
from monai.bundle.config_item import ConfigComponent, ConfigExpression, ConfigItem
2022
from monai.bundle.utils import DEPRECATED_ID_MAPPING, ID_REF_KEY, ID_SEP_KEY

monai/bundle/workflows.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
from copy import copy
2020
from logging.config import fileConfig
2121
from pathlib import Path
22-
from typing import Any, Sequence
22+
from typing import Any
23+
24+
from collections.abc import Sequence
2325

2426
from monai.apps.utils import get_logger
2527
from monai.bundle.config_parser import ConfigParser

monai/config/type_definitions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
from __future__ import annotations
1313

1414
import os
15-
from typing import Collection, Hashable, Iterable, Sequence, TypeVar, Union
15+
from typing import TypeVar, Union
16+
17+
from collections.abc import Collection, Hashable, Iterable, Sequence
1618

1719
import numpy as np
1820
import torch

monai/data/meta_obj.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import itertools
1515
import pprint
1616
from copy import deepcopy
17-
from typing import Any, Iterable
17+
from typing import Any
18+
19+
from collections.abc import Iterable
1820

1921
import numpy as np
2022
import torch

monai/data/meta_tensor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import functools
1515
import warnings
1616
from copy import deepcopy
17-
from typing import Any, Sequence
17+
from typing import Any
18+
19+
from collections.abc import Sequence
1820

1921
import numpy as np
2022
import torch

monai/engines/evaluator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
from __future__ import annotations
1313

1414
import warnings
15-
from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence
15+
from typing import TYPE_CHECKING, Any, Callable
16+
17+
from collections.abc import Iterable, Sequence
1618

1719
import torch
1820
from torch.utils.data import DataLoader

monai/engines/trainer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
from __future__ import annotations
1313

1414
import warnings
15-
from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence
15+
from typing import TYPE_CHECKING, Any, Callable
16+
17+
from collections.abc import Iterable, Sequence
1618

1719
import torch
1820
from torch.optim.optimizer import Optimizer

monai/engines/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
from abc import ABC, abstractmethod
1515
from collections.abc import Callable, Sequence
16-
from typing import TYPE_CHECKING, Any, Mapping, cast
16+
from typing import TYPE_CHECKING, Any, cast
17+
18+
from collections.abc import Mapping
1719

1820
import torch
1921
import torch.nn as nn

monai/handlers/clearml_handlers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import TYPE_CHECKING, Any, Mapping, Sequence
14+
from typing import TYPE_CHECKING, Any
15+
16+
from collections.abc import Mapping, Sequence
1517

1618
from monai.utils import optional_import
1719

monai/inferers/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
import itertools
1515
from collections.abc import Callable, Mapping, Sequence
16-
from typing import Any, Iterable
16+
from typing import Any
17+
18+
from collections.abc import Iterable
1719

1820
import numpy as np
1921
import torch

monai/metrics/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import warnings
1515
from functools import lru_cache, partial
1616
from types import ModuleType
17-
from typing import Any, Iterable, Sequence
17+
from typing import Any
18+
19+
from collections.abc import Iterable, Sequence
1820

1921
import numpy as np
2022
import torch

monai/networks/blocks/attention_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
from __future__ import annotations
1111

12-
from typing import Tuple
1312

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

5150

5251
def add_decomposed_rel_pos(
53-
attn: torch.Tensor, q: torch.Tensor, rel_pos_lst: nn.ParameterList, q_size: Tuple, k_size: Tuple
52+
attn: torch.Tensor, q: torch.Tensor, rel_pos_lst: nn.ParameterList, q_size: tuple, k_size: tuple
5453
) -> torch.Tensor:
5554
r"""
5655
Calculate decomposed Relative Positional Embeddings from mvitv2 implementation:

monai/networks/blocks/crossattention.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import Optional, Tuple
14+
from typing import Optional
1515

1616
import torch
1717
import torch.nn as nn
@@ -42,7 +42,7 @@ def __init__(
4242
causal: bool = False,
4343
sequence_length: int | None = None,
4444
rel_pos_embedding: Optional[str] = None,
45-
input_size: Optional[Tuple] = None,
45+
input_size: Optional[tuple] = None,
4646
attention_dtype: Optional[torch.dtype] = None,
4747
use_flash_attention: bool = False,
4848
) -> None:

monai/networks/blocks/denseblock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import Sequence
14+
from collections.abc import Sequence
1515

1616
import torch
1717
import torch.nn as nn

monai/networks/blocks/pos_embed_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import collections.abc
1515
from itertools import repeat
16-
from typing import List, Union
16+
from typing import Union
1717

1818
import torch
1919
import torch.nn as nn
@@ -33,7 +33,7 @@ def parse(x):
3333

3434

3535
def build_sincos_position_embedding(
36-
grid_size: Union[int, List[int]], embed_dim: int, spatial_dims: int = 3, temperature: float = 10000.0
36+
grid_size: Union[int, list[int]], embed_dim: int, spatial_dims: int = 3, temperature: float = 10000.0
3737
) -> torch.nn.Parameter:
3838
"""
3939
Builds a sin-cos position embedding based on the given grid size, embed dimension, spatial dimensions, and temperature.

monai/networks/blocks/rel_pos_embedding.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
from __future__ import annotations
1111

12-
from typing import Iterable, Tuple
12+
13+
from collections.abc import Iterable
1314

1415
import torch
1516
from torch import nn
@@ -19,7 +20,7 @@
1920

2021

2122
class DecomposedRelativePosEmbedding(nn.Module):
22-
def __init__(self, s_input_dims: Tuple[int, int] | Tuple[int, int, int], c_dim: int, num_heads: int) -> None:
23+
def __init__(self, s_input_dims: tuple[int, int] | tuple[int, int, int], c_dim: int, num_heads: int) -> None:
2324
"""
2425
Args:
2526
s_input_dims (Tuple): input spatial dimension. (H, W) or (H, W, D)

monai/networks/blocks/selfattention.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from __future__ import annotations
1313

14-
from typing import Tuple, Union
14+
from typing import Union
1515

1616
import torch
1717
import torch.nn as nn
@@ -41,7 +41,7 @@ def __init__(
4141
causal: bool = False,
4242
sequence_length: int | None = None,
4343
rel_pos_embedding: str | None = None,
44-
input_size: Tuple | None = None,
44+
input_size: tuple | None = None,
4545
attention_dtype: torch.dtype | None = None,
4646
include_fc: bool = True,
4747
use_combined_linear: bool = True,

monai/networks/layers/simplelayers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
import math
1515
from copy import deepcopy
16-
from typing import Sequence
16+
from collections.abc import Sequence
1717

1818
import torch
1919
import torch.nn.functional as F

0 commit comments

Comments
 (0)