Skip to content

Commit

Permalink
Apply pyupgrade fixes for Python 3.9+ syntax (#8150)
Browse files Browse the repository at this point in the history
### Description

Included is a commit here applying pyupgrade changes for Python 3.8+
syntax. This should have been included in
104a360
when Python 3.7 support was dropped.

Also included is a commit here apply pyupgrade changes for Python 3.9+
syntax. This should have been included in
14b086b
when Python 3.8 support was dropped.

I've also run the pre-commit autoupdate command to use the latest
versions of the various pre-commit hook repos. It appears that
pre-commit.ci bot has not been doing this on the quarterly schedule as
expected? It appears the last time it submitted the PR to update the
pre-commit hook repos was back in
#6286 from April 2023.

### Types of changes
<!--- Put an `x` in all the boxes that apply, and remove the not
applicable items -->
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).

---------

Signed-off-by: James Butler <james.butler@revvity.com>
  • Loading branch information
jamesobutler authored Oct 24, 2024
1 parent 90173c9 commit 8dc3b4f
Show file tree
Hide file tree
Showing 38 changed files with 75 additions and 67 deletions.
23 changes: 10 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ci:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
Expand All @@ -26,33 +26,30 @@ repos:
args: ['--autofix', '--no-sort-keys', '--indent=4']
- id: end-of-file-fixer
- id: mixed-line-ending
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.3.5
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.0
hooks:
- id: ruff
args:
- --fix

- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
rev: v3.19.0
hooks:
- id: pyupgrade
args: [--py37-plus]
name: Upgrade code excluding monai networks
args: [--py39-plus, --keep-runtime-typing]
name: Upgrade code with exceptions
exclude: |
(?x)(
^versioneer.py|
^monai/_version.py|
^monai/networks/| # no PEP 604 for torchscript tensorrt
^monai/losses/ # no PEP 604 for torchscript tensorrt
^monai/networks/| # avoid typing rewrites
^monai/apps/detection/utils/anchor_utils.py| # avoid typing rewrites
^tests/test_compute_panoptic_quality.py # avoid typing rewrites
)
- id: pyupgrade
args: [--py37-plus, --keep-runtime-typing]
name: Upgrade monai networks
files: (?x)(^monai/networks/)
- repo: https://github.com/asottile/yesqa
rev: v1.4.0
rev: v1.5.0
hooks:
- id: yesqa
name: Unused noqa
Expand Down
4 changes: 2 additions & 2 deletions monai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

from __future__ import annotations

import logging
import os
import sys
import logging
import warnings
from ._version import get_versions

from ._version import get_versions

old_showwarning = warnings.showwarning

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
3 changes: 2 additions & 1 deletion monai/apps/detection/transforms/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

from __future__ import annotations

from typing import Any, Sequence
from collections.abc import Sequence
from typing import Any

import numpy as np
import torch
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
3 changes: 2 additions & 1 deletion monai/apps/pathology/engines/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

from __future__ import annotations

from typing import Any, Sequence
from collections.abc import Sequence
from typing import Any

import torch

Expand Down
3 changes: 2 additions & 1 deletion monai/apps/pathology/inferers/inferer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

from __future__ import annotations

from typing import Any, Callable, Sequence
from collections.abc import Sequence
from typing import Any, Callable

import numpy as np
import torch
Expand Down
3 changes: 2 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,8 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Any, Iterable
from collections.abc import Iterable
from typing import TYPE_CHECKING, Any

import numpy as np

Expand Down
3 changes: 2 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,8 @@
from __future__ import annotations

import warnings
from typing import Callable, Sequence
from collections.abc import Sequence
from typing import Callable

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: 2 additions & 2 deletions monai/bundle/reference_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

import re
import warnings
from collections.abc import Sequence
from typing import Any, Iterator
from collections.abc import Iterator, Sequence
from typing import Any

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
3 changes: 2 additions & 1 deletion monai/bundle/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
import sys
import time
from abc import ABC, abstractmethod
from collections.abc import Sequence
from copy import copy
from logging.config import fileConfig
from pathlib import Path
from typing import Any, Sequence
from typing import Any

from monai.apps.utils import get_logger
from monai.bundle.config_parser import ConfigParser
Expand Down
3 changes: 2 additions & 1 deletion monai/config/type_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from __future__ import annotations

import os
from typing import Collection, Hashable, Iterable, Sequence, TypeVar, Union
from collections.abc import Collection, Hashable, Iterable, Sequence
from typing import TypeVar, Union

import numpy as np
import torch
Expand Down
3 changes: 2 additions & 1 deletion monai/data/meta_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

import itertools
import pprint
from collections.abc import Iterable
from copy import deepcopy
from typing import Any, Iterable
from typing import Any

import numpy as np
import torch
Expand Down
3 changes: 2 additions & 1 deletion monai/data/meta_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

import functools
import warnings
from collections.abc import Sequence
from copy import deepcopy
from typing import Any, Sequence
from typing import Any

import numpy as np
import torch
Expand Down
3 changes: 2 additions & 1 deletion monai/engines/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from __future__ import annotations

import warnings
from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence
from collections.abc import Iterable, Sequence
from typing import TYPE_CHECKING, Any, Callable

import torch
from torch.utils.data import DataLoader
Expand Down
3 changes: 2 additions & 1 deletion monai/engines/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
from __future__ import annotations

import warnings
from typing import TYPE_CHECKING, Any, Callable, Iterable, Sequence
from collections.abc import Iterable, Sequence
from typing import TYPE_CHECKING, Any, Callable

import torch
from torch.optim.optimizer import Optimizer
Expand Down
4 changes: 2 additions & 2 deletions monai/engines/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from collections.abc import Callable, Sequence
from typing import TYPE_CHECKING, Any, Mapping, cast
from collections.abc import Callable, Mapping, Sequence
from typing import TYPE_CHECKING, Any, cast

import torch
import torch.nn as nn
Expand Down
3 changes: 2 additions & 1 deletion monai/handlers/clearml_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Any, Mapping, Sequence
from collections.abc import Mapping, Sequence
from typing import TYPE_CHECKING, Any

from monai.utils import optional_import

Expand Down
4 changes: 2 additions & 2 deletions monai/inferers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from __future__ import annotations

import itertools
from collections.abc import Callable, Mapping, Sequence
from typing import Any, Iterable
from collections.abc import Callable, Iterable, Mapping, Sequence
from typing import Any

import numpy as np
import torch
Expand Down
2 changes: 1 addition & 1 deletion monai/losses/hausdorff_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(
Incompatible values.
"""
super(HausdorffDTLoss, self).__init__(reduction=LossReduction(reduction).value)
super().__init__(reduction=LossReduction(reduction).value)
if other_act is not None and not callable(other_act):
raise TypeError(f"other_act must be None or callable but is {type(other_act).__name__}.")
if int(sigmoid) + int(softmax) > 1:
Expand Down
3 changes: 2 additions & 1 deletion monai/metrics/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
from __future__ import annotations

import warnings
from collections.abc import Iterable, Sequence
from functools import lru_cache, partial
from types import ModuleType
from typing import Any, Iterable, Sequence
from typing import Any

import numpy as np
import torch
Expand Down
2 changes: 1 addition & 1 deletion monai/networks/nets/swin_unetr.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

import itertools
from collections.abc import Sequence
from typing import Final

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.checkpoint as checkpoint
from torch.nn import LayerNorm
from typing_extensions import Final

from monai.networks.blocks import MLPBlock as Mlp
from monai.networks.blocks import PatchEmbed, UnetOutBlock, UnetrBasicBlock, UnetrUpBlock
Expand Down
3 changes: 2 additions & 1 deletion monai/transforms/intensity/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

from __future__ import annotations

from typing import Callable, Hashable, Mapping, Sequence
from collections.abc import Hashable, Mapping, Sequence
from typing import Callable

import numpy as np

Expand Down
3 changes: 2 additions & 1 deletion monai/transforms/lazy/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

from __future__ import annotations

from typing import Any, Mapping, Sequence
from collections.abc import Mapping, Sequence
from typing import Any

import torch

Expand Down
6 changes: 3 additions & 3 deletions monai/transforms/spatial/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
from __future__ import annotations

import warnings
from collections.abc import Callable
from collections.abc import Callable, Sequence
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

import numpy as np
import torch
Expand Down Expand Up @@ -116,7 +116,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: 2 additions & 2 deletions monai/transforms/utility/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
from __future__ import annotations

import re
from collections.abc import Callable, Hashable, Mapping
from collections.abc import Callable, Hashable, Mapping, Sequence
from copy import deepcopy
from typing import Any, Sequence, cast
from typing import Any, cast

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
Loading

0 comments on commit 8dc3b4f

Please sign in to comment.