From 0a27464e27682b48de188e4bf4e97b91c0c8fdd8 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:19:08 +0200 Subject: [PATCH] STY: Apply ruff/flake8-type-checking rule TCH003 TCH003 Move standard library import into a type-checking block --- nibabel/_compression.py | 3 ++- nibabel/fileholders.py | 4 +++- nibabel/optpkg.py | 4 +++- nibabel/spatialimages.py | 5 +++-- nibabel/testing/__init__.py | 3 +-- nibabel/volumeutils.py | 3 ++- 6 files changed, 14 insertions(+), 8 deletions(-) diff --git a/nibabel/_compression.py b/nibabel/_compression.py index f697fa54c..871be2629 100644 --- a/nibabel/_compression.py +++ b/nibabel/_compression.py @@ -12,12 +12,13 @@ import bz2 import gzip -import io import typing as ty from .optpkg import optional_package if ty.TYPE_CHECKING: + import io + import indexed_gzip # type: ignore[import] import pyzstd diff --git a/nibabel/fileholders.py b/nibabel/fileholders.py index 3db4c62a9..df7c34af6 100644 --- a/nibabel/fileholders.py +++ b/nibabel/fileholders.py @@ -10,12 +10,14 @@ from __future__ import annotations -import io import typing as ty from copy import copy from .openers import ImageOpener +if ty.TYPE_CHECKING: + import io + class FileHolderError(Exception): pass diff --git a/nibabel/optpkg.py b/nibabel/optpkg.py index bfe6a629c..90b8ded51 100644 --- a/nibabel/optpkg.py +++ b/nibabel/optpkg.py @@ -3,12 +3,14 @@ from __future__ import annotations import typing as ty -from types import ModuleType from packaging.version import Version from .tripwire import TripWire +if ty.TYPE_CHECKING: + from types import ModuleType + def _check_pkg_version(min_version: str | Version) -> ty.Callable[[ModuleType], bool]: min_ver = Version(min_version) if isinstance(min_version, str) else min_version diff --git a/nibabel/spatialimages.py b/nibabel/spatialimages.py index bd5ff8c11..ce8ee3c6e 100644 --- a/nibabel/spatialimages.py +++ b/nibabel/spatialimages.py @@ -132,9 +132,7 @@ from __future__ import annotations -import io import typing as ty -from collections.abc import Sequence from typing import Literal import numpy as np @@ -153,6 +151,9 @@ from functools import lru_cache as cache if ty.TYPE_CHECKING: + import io + from collections.abc import Sequence + import numpy.typing as npt from .arrayproxy import ArrayLike diff --git a/nibabel/testing/__init__.py b/nibabel/testing/__init__.py index f41c657f5..be111747b 100644 --- a/nibabel/testing/__init__.py +++ b/nibabel/testing/__init__.py @@ -27,11 +27,10 @@ from .np_features import memmap_after_ufunc if ty.TYPE_CHECKING: - from importlib_resources.abc import Traversable + from importlib.resources.abc import Traversable try: from importlib.resources import as_file, files - from importlib.resources.abc import Traversable except ImportError: # PY38 from importlib_resources import as_file, files diff --git a/nibabel/volumeutils.py b/nibabel/volumeutils.py index c2387f094..6e43f7918 100644 --- a/nibabel/volumeutils.py +++ b/nibabel/volumeutils.py @@ -10,7 +10,6 @@ from __future__ import annotations -import io import sys import typing as ty import warnings @@ -25,6 +24,8 @@ from .externals.oset import OrderedSet if ty.TYPE_CHECKING: + import io + import numpy.typing as npt Scalar = np.number | float