Skip to content

Commit

Permalink
Apply repo-review suggestions (#559)
Browse files Browse the repository at this point in the history
* Apply repo-review rule PP304

PP304: Sets the log level in pytest
log_cli_level should be set. This will allow logs to be displayed on failures.

* Apply repo-review rule PP305

PP305: Specifies xfail_strict
xfail_strict should be set. You can manually specify if a check should be strict when setting each xfail.

* Apply repo-review rules PP306, PP307, PP308

PP306: Specifies strict config
--strict-config should be in addopts = [...]. This forces an error if a config setting is misspelled.

PP307: Specifies strict markers
--strict-markers should be in addopts = [...]. This forces all markers to be specified in config, avoiding misspellings.

PP308: Specifies useful pytest summary
An explicit summary flag like -ra should be in addopts = [...] (print summary of all fails/errors).

* Apply ruff/pygrep-hooks rule PGH004

PGH004 Use specific rule codes when using `noqa`
PGH004 Use specific rule codes when using `ruff: noqa`

* Apply and enforce ruff rules (RUF)

Apply ruff rule RUF012:
RUF012 Mutable class attributes should be annotated with `typing.ClassVar`

Disable ruff rule RUF001

* Apply and enforce ruff/flake8-bugbear rules (B)

B007 Loop control variable not used within loop body

B028 No explicit `stacklevel` keyword argument found

B904 Within an `except` clause, raise exceptions with `raise ... from err` or
     `raise ... from None` to distinguish them from errors in exception handling

B905 `zip()` without an explicit `strict=` parameter

Co-authored-by: David Stansby <dstansby@gmail.com>

* Apply and enforce ruff/isort rules (I)

* Apply repo-review rule PC100

PC100: Has pre-commit-hooks
Must have https://github.com/pre-commit/pre-commit-hooks repo in .pre-commit-config.yaml

* Enforce repo-review rules

* Apply ruff/flake8-implicit-str-concat rules (ISC)

ISC001 Implicitly concatenated string literals on one line

ISC003 Explicitly concatenated string should be implicitly concatenated

* Apply ruff/refurb rules (FURB)

FURB163 Prefer `math.log10`/`math.log2` over `math.log` with a redundant base

* Fix DeprecationWarning

	'pkgutil.find_loader' is deprecated and slated for removal
	in Python 3.14; use importlib.util.find_spec() instead

---------

Co-authored-by: David Stansby <dstansby@gmail.com>
  • Loading branch information
DimitriPapadopoulos and dstansby authored Oct 9, 2024
1 parent 3652eb1 commit 8edd3f9
Show file tree
Hide file tree
Showing 61 changed files with 194 additions and 254 deletions.
15 changes: 14 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exclude: ^fixture/ # files with trailing whitespaces on purpose
ci:
autoupdate_commit_msg: "chore: update pre-commit hooks"
autofix_commit_msg: "style: pre-commit fixes"
Expand All @@ -6,9 +7,21 @@ default_stages: [commit, push]
default_language_version:
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: check-yaml
- id: debug-statements

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.6.9'
rev: v0.6.9
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
- id: ruff-format

- repo: https://github.com/scientific-python/cookie
rev: 2024.04.23
hooks:
- id: sp-repo-review
2 changes: 1 addition & 1 deletion .pyup.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# autogenerated pyup.io config file
# autogenerated pyup.io config file
# see https://pyup.io/docs/configuration/ for all available options

schedule: every month
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Numcodecs
=========

Numcodecs is a Python package providing buffer compression and transformation
Numcodecs is a Python package providing buffer compression and transformation
codecs for use in data storage and communication applications.

.. image:: https://readthedocs.org/projects/numcodecs/badge/?version=latest
Expand Down
4 changes: 2 additions & 2 deletions adhoc/blosc_memleak_check.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import sys

import numcodecs
import numpy as np
from numpy.testing import assert_array_equal

import numcodecs

codec = numcodecs.Blosc()
data = np.arange(int(sys.argv[1]))
for i in range(int(sys.argv[2])):
for _ in range(int(sys.argv[2])):
enc = codec.encode(data)
dec = codec.decode(enc)
arr = np.frombuffer(dec, dtype=data.dtype)
Expand Down
3 changes: 2 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# serve to show the default.


import sys
import os
import sys
from unittest.mock import Mock as MagicMock

import numcodecs


Expand Down
1 change: 1 addition & 0 deletions notebooks/benchmark_vlen.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
],
"source": [
"import numpy as np\n",
"\n",
"import numcodecs\n",
"\n",
"numcodecs.__version__"
Expand Down
10 changes: 4 additions & 6 deletions numcodecs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# flake8: noqa
# ruff: noqa: E402,F401
"""Numcodecs is a Python package providing buffer compression and
transformation codecs for use in data storage and communication
applications. These include:
Expand All @@ -17,14 +17,12 @@
"""

import multiprocessing
import atexit
import multiprocessing
from contextlib import suppress


from numcodecs.version import version as __version__
from numcodecs.registry import get_codec, register_codec

from numcodecs.version import version as __version__
from numcodecs.zlib import Zlib

register_codec(Zlib)
Expand Down Expand Up @@ -130,7 +128,7 @@

with suppress(ImportError):
from numcodecs import vlen
from numcodecs.vlen import VLenUTF8, VLenBytes, VLenArray
from numcodecs.vlen import VLenArray, VLenBytes, VLenUTF8

register_codec(VLenUTF8)
register_codec(VLenBytes)
Expand Down
2 changes: 1 addition & 1 deletion numcodecs/astype.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np

from .abc import Codec
from .compat import ndarray_copy, ensure_ndarray
from .compat import ensure_ndarray, ndarray_copy


class AsType(Codec):
Expand Down
1 change: 0 additions & 1 deletion numcodecs/bitround.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np


from .abc import Codec
from .compat import ensure_ndarray_like, ndarray_copy

Expand Down
2 changes: 1 addition & 1 deletion numcodecs/blosc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def decompress_partial(source, start, nitems, dest=None):
raise RuntimeError('error during blosc partial decompression: %d', ret)

return dest


# set the value of this variable to True or False to override the
# default adaptive behaviour
Expand Down
3 changes: 1 addition & 2 deletions numcodecs/bz2.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import bz2 as _bz2


from numcodecs.abc import Codec
from numcodecs.compat import ndarray_copy, ensure_contiguous_ndarray
from numcodecs.compat import ensure_contiguous_ndarray, ndarray_copy


class BZ2(Codec):
Expand Down
9 changes: 4 additions & 5 deletions numcodecs/categorize.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from .abc import Codec
from .compat import ensure_ndarray, ndarray_copy, ensure_text


import numpy as np

from .abc import Codec
from .compat import ensure_ndarray, ensure_text, ndarray_copy


class Categorize(Codec):
"""Filter encoding categorical string data as integers.
Expand Down Expand Up @@ -41,7 +40,7 @@ class Categorize(Codec):
def __init__(self, labels, dtype, astype='u1'):
self.dtype = np.dtype(dtype)
if self.dtype.kind not in 'UO':
raise TypeError("only unicode ('U') and object ('O') dtypes are " "supported")
raise TypeError("only unicode ('U') and object ('O') dtypes are supported")
self.labels = [ensure_text(label) for label in labels]
self.astype = np.dtype(astype)
if self.astype == np.dtype(object):
Expand Down
4 changes: 1 addition & 3 deletions numcodecs/checksum32.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import struct
import zlib


import numpy as np
import struct


from .abc import Codec
from .compat import ensure_contiguous_ndarray, ndarray_copy
Expand Down
6 changes: 3 additions & 3 deletions numcodecs/compat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# flake8: noqa
import functools
import codecs
# ruff: noqa: F401
import array
import codecs
import functools

import numpy as np

Expand Down
1 change: 0 additions & 1 deletion numcodecs/delta.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np


from .abc import Codec
from .compat import ensure_ndarray, ndarray_copy

Expand Down
1 change: 0 additions & 1 deletion numcodecs/fixedscaleoffset.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np


from .abc import Codec
from .compat import ensure_ndarray, ndarray_copy

Expand Down
8 changes: 4 additions & 4 deletions numcodecs/jenkins.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ cpdef uint32_t jenkins_lookup3(const uint8_t[::1] _data, uint32_t initval=0):
if length == 12:
c += (<uint32_t>k[11]) << 24
length -= 1

if length == 11:
c += (<uint32_t>k[10]) << 16
length -= 1
Expand Down Expand Up @@ -231,7 +231,7 @@ cdef inline uint32_t _jenkins_lookup3_final(uint32_t a, uint32_t b, uint32_t c):
the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
is commonly produced by subtraction) look like a single 1-bit
difference.
* the base values were pseudorandom, all zero but one bit set, or
* the base values were pseudorandom, all zero but one bit set, or
all zero plus a counter that starts at zero.
These constants passed:
Expand Down Expand Up @@ -279,7 +279,7 @@ cdef inline (uint32_t, uint32_t, uint32_t) _jenkins_lookup3_mix(uint32_t a, uint
the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
is commonly produced by subtraction) look like a single 1-bit
difference.
* the base values were pseudorandom, all zero but one bit set, or
* the base values were pseudorandom, all zero but one bit set, or
all zero plus a counter that starts at zero.
Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
Expand All @@ -289,7 +289,7 @@ cdef inline (uint32_t, uint32_t, uint32_t) _jenkins_lookup3_mix(uint32_t a, uint
14 9 3 7 17 3
Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
for "differ" defined as + with a one-bit base and a two-bit delta. I
used http://burtleburtle.net/bob/hash/avalanche.html to choose
used http://burtleburtle.net/bob/hash/avalanche.html to choose
the operations, constants, and arrangements of the variables.
This does not achieve avalanche. There are input bits of (a,b,c)
Expand Down
2 changes: 0 additions & 2 deletions numcodecs/json.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import json as _json
import textwrap


import numpy as np


from .abc import Codec
from .compat import ensure_text

Expand Down
2 changes: 1 addition & 1 deletion numcodecs/lzma.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

if _lzma:
from .abc import Codec
from .compat import ndarray_copy, ensure_contiguous_ndarray
from .compat import ensure_contiguous_ndarray, ndarray_copy

# noinspection PyShadowingBuiltins
class LZMA(Codec):
Expand Down
3 changes: 1 addition & 2 deletions numcodecs/msgpacks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
import msgpack

import numpy as np

from .abc import Codec
from .compat import ensure_contiguous_ndarray
Expand Down
4 changes: 2 additions & 2 deletions numcodecs/ndarray_like.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Optional, Protocol, runtime_checkable
from typing import Any, ClassVar, Optional, Protocol, runtime_checkable


class _CachedProtocolMeta(Protocol.__class__):
Expand All @@ -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: ClassVar[dict[tuple[type, type], bool]] = {}

def __instancecheck__(self, instance):
key = (self, instance.__class__)
Expand Down
1 change: 0 additions & 1 deletion numcodecs/packbits.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np


from .abc import Codec
from .compat import ensure_ndarray, ndarray_copy

Expand Down
4 changes: 2 additions & 2 deletions numcodecs/pcodec.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Optional, Literal
from typing import Literal, Optional

import numcodecs
import numcodecs.abc
from numcodecs.compat import ensure_contiguous_ndarray

try:
from pcodec import standalone, ChunkConfig, PagingSpec, ModeSpec
from pcodec import ChunkConfig, ModeSpec, PagingSpec, standalone
except ImportError: # pragma: no cover
standalone = None

Expand Down
6 changes: 2 additions & 4 deletions numcodecs/quantize.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import math


import numpy as np


from .abc import Codec
from .compat import ensure_ndarray, ndarray_copy

Expand Down Expand Up @@ -65,12 +63,12 @@ def encode(self, buf):

# apply scaling
precision = 10.0**-self.digits
exp = math.log(precision, 10)
exp = math.log10(precision)
if exp < 0:
exp = int(math.floor(exp))
else:
exp = int(math.ceil(exp))
bits = math.ceil(math.log(10.0**-exp, 2))
bits = math.ceil(math.log2(10.0**-exp))
scale = 2.0**bits
enc = np.around(scale * arr) / scale

Expand Down
2 changes: 1 addition & 1 deletion numcodecs/registry.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""The registry module provides some simple convenience functions to enable
applications to dynamically register and look-up codec classes."""

from importlib.metadata import entry_points
import logging
from importlib.metadata import entry_points

logger = logging.getLogger("numcodecs")
codec_registry = {}
Expand Down
5 changes: 3 additions & 2 deletions numcodecs/shuffle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import numpy as np
from .compat import ensure_contiguous_ndarray
from .abc import Codec

from ._shuffle import _doShuffle, _doUnshuffle
from .abc import Codec
from .compat import ensure_contiguous_ndarray


class Shuffle(Codec):
Expand Down
12 changes: 4 additions & 8 deletions numcodecs/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
import os
from glob import glob


import numpy as np
from numpy.testing import assert_array_almost_equal, assert_array_equal
import pytest
from numpy.testing import assert_array_almost_equal, assert_array_equal


# star import needed for repr tests so eval finds names
from numcodecs import * # noqa: F403
from numcodecs.compat import ensure_bytes, ensure_ndarray
from numcodecs.registry import get_codec

# star import needed for repr tests so eval finds names
from numcodecs import * # noqa


greetings = [
'¡Hola mundo!',
'Hej Världen!',
Expand Down Expand Up @@ -200,7 +196,7 @@ def assert_array_items_equal(res, arr):
# and values
arr = arr.ravel().tolist()
res = res.ravel().tolist()
for a, r in zip(arr, res):
for a, r in zip(arr, res, strict=True):
if isinstance(a, np.ndarray):
assert_array_equal(a, r)
elif a != a:
Expand Down
Loading

0 comments on commit 8edd3f9

Please sign in to comment.