Skip to content

Commit

Permalink
Apply and enforce more ruff rules (#594)
Browse files Browse the repository at this point in the history
* Deprecate a couple ruff/pyupgrade rules (UP)

* Enforce ruff/flake8-pytest-style rules (PT)

* Apply ruff/flake8-pytest-style rule PT006

PT006 Wrong type passed to first argument of `@pytest.mark.parametrize`; expected `tuple`

* Apply ruff/flake8-pytest-style rule PT007

PT007 Wrong values type in `@pytest.mark.parametrize` expected `list` of `tuple`

* Enforce ruff/flake8-bugbear rules (B)

* Apply ruff/flake8-executable rule EXE001

EXE001 Shebang is present but file is not executable

* Enforce ruff/pygrep-hooks rules (PGH)

* Apply ruff/flake8-simplify rule SIM102

SIM102 Use a single `if` statement instead of nested `if` statements

* Apply ruff/flake8-simplify rule SIM105

SIM105 Use `contextlib.suppress(ImportError)` instead of `try`-`except`-`pass`

* Apply ruff/flake8-raise rule RSE102

RSE102 Unnecessary parentheses on raised exception

* Enforce ruff/flake8-raise rules (RSE)
  • Loading branch information
DimitriPapadopoulos authored Oct 9, 2024
1 parent 8edd3f9 commit e836f39
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 17 deletions.
Empty file modified docs/conf.py
100644 → 100755
Empty file.
6 changes: 3 additions & 3 deletions numcodecs/lzma.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import contextlib

_lzma = None
try:
import lzma as _lzma
except ImportError: # pragma: no cover
try:
with contextlib.suppress(ImportError):
from backports import lzma as _lzma
except ImportError:
pass


if _lzma:
Expand Down
9 changes: 4 additions & 5 deletions numcodecs/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ def get_codec(config):
config = dict(config)
codec_id = config.pop('id', None)
cls = codec_registry.get(codec_id)
if cls is None:
if codec_id in entries:
logger.debug("Auto loading codec '%s' from entrypoint", codec_id)
cls = entries[codec_id].load()
register_codec(cls, codec_id=codec_id)
if cls is None and codec_id in entries:
logger.debug("Auto loading codec '%s' from entrypoint", codec_id)
cls = entries[codec_id].load()
register_codec(cls, codec_id=codec_id)
if cls:
return cls.from_config(config)
raise ValueError(f'codec not available: {codec_id!r}')
Expand Down
4 changes: 2 additions & 2 deletions numcodecs/tests/test_blosc.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def test_compress_blocksize_default(use_threads):
assert blocksize > 0


@pytest.mark.parametrize('bs', (2**7, 2**8))
@pytest.mark.parametrize('bs', [2**7, 2**8])
def test_compress_blocksize(use_threads, bs):
arr = np.arange(1000, dtype='i4')

Expand Down Expand Up @@ -225,7 +225,7 @@ def _decode_worker(enc):
return data


@pytest.mark.parametrize('pool', (Pool, ThreadPool))
@pytest.mark.parametrize('pool', [Pool, ThreadPool])
def test_multiprocessing(use_threads, pool):
data = np.arange(1000000)
enc = _encode_worker(data)
Expand Down
2 changes: 1 addition & 1 deletion numcodecs/tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_backwards_compatibility():


@pytest.mark.parametrize(
"input_data, dtype",
('input_data', 'dtype'),
[
([0, 1], None),
([[0, 1], [2, 3]], None),
Expand Down
2 changes: 1 addition & 1 deletion numcodecs/tests/test_msgpacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_backwards_compatibility():


@pytest.mark.parametrize(
"input_data, dtype",
("input_data", "dtype"),
[
([0, 1], None),
([[0, 1], [2, 3]], None),
Expand Down
2 changes: 1 addition & 1 deletion numcodecs/tests/test_shuffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _decode_worker(enc):
return data


@pytest.mark.parametrize('pool', (Pool, ThreadPool))
@pytest.mark.parametrize('pool', [Pool, ThreadPool])
def test_multiprocessing(pool):
data = np.arange(1000000)
enc = _encode_worker(data)
Expand Down
24 changes: 22 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,28 @@ environment = { DISABLE_NUMCODECS_AVX2=1, DISABLE_NUMCODECS_SSE2=1 }
line-length = 100

[tool.ruff.lint]
extend-select = [ "B", "I", "RUF", "UP" ]
ignore = [ "RUF001", "UP007" ]
extend-select = [
"B",
"I",
"PGH",
"PT",
"RSE",
"RUF",
"UP",
]
ignore = [
"B028",
"B904",
"PT001",
"PT004", # deprecated
"PT005", # deprecated
"PT011",
"PT012",
"RUF001",
"UP007",
"UP027", # deprecated
"UP038", # https://github.com/astral-sh/ruff/issues/7871
]

[tool.ruff.format]
quote-style = "preserve"
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,14 @@ def run(self):
build_ext.run(self)
except PlatformError as e:
error(e)
raise BuildFailed() from e
raise BuildFailed from e

def build_extension(self, ext):
try:
build_ext.build_extension(self, ext)
except ext_errors as e:
error(e)
raise BuildFailed() from e
raise BuildFailed from e


class Sclean(clean):
Expand Down

0 comments on commit e836f39

Please sign in to comment.