Skip to content

Commit 1855f0c

Browse files
Enforce ruff/flake8-comprehensions rules (C4) (#605)
* Apply ruff/flake8-comprehensions rule C401 C401 Unnecessary generator (rewrite as a `set` comprehension) * Apply ruff/flake8-comprehensions rules C408 C408 Unnecessary `dict` call (rewrite as a literal) * Enforce ruff/flake8-comprehensions rules (C4) --------- Co-authored-by: David Stansby <[email protected]>
1 parent 8c7c043 commit 1855f0c

13 files changed

+48
-47
lines changed

numcodecs/abc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def get_config(self):
8484
# override in sub-class if need special encoding of config values
8585

8686
# setup config object
87-
config = dict(id=self.codec_id)
87+
config = {'id': self.codec_id}
8888

8989
# by default, assume all non-private members are configuration
9090
# parameters - override this in sub-class if not the case

numcodecs/categorize.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ def decode(self, buf, out=None):
8585
return dec
8686

8787
def get_config(self):
88-
config = dict(
89-
id=self.codec_id,
90-
labels=self.labels,
91-
dtype=self.dtype.str,
92-
astype=self.astype.str,
93-
)
88+
config = {
89+
'id': self.codec_id,
90+
'labels': self.labels,
91+
'dtype': self.dtype.str,
92+
'astype': self.astype.str,
93+
}
9494
return config
9595

9696
def __repr__(self):

numcodecs/delta.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def decode(self, buf, out=None):
9191

9292
def get_config(self):
9393
# override to handle encoding dtypes
94-
return dict(id=self.codec_id, dtype=self.dtype.str, astype=self.astype.str)
94+
return {'id': self.codec_id, 'dtype': self.dtype.str, 'astype': self.astype.str}
9595

9696
def __repr__(self):
9797
r = f'{type(self).__name__}(dtype={self.dtype.str!r}'

numcodecs/fixedscaleoffset.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ def decode(self, buf, out=None):
116116

117117
def get_config(self):
118118
# override to handle encoding dtypes
119-
return dict(
120-
id=self.codec_id,
121-
scale=self.scale,
122-
offset=self.offset,
123-
dtype=self.dtype.str,
124-
astype=self.astype.str,
125-
)
119+
return {
120+
'id': self.codec_id,
121+
'scale': self.scale,
122+
'offset': self.offset,
123+
'dtype': self.dtype.str,
124+
'astype': self.astype.str,
125+
}
126126

127127
def __repr__(self):
128128
r = f'{type(self).__name__}(scale={self.scale}, offset={self.offset}, dtype={self.dtype.str!r}'

numcodecs/json.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ def __init__(
5252
else:
5353
separators = ', ', ': '
5454
separators = tuple(separators)
55-
self._encoder_config = dict(
56-
skipkeys=skipkeys,
57-
ensure_ascii=ensure_ascii,
58-
check_circular=check_circular,
59-
allow_nan=allow_nan,
60-
indent=indent,
61-
separators=separators,
62-
sort_keys=sort_keys,
63-
)
55+
self._encoder_config = {
56+
'skipkeys': skipkeys,
57+
'ensure_ascii': ensure_ascii,
58+
'check_circular': check_circular,
59+
'allow_nan': allow_nan,
60+
'indent': indent,
61+
'separators': separators,
62+
'sort_keys': sort_keys,
63+
}
6464
self._encoder = _json.JSONEncoder(**self._encoder_config)
65-
self._decoder_config = dict(strict=strict)
65+
self._decoder_config = {'strict': strict}
6666
self._decoder = _json.JSONDecoder(**self._decoder_config)
6767

6868
def encode(self, buf):
@@ -89,7 +89,7 @@ def decode(self, buf, out=None):
8989
return dec
9090

9191
def get_config(self):
92-
config = dict(id=self.codec_id, encoding=self._text_encoding)
92+
config = {'id': self.codec_id, 'encoding': self._text_encoding}
9393
config.update(self._encoder_config)
9494
config.update(self._decoder_config)
9595
return config

numcodecs/msgpacks.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ def decode(self, buf, out=None):
7575
return dec
7676

7777
def get_config(self):
78-
return dict(
79-
id=self.codec_id,
80-
raw=self.raw,
81-
use_single_float=self.use_single_float,
82-
use_bin_type=self.use_bin_type,
83-
)
78+
return {
79+
'id': self.codec_id,
80+
'raw': self.raw,
81+
'use_single_float': self.use_single_float,
82+
'use_bin_type': self.use_bin_type,
83+
}
8484

8585
def __repr__(self):
8686
return f'MsgPack(raw={self.raw!r}, use_bin_type={self.use_bin_type!r}, use_single_float={self.use_single_float!r})'

numcodecs/pickles.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def decode(self, buf, out=None):
4949
return dec
5050

5151
def get_config(self):
52-
return dict(id=self.codec_id, protocol=self.protocol)
52+
return {'id': self.codec_id, 'protocol': self.protocol}
5353

5454
def __repr__(self):
5555
return f'Pickle(protocol={self.protocol})'

numcodecs/quantize.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ def decode(self, buf, out=None):
8585

8686
def get_config(self):
8787
# override to handle encoding dtypes
88-
return dict(
89-
id=self.codec_id,
90-
digits=self.digits,
91-
dtype=self.dtype.str,
92-
astype=self.astype.str,
93-
)
88+
return {
89+
'id': self.codec_id,
90+
'digits': self.digits,
91+
'dtype': self.dtype.str,
92+
'astype': self.astype.str,
93+
}
9494

9595
def __repr__(self):
9696
r = f'{type(self).__name__}(digits={self.digits}, dtype={self.dtype.str!r}'

numcodecs/tests/test_blosc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ def test_config_blocksize():
199199
# explicitly stated
200200

201201
# blocksize not stated
202-
config = dict(cname='lz4', clevel=1, shuffle=Blosc.SHUFFLE)
202+
config = {"cname": 'lz4', "clevel": 1, "shuffle": Blosc.SHUFFLE}
203203
codec = Blosc.from_config(config)
204204
assert codec.blocksize == 0
205205

206206
# blocksize stated
207-
config = dict(cname='lz4', clevel=1, shuffle=Blosc.SHUFFLE, blocksize=2**8)
207+
config = {"cname": 'lz4', "clevel": 1, "shuffle": Blosc.SHUFFLE, "blocksize": 2**8}
208208
codec = Blosc.from_config(config)
209209
assert codec.blocksize == 2**8
210210

numcodecs/tests/test_lzma.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
LZMA(preset=1),
3030
LZMA(preset=5),
3131
LZMA(preset=9),
32-
LZMA(format=_lzma.FORMAT_RAW, filters=[dict(id=_lzma.FILTER_LZMA2, preset=1)]),
32+
LZMA(format=_lzma.FORMAT_RAW, filters=[{"id": _lzma.FILTER_LZMA2, "preset": 1}]),
3333
]
3434

3535

numcodecs/tests/test_registry.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_all_classes_registered():
2626
2727
see #346 for more info
2828
"""
29-
missing = set(
29+
missing = {
3030
obj.codec_id
3131
for _, submod in inspect.getmembers(numcodecs, inspect.ismodule)
3232
for _, obj in inspect.getmembers(submod)
@@ -36,7 +36,7 @@ def test_all_classes_registered():
3636
and obj.codec_id not in numcodecs.registry.codec_registry
3737
and obj.codec_id is not None # remove `None`
3838
)
39-
)
39+
}
4040

4141
if missing:
4242
raise Exception(f"these codecs are missing: {missing}") # pragma: no cover

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ line-length = 100
168168
[tool.ruff.lint]
169169
extend-select = [
170170
"B",
171+
"C4",
171172
"FLY",
172173
"FURB",
173174
"G",

setup.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040

4141

4242
def info(*msg):
43-
kwargs = dict(file=sys.stdout)
43+
kwargs = {'file': sys.stdout}
4444
print('[numcodecs]', *msg, **kwargs)
4545

4646

4747
def error(*msg):
48-
kwargs = dict(file=sys.stderr)
48+
kwargs = {'file': sys.stderr}
4949
print('[numcodecs]', *msg, **kwargs)
5050

5151

@@ -368,7 +368,7 @@ def run_setup(with_extensions):
368368
+ jenkins_extension()
369369
)
370370

371-
cmdclass = dict(build_ext=ve_build_ext, clean=Sclean)
371+
cmdclass = {'build_ext': ve_build_ext, 'clean': Sclean}
372372
else:
373373
ext_modules = []
374374
cmdclass = {}

0 commit comments

Comments
 (0)