diff --git a/docs/release.rst b/docs/release.rst index f687e511..1d8a2d41 100644 --- a/docs/release.rst +++ b/docs/release.rst @@ -21,6 +21,8 @@ Fix Maintenance ~~~~~~~~~~~ +* Change format() and old string formatting to f-strings. + By :user:`Dimitri Papadopoulos Orfanos `, :issue:`439`. * Remove pin on Sphinx By :user:`Elliott Sales de Andrade `, :issue:`552`. diff --git a/numcodecs/abc.py b/numcodecs/abc.py index e0de9272..d7328be4 100644 --- a/numcodecs/abc.py +++ b/numcodecs/abc.py @@ -118,11 +118,9 @@ def __repr__(self): # by default, assume all non-private members are configuration # parameters and valid keyword arguments to constructor function - r = '%s(' % type(self).__name__ + r = f'{type(self).__name__}(' params = [ - '{}={!r}'.format(k, getattr(self, k)) - for k in sorted(self.__dict__) - if not k.startswith('_') + f'{k}={getattr(self, k)!r}' for k in sorted(self.__dict__) if not k.startswith('_') ] r += ', '.join(params) + ')' return r diff --git a/numcodecs/astype.py b/numcodecs/astype.py index 6dffb229..e1e95adf 100644 --- a/numcodecs/astype.py +++ b/numcodecs/astype.py @@ -73,6 +73,4 @@ def get_config(self): } def __repr__(self): - return '{}(encode_dtype={!r}, decode_dtype={!r})'.format( - type(self).__name__, self.encode_dtype.str, self.decode_dtype.str - ) + return f'{type(self).__name__}(encode_dtype={self.encode_dtype.str!r}, decode_dtype={self.decode_dtype.str!r})' diff --git a/numcodecs/categorize.py b/numcodecs/categorize.py index bb1b12b2..74d35afa 100644 --- a/numcodecs/categorize.py +++ b/numcodecs/categorize.py @@ -99,10 +99,4 @@ def __repr__(self): labels = repr(self.labels[:3]) if len(self.labels) > 3: labels = labels[:-1] + ', ...]' - r = '%s(dtype=%r, astype=%r, labels=%s)' % ( - type(self).__name__, - self.dtype.str, - self.astype.str, - labels, - ) - return r + return f'{type(self).__name__}(dtype={self.dtype.str!r}, astype={self.astype.str!r}, labels={labels})' diff --git a/numcodecs/compat.py b/numcodecs/compat.py index 892e9432..c602d3a9 100644 --- a/numcodecs/compat.py +++ b/numcodecs/compat.py @@ -115,7 +115,7 @@ def ensure_contiguous_ndarray_like(buf, max_buffer_size=None, flatten=True) -> N raise ValueError("an array with contiguous memory is required") if max_buffer_size is not None and arr.nbytes > max_buffer_size: - msg = "Codec does not support buffers of > {} bytes".format(max_buffer_size) + msg = f"Codec does not support buffers of > {max_buffer_size} bytes" raise ValueError(msg) return arr diff --git a/numcodecs/delta.py b/numcodecs/delta.py index 395a9207..624a36df 100644 --- a/numcodecs/delta.py +++ b/numcodecs/delta.py @@ -91,8 +91,8 @@ def get_config(self): return dict(id=self.codec_id, dtype=self.dtype.str, astype=self.astype.str) def __repr__(self): - r = '{}(dtype={!r}'.format(type(self).__name__, self.dtype.str) + r = f'{type(self).__name__}(dtype={self.dtype.str!r}' if self.astype != self.dtype: - r += ', astype=%r' % self.astype.str + r += f', astype={self.astype.str!r}' r += ')' return r diff --git a/numcodecs/fixedscaleoffset.py b/numcodecs/fixedscaleoffset.py index b4d9469a..83834c4d 100644 --- a/numcodecs/fixedscaleoffset.py +++ b/numcodecs/fixedscaleoffset.py @@ -126,13 +126,8 @@ def get_config(self): ) def __repr__(self): - r = '%s(scale=%s, offset=%s, dtype=%r' % ( - type(self).__name__, - self.scale, - self.offset, - self.dtype.str, - ) + r = f'{type(self).__name__}(scale={self.scale}, offset={self.offset}, dtype={self.dtype.str!r}' if self.astype != self.dtype: - r += ', astype=%r' % self.astype.str + r += f', astype={self.astype.str!r}' r += ')' return r diff --git a/numcodecs/json.py b/numcodecs/json.py index a32ec5d0..7db107dd 100644 --- a/numcodecs/json.py +++ b/numcodecs/json.py @@ -97,12 +97,13 @@ def get_config(self): return config def __repr__(self): - params = ['encoding=%r' % self._text_encoding] + params = [f'encoding={self._text_encoding!r}'] for k, v in sorted(self._encoder_config.items()): - params.append('{}={!r}'.format(k, v)) + params.append(f'{k}={v!r}') for k, v in sorted(self._decoder_config.items()): - params.append('{}={!r}'.format(k, v)) + params.append(f'{k}={v!r}') classname = type(self).__name__ - r = '{}({})'.format(classname, ', '.join(params)) - r = textwrap.fill(r, width=80, break_long_words=False, subsequent_indent=' ') - return r + params = ', '.join(params) + return textwrap.fill( + f'{classname}({params})', width=80, break_long_words=False, subsequent_indent=' ' + ) diff --git a/numcodecs/lzma.py b/numcodecs/lzma.py index 70bffcc4..4966920b 100644 --- a/numcodecs/lzma.py +++ b/numcodecs/lzma.py @@ -66,11 +66,4 @@ def decode(self, buf, out=None): return ndarray_copy(dec, out) def __repr__(self): - r = '%s(format=%r, check=%r, preset=%r, filters=%r)' % ( - type(self).__name__, - self.format, - self.check, - self.preset, - self.filters, - ) - return r + return f'{type(self).__name__}(format={self.format!r}, check={self.check!r}, preset={self.preset!r}, filters={self.filters!r})' diff --git a/numcodecs/msgpacks.py b/numcodecs/msgpacks.py index f7cd7f12..e3dc7680 100644 --- a/numcodecs/msgpacks.py +++ b/numcodecs/msgpacks.py @@ -84,6 +84,4 @@ def get_config(self): ) def __repr__(self): - return 'MsgPack(raw={!r}, use_bin_type={!r}, use_single_float={!r})'.format( - self.raw, self.use_bin_type, self.use_single_float - ) + return f'MsgPack(raw={self.raw!r}, use_bin_type={self.use_bin_type!r}, use_single_float={self.use_single_float!r})' diff --git a/numcodecs/ndarray_like.py b/numcodecs/ndarray_like.py index 0c5cd4df..99416f8d 100644 --- a/numcodecs/ndarray_like.py +++ b/numcodecs/ndarray_like.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, Optional, Protocol, Tuple, Type, runtime_checkable +from typing import Any, Optional, Protocol, runtime_checkable class _CachedProtocolMeta(Protocol.__class__): @@ -11,7 +11,7 @@ class _CachedProtocolMeta(Protocol.__class__): isinstance checks using the object's class as the cache key. """ - _instancecheck_cache: Dict[Tuple[Type, Type], bool] = {} + _instancecheck_cache: dict[tuple[type, type], bool] = {} def __instancecheck__(self, instance): key = (self, instance.__class__) @@ -39,8 +39,8 @@ class FlagsObj(Protocol, metaclass=_CachedProtocolMeta): @runtime_checkable class NDArrayLike(Protocol, metaclass=_CachedProtocolMeta): dtype: DType - shape: Tuple[int, ...] - strides: Tuple[int, ...] + shape: tuple[int, ...] + strides: tuple[int, ...] ndim: int size: int itemsize: int diff --git a/numcodecs/pickles.py b/numcodecs/pickles.py index cb25a65f..1daec8d6 100644 --- a/numcodecs/pickles.py +++ b/numcodecs/pickles.py @@ -52,4 +52,4 @@ def get_config(self): return dict(id=self.codec_id, protocol=self.protocol) def __repr__(self): - return 'Pickle(protocol=%s)' % self.protocol + return f'Pickle(protocol={self.protocol})' diff --git a/numcodecs/quantize.py b/numcodecs/quantize.py index 86569cfd..c9fe3bd9 100644 --- a/numcodecs/quantize.py +++ b/numcodecs/quantize.py @@ -95,12 +95,8 @@ def get_config(self): ) def __repr__(self): - r = '%s(digits=%s, dtype=%r' % ( - type(self).__name__, - self.digits, - self.dtype.str, - ) + r = f'{type(self).__name__}(digits={self.digits}, dtype={self.dtype.str!r}' if self.astype != self.dtype: - r += ', astype=%r' % self.astype.str + r += f', astype={self.astype.str!r}' r += ')' return r diff --git a/numcodecs/registry.py b/numcodecs/registry.py index 5ba15872..cb512548 100644 --- a/numcodecs/registry.py +++ b/numcodecs/registry.py @@ -49,7 +49,7 @@ def get_codec(config): register_codec(cls, codec_id=codec_id) if cls: return cls.from_config(config) - raise ValueError('codec not available: %r' % codec_id) + raise ValueError(f'codec not available: {codec_id!r}') def register_codec(cls, codec_id=None): diff --git a/numcodecs/shuffle.py b/numcodecs/shuffle.py index a8067df9..cc243369 100644 --- a/numcodecs/shuffle.py +++ b/numcodecs/shuffle.py @@ -57,5 +57,4 @@ def decode(self, buf, out=None): return out def __repr__(self): - r = '%s(elementsize=%s)' % (type(self).__name__, self.elementsize) - return r + return f'{type(self).__name__}(elementsize={self.elementsize})' diff --git a/numcodecs/tests/common.py b/numcodecs/tests/common.py index d69d8c03..012b820c 100644 --- a/numcodecs/tests/common.py +++ b/numcodecs/tests/common.py @@ -258,7 +258,7 @@ def check_backwards_compatibility(codec_id, arrays, codecs, precision=None, pref # save fixture data for i, arr in enumerate(arrays): - arr_fn = os.path.join(fixture_dir, 'array.{:02d}.npy'.format(i)) + arr_fn = os.path.join(fixture_dir, f'array.{i:02d}.npy') if not os.path.exists(arr_fn): # pragma: no cover np.save(arr_fn, arr) @@ -278,7 +278,7 @@ def check_backwards_compatibility(codec_id, arrays, codecs, precision=None, pref pytest.skip("codec has been removed") # setup a directory to hold encoded data - codec_dir = os.path.join(fixture_dir, 'codec.{:02d}'.format(j)) + codec_dir = os.path.join(fixture_dir, f'codec.{j:02d}') if not os.path.exists(codec_dir): # pragma: no cover os.makedirs(codec_dir) @@ -293,7 +293,7 @@ def check_backwards_compatibility(codec_id, arrays, codecs, precision=None, pref config = _json.load(cf) assert codec == get_codec(config) - enc_fn = os.path.join(codec_dir, 'encoded.{:02d}.dat'.format(i)) + enc_fn = os.path.join(codec_dir, f'encoded.{i:02d}.dat') # one time encode and save array if not os.path.exists(enc_fn): # pragma: no cover diff --git a/numcodecs/zfpy.py b/numcodecs/zfpy.py index 99e014de..417aa6fc 100644 --- a/numcodecs/zfpy.py +++ b/numcodecs/zfpy.py @@ -102,11 +102,8 @@ def decode(self, buf, out=None): return dec def __repr__(self): - r = "%s(mode=%r, tolerance=%s, rate=%s, precision=%s)" % ( - type(self).__name__, - self.mode, - self.tolerance, - self.rate, - self.precision, + return ( + f"{type(self).__name__}(mode={self.mode!r}, " + f"tolerance={self.tolerance}, rate={self.rate}, " + f"precision={self.precision})" ) - return r diff --git a/pyproject.toml b/pyproject.toml index c409cbd5..3fb3ad0d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -131,5 +131,9 @@ environment = { DISABLE_NUMCODECS_AVX2=1, DISABLE_NUMCODECS_SSE2=1 } [tool.ruff] line-length = 100 +[tool.ruff.lint] +extend-select = [ "UP" ] +ignore = ["UP007"] + [tool.ruff.format] quote-style = "preserve"