Skip to content

Commit 6948625

Browse files
committed
typ: Consolidate typing_extensions imports
1 parent 6b14f84 commit 6948625

11 files changed

+52
-37
lines changed

nibabel/_typing.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Helpers for typing compatibility across Python versions"""
2+
3+
import sys
4+
5+
if sys.version_info < (3, 10):
6+
from typing_extensions import ParamSpec
7+
else:
8+
from typing import ParamSpec
9+
10+
if sys.version_info < (3, 11):
11+
from typing_extensions import Self
12+
else:
13+
from typing import Self
14+
15+
if sys.version_info < (3, 13):
16+
from typing_extensions import TypeVar
17+
else:
18+
from typing import TypeVar
19+
20+
21+
__all__ = [
22+
'ParamSpec',
23+
'Self',
24+
'TypeVar',
25+
]

nibabel/arrayproxy.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@
5959

6060
if ty.TYPE_CHECKING:
6161
import numpy.typing as npt
62-
from typing_extensions import Self # PY310
62+
63+
from ._typing import Self, TypeVar
6364

6465
# Taken from numpy/__init__.pyi
65-
_DType = ty.TypeVar('_DType', bound=np.dtype[ty.Any])
66+
_DType = TypeVar('_DType', bound=np.dtype[ty.Any])
6667

6768

6869
class ArrayLike(ty.Protocol):

nibabel/dataobj_images.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,18 @@
1313
import typing as ty
1414

1515
import numpy as np
16-
from typing_extensions import Self
1716

1817
from .deprecated import deprecate_with_version
1918
from .filebasedimages import FileBasedHeader, FileBasedImage
2019

2120
if ty.TYPE_CHECKING:
2221
import numpy.typing as npt
2322

23+
from ._typing import Self
2424
from .arrayproxy import ArrayLike
2525
from .fileholders import FileMap
2626
from .filename_parser import FileSpec
2727

28-
ArrayImgT = ty.TypeVar('ArrayImgT', bound='DataobjImage')
29-
3028

3129
class DataobjImage(FileBasedImage):
3230
"""Template class for images that have dataobj data stores"""

nibabel/deprecated.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@
55
import typing as ty
66
import warnings
77

8+
from ._typing import ParamSpec
89
from .deprecator import Deprecator
910
from .pkg_info import cmp_pkg_version
1011

11-
if ty.TYPE_CHECKING:
12-
# PY39: ParamSpec is available in Python 3.10+
13-
P = ty.ParamSpec('P')
14-
else:
15-
# Just to keep the runtime happy
16-
P = ty.TypeVar('P')
12+
P = ParamSpec('P')
1713

1814

1915
class ModuleProxy:

nibabel/filebasedimages.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,17 @@
1515
from copy import deepcopy
1616
from urllib import request
1717

18-
from typing_extensions import Self
19-
2018
from ._compression import COMPRESSION_ERRORS
2119
from .fileholders import FileHolder, FileMap
2220
from .filename_parser import TypesFilenamesError, _stringify_path, splitext_addext, types_filenames
2321
from .openers import ImageOpener
2422

2523
if ty.TYPE_CHECKING:
24+
from ._typing import Self
2625
from .filename_parser import ExtensionSpec, FileSpec
2726

2827
FileSniff = tuple[bytes, str]
2928

30-
ImgT = ty.TypeVar('ImgT', bound='FileBasedImage')
31-
HdrT = ty.TypeVar('HdrT', bound='FileBasedHeader')
32-
33-
StreamImgT = ty.TypeVar('StreamImgT', bound='SerializableImage')
34-
3529

3630
class ImageFileError(Exception):
3731
pass

nibabel/loadsave.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from __future__ import annotations
1313

1414
import os
15-
import typing as ty
1615

1716
import numpy as np
1817

@@ -26,13 +25,17 @@
2625
_compressed_suffixes = ('.gz', '.bz2', '.zst')
2726

2827

29-
if ty.TYPE_CHECKING:
28+
TYPE_CHECKING = False
29+
if TYPE_CHECKING:
30+
from typing import TypedDict
31+
32+
from ._typing import ParamSpec
3033
from .filebasedimages import FileBasedImage
3134
from .filename_parser import FileSpec
3235

33-
P = ty.ParamSpec('P')
36+
P = ParamSpec('P')
3437

35-
class Signature(ty.TypedDict):
38+
class Signature(TypedDict):
3639
signature: bytes
3740
format_name: str
3841

nibabel/nifti1.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,15 @@
1414
from __future__ import annotations
1515

1616
import json
17-
import sys
1817
import typing as ty
1918
import warnings
2019
from io import BytesIO
2120

2221
import numpy as np
2322
import numpy.linalg as npl
2423

25-
if sys.version_info < (3, 13):
26-
from typing_extensions import Self, TypeVar # PY312
27-
else:
28-
from typing import Self, TypeVar
29-
3024
from . import analyze # module import
25+
from ._typing import Self, TypeVar
3126
from .arrayproxy import get_obj_dtype
3227
from .batteryrunners import Report
3328
from .casting import have_binary128

nibabel/openers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
from types import TracebackType
2323

2424
from _typeshed import WriteableBuffer
25-
from typing_extensions import Self
25+
26+
from ._typing import Self
2627

2728
ModeRT = ty.Literal['r', 'rt']
2829
ModeRB = ty.Literal['rb']

nibabel/pointset.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
from nibabel.spatialimages import SpatialImage
3232

3333
if ty.TYPE_CHECKING:
34-
from typing_extensions import Self
34+
from ._typing import Self, TypeVar
3535

36-
_DType = ty.TypeVar('_DType', bound=np.dtype[ty.Any])
36+
_DType = TypeVar('_DType', bound=np.dtype[ty.Any])
3737

3838

3939
class CoordinateArray(ty.Protocol):

nibabel/spatialimages.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@
137137
from typing import Literal
138138

139139
import numpy as np
140-
from typing_extensions import Self
141140

141+
from ._typing import TypeVar
142142
from .casting import sctypes_aliases
143143
from .dataobj_images import DataobjImage
144144
from .filebasedimages import FileBasedHeader, FileBasedImage
@@ -153,11 +153,11 @@
153153

154154
import numpy.typing as npt
155155

156+
from ._typing import Self
156157
from .arrayproxy import ArrayLike
157158
from .fileholders import FileMap
158159

159-
SpatialImgT = ty.TypeVar('SpatialImgT', bound='SpatialImage')
160-
SpatialHdrT = ty.TypeVar('SpatialHdrT', bound='SpatialHeader')
160+
SpatialImgT = TypeVar('SpatialImgT', bound='SpatialImage')
161161

162162

163163
class HasDtype(ty.Protocol):

nibabel/volumeutils.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828

2929
import numpy.typing as npt
3030

31+
from ._typing import TypeVar
32+
3133
Scalar = np.number | float
3234

33-
K = ty.TypeVar('K')
34-
V = ty.TypeVar('V')
35-
DT = ty.TypeVar('DT', bound=np.generic)
35+
K = TypeVar('K')
36+
V = TypeVar('V')
37+
DT = TypeVar('DT', bound=np.generic)
3638

3739
sys_is_le = sys.byteorder == 'little'
3840
native_code: ty.Literal['<', '>'] = '<' if sys_is_le else '>'

0 commit comments

Comments
 (0)