Skip to content

Commit f1f6cfe

Browse files
committed
rf: Hide typing backports in helper module
1 parent 871b884 commit f1f6cfe

12 files changed

+54
-29
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

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import numpy as np
1616

17+
from ._typing import TypeVar
1718
from .deprecated import deprecate_with_version
1819
from .filebasedimages import FileBasedHeader, FileBasedImage
1920

@@ -24,13 +25,13 @@
2425
from .fileholders import FileMap
2526
from .filename_parser import FileSpec
2627

27-
FT = ty.TypeVar('FT', bound=np.floating)
28+
FT = TypeVar('FT', bound=np.floating)
2829
F16 = ty.Literal['float16', 'f2', '|f2', '=f2', '<f2', '>f2']
2930
F32 = ty.Literal['float32', 'f4', '|f4', '=f4', '<f4', '>f4']
3031
F64 = ty.Literal['float64', 'f8', '|f8', '=f8', '<f8', '>f8']
3132
Caching = ty.Literal['fill', 'unchanged']
3233

33-
ArrayImgT = ty.TypeVar('ArrayImgT', bound='DataobjImage')
34+
ArrayImgT = TypeVar('ArrayImgT', bound='DataobjImage')
3435

3536

3637
class DataobjImage(FileBasedImage):

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/deprecator.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
from textwrap import dedent
1111

1212
if ty.TYPE_CHECKING:
13-
T = ty.TypeVar('T')
14-
P = ty.ParamSpec('P')
13+
from ._typing import ParamSpec, TypeVar
14+
15+
T = TypeVar('T')
16+
P = ParamSpec('P')
1517

1618
_LEADING_WHITE = re.compile(r'^(\s*)')
1719

nibabel/filebasedimages.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from urllib import request
1717

1818
from ._compression import COMPRESSION_ERRORS
19+
from ._typing import TypeVar
1920
from .fileholders import FileHolder, FileMap
2021
from .filename_parser import TypesFilenamesError, _stringify_path, splitext_addext, types_filenames
2122
from .openers import ImageOpener
@@ -25,10 +26,10 @@
2526

2627
FileSniff = tuple[bytes, str]
2728

28-
ImgT = ty.TypeVar('ImgT', bound='FileBasedImage')
29-
HdrT = ty.TypeVar('HdrT', bound='FileBasedHeader')
29+
ImgT = TypeVar('ImgT', bound='FileBasedImage')
30+
HdrT = TypeVar('HdrT', bound='FileBasedHeader')
3031

31-
StreamImgT = ty.TypeVar('StreamImgT', bound='SerializableImage')
32+
StreamImgT = TypeVar('StreamImgT', bound='SerializableImage')
3233

3334

3435
class ImageFileError(Exception):

nibabel/loadsave.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727

2828

2929
if ty.TYPE_CHECKING:
30+
from ._typing import ParamSpec
3031
from .filebasedimages import FileBasedImage
3132
from .filename_parser import FileSpec
3233

33-
P = ty.ParamSpec('P')
34+
P = ParamSpec('P')
3435

3536
class Signature(ty.TypedDict):
3637
signature: bytes

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@
138138

139139
import numpy as np
140140
import numpy.typing as npt
141-
from typing_extensions import TypeVar
142141

142+
from ._typing import TypeVar
143143
from .casting import sctypes_aliases
144144
from .dataobj_images import DataobjImage
145145
from .filebasedimages import FileBasedHeader, FileBasedImage

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)