Skip to content

Commit 6472f5a

Browse files
committed
ENH: Add type annotations to pacify mypy
1 parent 9ef7aab commit 6472f5a

21 files changed

+82
-72
lines changed

nibabel/analyze.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@
8181
can be loaded with and without a default flip, so the saved zoom will not
8282
constrain the affine.
8383
"""
84+
from __future__ import annotations
85+
86+
from typing import Type
8487

8588
import numpy as np
8689

@@ -131,7 +134,7 @@
131134
('glmax', 'i4'),
132135
('glmin', 'i4'),
133136
]
134-
data_history_dtd = [
137+
data_history_dtd: list[tuple[str, str] | tuple[str, str, tuple[int, ...]]] = [
135138
('descrip', 'S80'),
136139
('aux_file', 'S24'),
137140
('orient', 'S1'),
@@ -892,11 +895,11 @@ def may_contain_header(klass, binaryblock):
892895
class AnalyzeImage(SpatialImage):
893896
"""Class for basic Analyze format image"""
894897

895-
header_class = AnalyzeHeader
898+
header_class: Type[AnalyzeHeader] = AnalyzeHeader
896899
_meta_sniff_len = header_class.sizeof_hdr
897-
files_types = (('image', '.img'), ('header', '.hdr'))
898-
valid_exts = ('.img', '.hdr')
899-
_compressed_suffixes = ('.gz', '.bz2', '.zst')
900+
files_types: tuple[tuple[str, str], ...] = (('image', '.img'), ('header', '.hdr'))
901+
valid_exts: tuple[str, ...] = ('.img', '.hdr')
902+
_compressed_suffixes: tuple[str, ...] = ('.gz', '.bz2', '.zst')
900903

901904
makeable = True
902905
rw = True

nibabel/benchmarks/bench_arrayproxy_slicing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
# if memory_profiler is installed, we get memory usage results
2828
try:
29-
from memory_profiler import memory_usage
29+
from memory_profiler import memory_usage # type: ignore
3030
except ImportError:
3131
memory_usage = None
3232

nibabel/brikhead.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
am aware) always be >= 1. This permits sub-brick indexing common in AFNI
2828
programs (e.g., example4d+orig'[0]').
2929
"""
30+
from __future__ import annotations
3031

3132
import os
3233
import re
@@ -476,9 +477,9 @@ class AFNIImage(SpatialImage):
476477
True
477478
"""
478479

479-
header_class = AFNIHeader
480-
valid_exts = ('.brik', '.head')
481-
files_types = (('image', '.brik'), ('header', '.head'))
480+
header_class: type = AFNIHeader
481+
valid_exts: tuple[str, ...] = ('.brik', '.head')
482+
files_types: tuple[tuple[str, str], ...] = (('image', '.brik'), ('header', '.head'))
482483
_compressed_suffixes = ('.gz', '.bz2', '.Z', '.zst')
483484
makeable = False
484485
rw = False

nibabel/casting.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Most routines work round some numpy oddities in floating point precision and
44
casting. Others work round numpy casting to and from python ints
55
"""
6+
from __future__ import annotations
67

78
import warnings
89
from numbers import Integral
@@ -110,7 +111,7 @@ def float_to_int(arr, int_type, nan2zero=True, infmax=False):
110111

111112

112113
# Cache range values
113-
_SHARED_RANGES = {}
114+
_SHARED_RANGES: dict[tuple[type, type], tuple[np.number, np.number]] = {}
114115

115116

116117
def shared_range(flt_type, int_type):

nibabel/cmdline/dicomfs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class dummy_fuse:
2525

2626

2727
try:
28-
import fuse
28+
import fuse # type: ignore
2929

3030
uid = os.getuid()
3131
gid = os.getgid()

nibabel/externals/netcdf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,7 @@ def __setattr__(self, attr, value):
871871
pass
872872
self.__dict__[attr] = value
873873

874+
@property
874875
def isrec(self):
875876
"""Returns whether the variable has a record dimension or not.
876877
@@ -881,16 +882,15 @@ def isrec(self):
881882
882883
"""
883884
return bool(self.data.shape) and not self._shape[0]
884-
isrec = property(isrec)
885885

886+
@property
886887
def shape(self):
887888
"""Returns the shape tuple of the data variable.
888889
889890
This is a read-only attribute and can not be modified in the
890891
same manner of other numpy arrays.
891892
"""
892893
return self.data.shape
893-
shape = property(shape)
894894

895895
def getValue(self):
896896
"""

nibabel/filebasedimages.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99
"""Common interface for any image format--volume or surface, binary or xml."""
10+
from __future__ import annotations
1011

1112
import io
1213
from copy import deepcopy
@@ -144,14 +145,14 @@ class FileBasedImage:
144145
work.
145146
"""
146147

147-
header_class = FileBasedHeader
148-
_meta_sniff_len = 0
149-
files_types = (('image', None),)
150-
valid_exts = ()
151-
_compressed_suffixes = ()
148+
header_class: type = FileBasedHeader
149+
_meta_sniff_len: int = 0
150+
files_types: tuple[tuple[str, str | None], ...] = (('image', None),)
151+
valid_exts: tuple[str, ...] = ()
152+
_compressed_suffixes: tuple[str, ...] = ()
152153

153-
makeable = True # Used in test code
154-
rw = True # Used in test code
154+
makeable: bool = True # Used in test code
155+
rw: bool = True # Used in test code
155156

156157
def __init__(self, header=None, extra=None, file_map=None):
157158
"""Initialize image

nibabel/gifti/gifti.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
The Gifti specification was (at time of writing) available as a PDF download
1212
from http://www.nitrc.org/projects/gifti/
1313
"""
14+
from __future__ import annotations
1415

1516
import base64
1617
import sys
1718
import warnings
19+
from typing import Type
1820

1921
import numpy as np
2022

@@ -577,7 +579,7 @@ class GiftiImage(xml.XmlSerializable, SerializableImage):
577579
# The parser will in due course be a GiftiImageParser, but we can't set
578580
# that now, because it would result in a circular import. We set it after
579581
# the class has been defined, at the end of the class definition.
580-
parser = None
582+
parser: Type[xml.XmlParser]
581583

582584
def __init__(
583585
self,
@@ -832,17 +834,18 @@ def _to_xml_element(self):
832834
GIFTI.append(dar._to_xml_element())
833835
return GIFTI
834836

835-
def to_xml(self, enc='utf-8'):
837+
def to_xml(self, enc='utf-8') -> bytes:
836838
"""Return XML corresponding to image content"""
837839
header = b"""<?xml version="1.0" encoding="UTF-8"?>
838840
<!DOCTYPE GIFTI SYSTEM "http://www.nitrc.org/frs/download.php/115/gifti.dtd">
839841
"""
840842
return header + super().to_xml(enc)
841843

842844
# Avoid the indirection of going through to_file_map
843-
to_bytes = to_xml
845+
def to_bytes(self, enc='utf-8'):
846+
return self.to_xml(enc=enc)
844847

845-
def to_file_map(self, file_map=None):
848+
def to_file_map(self, file_map=None, enc='utf-8'):
846849
"""Save the current image to the specified file_map
847850
848851
Parameters
@@ -858,7 +861,7 @@ def to_file_map(self, file_map=None):
858861
if file_map is None:
859862
file_map = self.file_map
860863
with file_map['image'].get_prepare_fileobj('wb') as f:
861-
f.write(self.to_xml())
864+
f.write(self.to_xml(enc=enc))
862865

863866
@classmethod
864867
def from_file_map(klass, file_map, buffer_size=35000000, mmap=True):

nibabel/minc1.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99
"""Read MINC1 format images"""
10+
from __future__ import annotations
1011

1112
from numbers import Integral
1213

@@ -305,11 +306,11 @@ class Minc1Image(SpatialImage):
305306
load.
306307
"""
307308

308-
header_class = Minc1Header
309+
header_class: type = Minc1Header
309310
_meta_sniff_len = 4
310311
valid_exts = ('.mnc',)
311312
files_types = (('image', '.mnc'),)
312-
_compressed_suffixes = ('.gz', '.bz2', '.zst')
313+
_compressed_suffixes: tuple[str, ...] = ('.gz', '.bz2', '.zst')
313314

314315
makeable = True
315316
rw = False

nibabel/minc2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
2626
mincstats my_funny.mnc
2727
"""
28+
from __future__ import annotations
29+
2830
import numpy as np
2931

3032
from .minc1 import Minc1File, Minc1Image, MincError, MincHeader
@@ -148,14 +150,14 @@ class Minc2Image(Minc1Image):
148150
"""
149151

150152
# MINC2 does not do compressed whole files
151-
_compressed_suffixes = ()
153+
_compressed_suffixes: tuple[str, ...] = ()
152154
header_class = Minc2Header
153155

154156
@classmethod
155157
def from_file_map(klass, file_map, *, mmap=True, keep_file_open=None):
156158
# Import of h5py might take awhile for MPI-enabled builds
157159
# So we are importing it here "on demand"
158-
import h5py
160+
import h5py # type: ignore
159161

160162
holder = file_map['image']
161163
if holder.filename is None:

0 commit comments

Comments
 (0)