Skip to content

Commit b58c911

Browse files
STY: Apply ruff rule RUF012
RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
1 parent c18f542 commit b58c911

File tree

11 files changed

+34
-32
lines changed

11 files changed

+34
-32
lines changed

doc/tools/apigen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ApiDocWriter:
3232
to Sphinx-parsable reST format"""
3333

3434
# only separating first two levels
35-
rst_section_levels = ['*', '=', '-', '~', '^']
35+
rst_section_levels = ('*', '=', '-', '~', '^')
3636

3737
def __init__(
3838
self,

nibabel/analyze.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class AnalyzeHeader(LabeledWrapStruct, SpatialHeader):
185185
template_dtype = header_dtype
186186
_data_type_codes = data_type_codes
187187
# fields with recoders for their values
188-
_field_recoders = {'datatype': data_type_codes}
188+
_field_recoders = {'datatype': data_type_codes} # noqa: RUF012
189189
# default x flip
190190
default_x_flip = True
191191

nibabel/cifti2/cifti2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ class Cifti2MatrixIndicesMap(xml.XmlSerializable, MutableSequence):
10451045
If it is a series, units
10461046
"""
10471047

1048-
_valid_type_mappings_ = {
1048+
_valid_type_mappings_ = { # noqa: RUF012
10491049
Cifti2BrainModel: ('CIFTI_INDEX_TYPE_BRAIN_MODELS',),
10501050
Cifti2Parcel: ('CIFTI_INDEX_TYPE_PARCELS',),
10511051
Cifti2NamedMap: ('CIFTI_INDEX_TYPE_LABELS',),

nibabel/nifti1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ class Nifti1Header(SpmAnalyzeHeader):
811811
_data_type_codes = data_type_codes
812812

813813
# fields with recoders for their values
814-
_field_recoders = {
814+
_field_recoders = { # noqa: RUF012
815815
'datatype': data_type_codes,
816816
'qform_code': xform_codes,
817817
'sform_code': xform_codes,

nibabel/openers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class Opener:
131131
gz_def = (_gzip_open, ('mode', 'compresslevel', 'mtime', 'keep_open'))
132132
bz2_def = (BZ2File, ('mode', 'buffering', 'compresslevel'))
133133
zstd_def = (_zstd_open, ('mode', 'level_or_option', 'zstd_dict'))
134-
compress_ext_map: dict[str | None, OpenerDef] = {
134+
compress_ext_map: dict[str | None, OpenerDef] = { # noqa: RUF012
135135
'.gz': gz_def,
136136
'.bz2': bz2_def,
137137
'.zst': zstd_def,
@@ -141,7 +141,7 @@ class Opener:
141141
default_compresslevel = 1
142142
#: default option for zst files
143143
default_zst_compresslevel = 3
144-
default_level_or_option = {
144+
default_level_or_option = { # noqa: RUF012
145145
'rb': None,
146146
'r': None,
147147
'wb': default_zst_compresslevel,

nibabel/tests/test_analyze.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class TestAnalyzeHeader(tws._TestLabeledWrapStruct):
5858
header_class = AnalyzeHeader
5959
example_file = header_file
6060
sizeof_hdr = AnalyzeHeader.sizeof_hdr
61-
supported_np_types = {np.uint8, np.int16, np.int32, np.float32, np.float64, np.complex64}
61+
supported_np_types = frozenset((np.uint8, np.int16, np.int32, np.float32, np.float64, np.complex64))
6262
add_duplicate_types(supported_np_types)
6363

6464
def test_supported_types(self):

nibabel/tests/test_brikhead.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,10 @@ def test_brikheadfile(self):
136136

137137
class TestBadVars:
138138
module = brikhead
139-
vars = [
139+
vars = (
140140
'type = badtype-attribute\nname = BRICK_TYPES\ncount = 1\n1\n',
141141
'type = integer-attribute\ncount = 1\n1\n',
142-
]
142+
)
143143

144144
def test_unpack_var(self):
145145
for var in self.vars:

nibabel/tests/test_minc2_data.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def _make_affine(coses, zooms, starts):
3535

3636
class TestEPIFrame:
3737
opener = staticmethod(top_load)
38-
x_cos = [1, 0, 0]
39-
y_cos = [0.0, 1, 0]
40-
z_cos = [0, 0, 1]
41-
zooms = [-0.8984375, -0.8984375, 3.0]
42-
starts = [117.25609125, 138.89861125, -54.442028]
38+
x_cos = (1, 0, 0)
39+
y_cos = (0.0, 1, 0)
40+
z_cos = (0, 0, 1)
41+
zooms = (-0.8984375, -0.8984375, 3.0)
42+
starts = (117.25609125, 138.89861125, -54.442028)
4343
example_params = dict(
4444
fname=os.path.join(MINC2_PATH, 'mincex_EPI-frame.mnc'),
4545
shape=(40, 256, 256),
@@ -74,11 +74,11 @@ def test_load(self):
7474

7575

7676
class TestB0(TestEPIFrame):
77-
x_cos = [0.9970527523765, 0.0, 0.0767190261828617]
78-
y_cos = [0.0, 1.0, -6.9388939e-18]
79-
z_cos = [-0.0767190261828617, 6.9184432614435e-18, 0.9970527523765]
80-
zooms = [-0.8984375, -0.8984375, 6.49999990444107]
81-
starts = [105.473101260826, 151.74885125, -61.8714747993248]
77+
x_cos = (0.9970527523765, 0.0, 0.0767190261828617)
78+
y_cos = (0.0, 1.0, -6.9388939e-18)
79+
z_cos = (-0.0767190261828617, 6.9184432614435e-18, 0.9970527523765)
80+
zooms = (-0.8984375, -0.8984375, 6.49999990444107)
81+
starts = (105.473101260826, 151.74885125, -61.8714747993248)
8282
example_params = dict(
8383
fname=os.path.join(MINC2_PATH, 'mincex_diff-B0.mnc'),
8484
shape=(19, 256, 256),
@@ -105,11 +105,11 @@ class TestFA(TestEPIFrame):
105105

106106

107107
class TestGado(TestEPIFrame):
108-
x_cos = [0.999695413509548, -0.0174524064372835, 0.0174497483512505]
109-
y_cos = [0.0174497483512505, 0.999847695156391, 0.000304586490452135]
110-
z_cos = [-0.0174524064372835, 0.0, 0.999847695156391]
111-
zooms = [1, -1, -1]
112-
starts = [-75.76775, 115.80462, 81.38605]
108+
x_cos = (0.999695413509548, -0.0174524064372835, 0.0174497483512505)
109+
y_cos = (0.0174497483512505, 0.999847695156391, 0.000304586490452135)
110+
z_cos = (-0.0174524064372835, 0.0, 0.999847695156391)
111+
zooms = (1, -1, -1)
112+
starts = (-75.76775, 115.80462, 81.38605)
113113
example_params = dict(
114114
fname=os.path.join(MINC2_PATH, 'mincex_gado-contrast.mnc'),
115115
shape=(100, 170, 146),
@@ -124,11 +124,11 @@ class TestGado(TestEPIFrame):
124124

125125

126126
class TestT1(TestEPIFrame):
127-
x_cos = [1, 0, 0]
128-
y_cos = [0, 1, 0]
129-
z_cos = [0, 0, 1]
130-
zooms = [1, 1, 1]
131-
starts = [-90, -126, -12]
127+
x_cos = (1, 0, 0)
128+
y_cos = (0, 1, 0)
129+
z_cos = (0, 0, 1)
130+
zooms = (1, 1, 1)
131+
starts = (-90, -126, -12)
132132
example_params = dict(
133133
fname=os.path.join(MINC2_PATH, 'mincex_t1.mnc'),
134134
shape=(110, 217, 181),

nibabel/wrapstruct.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112

113113
from __future__ import annotations
114114

115+
from typing import ClassVar
116+
115117
import numpy as np
116118

117119
from . import imageglobals as imageglobals
@@ -485,7 +487,7 @@ def _get_checks(klass):
485487
class LabeledWrapStruct(WrapStruct):
486488
"""A WrapStruct with some fields having value labels for printing etc"""
487489

488-
_field_recoders: dict[str, Recoder] = {} # for recoding values for str
490+
_field_recoders: ClassVar[dict[str, Recoder]] = {} # for recoding values for str
489491

490492
def get_value_label(self, fieldname):
491493
"""Returns label for coded field

nibabel/xmlutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class XmlParser:
4949
CharacterDataHandler
5050
"""
5151

52-
HANDLER_NAMES = ['StartElementHandler', 'EndElementHandler', 'CharacterDataHandler']
52+
HANDLER_NAMES = ('StartElementHandler', 'EndElementHandler', 'CharacterDataHandler')
5353

5454
def __init__(self, encoding='utf-8', buffer_size=35000000, verbose=0):
5555
"""

0 commit comments

Comments
 (0)