Skip to content

Commit

Permalink
Merge pull request #1358 from DimitriPapadopoulos/TCH
Browse files Browse the repository at this point in the history
STY: Enforce ruff/flake8-type-checking rules (TCH)
  • Loading branch information
effigies authored Sep 22, 2024
2 parents 4c216b1 + 8ca899a commit 7b3c1b2
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 14 deletions.
3 changes: 2 additions & 1 deletion nibabel/_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions nibabel/dataobj_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

import numpy as np

from .arrayproxy import ArrayLike
from .deprecated import deprecate_with_version
from .filebasedimages import FileBasedHeader, FileBasedImage
from .fileholders import FileMap

if ty.TYPE_CHECKING:
import numpy.typing as npt

from .arrayproxy import ArrayLike
from .fileholders import FileMap
from .filename_parser import FileSpec

ArrayImgT = ty.TypeVar('ArrayImgT', bound='DataobjImage')
Expand Down
4 changes: 3 additions & 1 deletion nibabel/fileholders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions nibabel/imageclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

from __future__ import annotations

from typing import TYPE_CHECKING

from .analyze import AnalyzeImage
from .brikhead import AFNIImage
from .cifti2 import Cifti2Image
from .dataobj_images import DataobjImage
from .filebasedimages import FileBasedImage
from .freesurfer import MGHImage
from .gifti import GiftiImage
from .minc1 import Minc1Image
Expand All @@ -25,6 +25,10 @@
from .spm2analyze import Spm2AnalyzeImage
from .spm99analyze import Spm99AnalyzeImage

if TYPE_CHECKING:
from .dataobj_images import DataobjImage
from .filebasedimages import FileBasedImage

# Ordered by the load/save priority.
all_image_classes: list[type[FileBasedImage]] = [
Nifti1Pair,
Expand Down
4 changes: 3 additions & 1 deletion nibabel/optpkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions nibabel/spatialimages.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,14 @@

from __future__ import annotations

import io
import typing as ty
from collections.abc import Sequence
from typing import Literal

import numpy as np

from .arrayproxy import ArrayLike
from .casting import sctypes_aliases
from .dataobj_images import DataobjImage
from .filebasedimages import FileBasedHeader, FileBasedImage
from .fileholders import FileMap
from .fileslice import canonical_slicers
from .orientations import apply_orientation, inv_ornt_aff
from .viewers import OrthoSlicer3D
Expand All @@ -155,8 +151,14 @@
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
from .fileholders import FileMap

SpatialImgT = ty.TypeVar('SpatialImgT', bound='SpatialImage')
SpatialHdrT = ty.TypeVar('SpatialHdrT', bound='SpatialHeader')

Expand Down
5 changes: 3 additions & 2 deletions nibabel/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
from .helpers import assert_data_similar, bytesio_filemap, bytesio_round_trip
from .np_features import memmap_after_ufunc

if ty.TYPE_CHECKING:
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
from importlib_resources.abc import Traversable


def get_test_data(
Expand Down
3 changes: 2 additions & 1 deletion nibabel/volumeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from __future__ import annotations

import io
import sys
import typing as ty
import warnings
Expand All @@ -25,6 +24,8 @@
from .externals.oset import OrderedSet

if ty.TYPE_CHECKING:
import io

import numpy.typing as npt

Scalar = np.number | float
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ select = [
"PLE",
"Q",
"RSE",
"TCH",
"UP",
]
ignore = [
Expand Down

0 comments on commit 7b3c1b2

Please sign in to comment.