Skip to content

Commit 2008a6c

Browse files
committed
fix(types): Use typing_extensions for 3.10
1 parent 0a1db80 commit 2008a6c

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies = [
3030
"scikit-image>=0.15.0",
3131
"scikit_learn>=1.3.0",
3232
"scipy>=1.8.0",
33+
"typing_extensions >=4.12",
3334
]
3435
dynamic = ["version"]
3536

src/nifreeze/data/base.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727
from collections import namedtuple
2828
from pathlib import Path
2929
from tempfile import mkdtemp
30-
from typing import Any, Generic, TypeVarTuple
30+
from typing import Any, Generic
3131

3232
import attr
3333
import h5py
3434
import nibabel as nb
3535
import numpy as np
3636
from nibabel.spatialimages import SpatialHeader, SpatialImage
3737
from nitransforms.linear import Affine
38+
from typing_extensions import TypeVarTuple, Unpack
3839

3940
from nifreeze.utils.ndimage import load_api
4041

@@ -58,7 +59,7 @@ def _cmp(lh: Any, rh: Any) -> bool:
5859

5960

6061
@attr.s(slots=True)
61-
class BaseDataset(Generic[*Ts]):
62+
class BaseDataset(Generic[Unpack[Ts]]):
6263
"""
6364
Base dataset representation structure.
6465
@@ -99,13 +100,12 @@ def __len__(self) -> int:
99100

100101
return self.dataobj.shape[-1]
101102

102-
def _getextra(self, idx: int | slice | tuple | np.ndarray) -> tuple[*Ts]:
103-
# PY312: Default values for TypeVarTuples are not yet supported
103+
def _getextra(self, idx: int | slice | tuple | np.ndarray) -> tuple[Unpack[Ts]]:
104104
return () # type: ignore[return-value]
105105

106106
def __getitem__(
107107
self, idx: int | slice | tuple | np.ndarray
108-
) -> tuple[np.ndarray, np.ndarray | None, *Ts]:
108+
) -> tuple[np.ndarray, np.ndarray | None, Unpack[Ts]]:
109109
"""
110110
Returns volume(s) and corresponding affine(s) through fancy indexing.
111111

src/nifreeze/estimator.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626

2727
from pathlib import Path
2828
from tempfile import TemporaryDirectory
29-
from typing import Self, TypeVar
29+
from typing import TypeVar
3030

3131
from tqdm import tqdm
32+
from typing_extensions import Self
3233

3334
from nifreeze.data.base import BaseDataset
3435
from nifreeze.model.base import BaseModel, ModelFactory

test/test_data_base.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,25 @@ def test_len(random_dataset: BaseDataset):
5353
assert len(random_dataset) == 5 # last dimension is 5 volumes
5454

5555

56-
def test_getitem_volume_index(random_dataset: BaseDataset[()]):
56+
def test_getitem_volume_index(random_dataset: BaseDataset):
5757
"""
5858
Test that __getitem__ returns the correct (volume, affine) tuple.
5959
6060
By default, motion_affines is None, so we expect to get None for the affine.
6161
"""
62-
# Single volume
63-
volume0, aff0 = random_dataset[0]
62+
# Single volume # Note that the type ignore can be removed once we can use *Ts
63+
volume0, aff0 = random_dataset[0] # type: ignore[misc] # PY310
6464
assert volume0.shape == (32, 32, 32)
6565
# No transforms have been applied yet, so there's no motion_affines array
6666
assert aff0 is None
6767

6868
# Slice of volumes
69-
volume_slice, aff_slice = random_dataset[2:4]
69+
volume_slice, aff_slice = random_dataset[2:4] # type: ignore[misc] # PY310
7070
assert volume_slice.shape == (32, 32, 32, 2)
7171
assert aff_slice is None
7272

7373

74-
def test_set_transform(random_dataset: BaseDataset[()]):
74+
def test_set_transform(random_dataset: BaseDataset):
7575
"""
7676
Test that calling set_transform changes the data and motion_affines.
7777
For simplicity, we'll apply an identity transform and check that motion_affines is updated.
@@ -83,7 +83,7 @@ def test_set_transform(random_dataset: BaseDataset[()]):
8383
random_dataset.set_transform(idx, affine, order=1)
8484

8585
# Data shouldn't have changed (since transform is identity).
86-
volume0, aff0 = random_dataset[idx]
86+
volume0, aff0 = random_dataset[idx] # type: ignore[misc] # PY310
8787
assert np.allclose(data_before, volume0)
8888

8989
# motion_affines should be created and match the transform matrix.

0 commit comments

Comments
 (0)