Skip to content

Commit 3284901

Browse files
committed
chore: format and lint with ruff
1 parent f5db265 commit 3284901

File tree

12 files changed

+726
-527
lines changed

12 files changed

+726
-527
lines changed

.github/workflows/build.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ jobs:
7373
- name: Test
7474
run: python -m unittest discover tests -v
7575

76+
lint:
77+
name: Lint
78+
runs-on: ubuntu-latest
79+
steps:
80+
- uses: actions/checkout@v6
81+
- name: Setup Python
82+
uses: actions/setup-python@v6
83+
with:
84+
python-version: 3.14
85+
- name: Install dependencies
86+
run: python -m pip install -r requirements-lint.txt
87+
- name: ruff check
88+
run: ruff check
89+
- name: ruff format
90+
run: ruff format --check
91+
7692
publish:
7793
name: Publish to PyPI
7894
if: startsWith(github.ref, 'refs/tags')

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file.
1010
- Changes in build dependency: remove `setuptools` and C build toolchain, add `hatchling` and `hatch-vcs`
1111
- Remove git submodule usage
1212
- Drop support for Python 3.9 and below
13+
- Use `ruff` as formatter and linter
1314

1415
## 0.18.0 (October 5, 2025)
1516

docs/conf.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
project = 'pyzstd module'
2-
author = 'Ma Lin and contributors'
3-
copyright = '2020-present, Ma Lin and contributors'
4-
language = 'en'
1+
project = "pyzstd module"
2+
author = "Ma Lin and contributors"
3+
copyright = "2020-present, Ma Lin and contributors"
4+
language = "en"
55

6-
master_doc = 'index'
7-
pygments_style = 'sphinx'
8-
extensions = ['myst_parser', 'sphinx_rtd_theme']
9-
html_theme = 'sphinx_rtd_theme'
6+
master_doc = "index"
7+
pygments_style = "sphinx"
8+
extensions = ["myst_parser", "sphinx_rtd_theme"]
9+
html_theme = "sphinx_rtd_theme"

pyproject.toml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,45 @@ version-file = "src/pyzstd/_version.py"
5858

5959
[tool.hatch.version]
6060
source = "vcs"
61+
62+
63+
#
64+
# ruff
65+
#
66+
67+
[tool.ruff]
68+
src = ["src"]
69+
target-version = "py310"
70+
extend-exclude = [
71+
"tests",
72+
'*.pyi', # FIXME
73+
]
74+
75+
[tool.ruff.lint]
76+
select = ["ALL"]
77+
ignore = [
78+
"ANN", # FIXME
79+
"C901",
80+
"COM812",
81+
"D",
82+
"E501",
83+
"EM",
84+
"ERA001",
85+
"FA100",
86+
"ISC001",
87+
"PLR0912",
88+
"PLR0913",
89+
"PLR0915",
90+
"PLR2004",
91+
"PTH",
92+
"TRY003",
93+
"TRY301",
94+
]
95+
96+
[tool.ruff.lint.per-file-ignores]
97+
"src/pyzstd/__main__.py" = ["PLC0415", "T201"]
98+
"docs/conf.py" = ["A001", "INP001"]
99+
100+
[tool.ruff.lint.isort]
101+
force-sort-within-sections = true
102+
known-first-party = ["pyzstd"]

requirements-dev.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-e .
2+
3+
-r requirements-lint.txt

requirements-lint.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ruff==0.14.8

src/pyzstd/__init__.py

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
from enum import IntEnum
12
import sys
3+
from typing import NamedTuple
24
import warnings
35

4-
from collections import namedtuple
5-
from enum import IntEnum
6-
76
if sys.version_info < (3, 14):
87
from backports import zstd
98
else:
@@ -14,8 +13,7 @@
1413
except ImportError:
1514
from typing_extensions import deprecated
1615

17-
from pyzstd._version import __version__
18-
16+
from pyzstd._version import __version__ # noqa: F401
1917

2018
__doc__ = """\
2119
Python bindings to Zstandard (zstd) compression library, the API style is
@@ -28,32 +26,32 @@
2826
PyPI: https://pypi.org/project/pyzstd"""
2927

3028
__all__ = (
31-
"ZstdCompressor",
32-
"RichMemZstdCompressor",
33-
"ZstdDecompressor",
34-
"EndlessZstdDecompressor",
3529
"CParameter",
3630
"DParameter",
31+
"EndlessZstdDecompressor",
32+
"RichMemZstdCompressor",
33+
"SeekableFormatError",
34+
"SeekableZstdFile",
3735
"Strategy",
36+
"ZstdCompressor",
37+
"ZstdDecompressor",
38+
"ZstdDict",
3839
"ZstdError",
40+
"ZstdFile",
3941
"compress",
40-
"richmem_compress",
41-
"decompress",
4242
"compress_stream",
43+
"compressionLevel_values",
44+
"decompress",
4345
"decompress_stream",
44-
"ZstdDict",
45-
"train_dict",
4646
"finalize_dict",
4747
"get_frame_info",
4848
"get_frame_size",
49-
"ZstdFile",
5049
"open",
50+
"richmem_compress",
51+
"train_dict",
52+
"zstd_support_multithread",
5153
"zstd_version",
5254
"zstd_version_info",
53-
"zstd_support_multithread",
54-
"compressionLevel_values",
55-
"SeekableZstdFile",
56-
"SeekableFormatError",
5755
)
5856

5957

@@ -68,29 +66,29 @@ def __repr__(self):
6866
class CParameter(IntEnum):
6967
"""Compression parameters"""
7068

71-
compressionLevel = zstd.CompressionParameter.compression_level
72-
windowLog = zstd.CompressionParameter.window_log
73-
hashLog = zstd.CompressionParameter.hash_log
74-
chainLog = zstd.CompressionParameter.chain_log
75-
searchLog = zstd.CompressionParameter.search_log
76-
minMatch = zstd.CompressionParameter.min_match
77-
targetLength = zstd.CompressionParameter.target_length
69+
compressionLevel = zstd.CompressionParameter.compression_level # noqa: N815
70+
windowLog = zstd.CompressionParameter.window_log # noqa: N815
71+
hashLog = zstd.CompressionParameter.hash_log # noqa: N815
72+
chainLog = zstd.CompressionParameter.chain_log # noqa: N815
73+
searchLog = zstd.CompressionParameter.search_log # noqa: N815
74+
minMatch = zstd.CompressionParameter.min_match # noqa: N815
75+
targetLength = zstd.CompressionParameter.target_length # noqa: N815
7876
strategy = zstd.CompressionParameter.strategy
79-
targetCBlockSize = 130 # not part of PEP-784
77+
targetCBlockSize = 130 # not part of PEP-784 # noqa: N815
8078

81-
enableLongDistanceMatching = zstd.CompressionParameter.enable_long_distance_matching
82-
ldmHashLog = zstd.CompressionParameter.ldm_hash_log
83-
ldmMinMatch = zstd.CompressionParameter.ldm_min_match
84-
ldmBucketSizeLog = zstd.CompressionParameter.ldm_bucket_size_log
85-
ldmHashRateLog = zstd.CompressionParameter.ldm_hash_rate_log
79+
enableLongDistanceMatching = zstd.CompressionParameter.enable_long_distance_matching # noqa: N815
80+
ldmHashLog = zstd.CompressionParameter.ldm_hash_log # noqa: N815
81+
ldmMinMatch = zstd.CompressionParameter.ldm_min_match # noqa: N815
82+
ldmBucketSizeLog = zstd.CompressionParameter.ldm_bucket_size_log # noqa: N815
83+
ldmHashRateLog = zstd.CompressionParameter.ldm_hash_rate_log # noqa: N815
8684

87-
contentSizeFlag = zstd.CompressionParameter.content_size_flag
88-
checksumFlag = zstd.CompressionParameter.checksum_flag
89-
dictIDFlag = zstd.CompressionParameter.dict_id_flag
85+
contentSizeFlag = zstd.CompressionParameter.content_size_flag # noqa: N815
86+
checksumFlag = zstd.CompressionParameter.checksum_flag # noqa: N815
87+
dictIDFlag = zstd.CompressionParameter.dict_id_flag # noqa: N815
9088

91-
nbWorkers = zstd.CompressionParameter.nb_workers
92-
jobSize = zstd.CompressionParameter.job_size
93-
overlapLog = zstd.CompressionParameter.overlap_log
89+
nbWorkers = zstd.CompressionParameter.nb_workers # noqa: N815
90+
jobSize = zstd.CompressionParameter.job_size # noqa: N815
91+
overlapLog = zstd.CompressionParameter.overlap_log # noqa: N815
9492

9593
def bounds(self):
9694
"""Return lower and upper bounds of a compression parameter, both inclusive."""
@@ -100,7 +98,7 @@ def bounds(self):
10098
class DParameter(IntEnum):
10199
"""Decompression parameters"""
102100

103-
windowLogMax = zstd.DecompressionParameter.window_log_max
101+
windowLogMax = zstd.DecompressionParameter.window_log_max # noqa: N815
104102

105103
def bounds(self):
106104
"""Return lower and upper bounds of a decompression parameter, both inclusive."""
@@ -110,17 +108,15 @@ def bounds(self):
110108
def _convert_level_or_option(level_or_option, mode):
111109
"""Transform pyzstd params into PEP-784 `options` param"""
112110
if not isinstance(mode, str):
113-
raise ValueError(f"Invalid mode type: {mode}")
111+
raise TypeError(f"Invalid mode type: {mode}")
114112
read_mode = mode.startswith("r")
115113
if isinstance(level_or_option, int):
116114
if read_mode:
117115
raise TypeError(
118-
(
119-
"In read mode (decompression), level_or_option argument "
120-
"should be a dict object, that represents decompression "
121-
"option. It doesn't support int type compression level "
122-
"in this case."
123-
)
116+
"In read mode (decompression), level_or_option argument "
117+
"should be a dict object, that represents decompression "
118+
"option. It doesn't support int type compression level "
119+
"in this case."
124120
)
125121
return {
126122
CParameter.compressionLevel: level_or_option,
@@ -477,7 +473,7 @@ def __init__(
477473
)
478474

479475

480-
def open(
476+
def open( # noqa: A001
481477
filename,
482478
mode="rb",
483479
*,
@@ -536,7 +532,7 @@ def cb(total_input, total_output, data_in, data_out):
536532

537533
elif callback is None:
538534

539-
def cb(total_input, total_output, data_in, data_out):
535+
def cb(total_input, total_output, data_in, data_out): # noqa: ARG001
540536
output_stream.write(data_out)
541537

542538
else:
@@ -561,7 +557,7 @@ def compress_stream(
561557
zstd_dict=None,
562558
pledged_input_size=None,
563559
read_size=131_072,
564-
write_size=_DEPRECATED_PLACEHOLDER,
560+
write_size=_DEPRECATED_PLACEHOLDER, # noqa: ARG001
565561
callback=None,
566562
):
567563
"""Compresses input_stream and writes the compressed data to output_stream, it
@@ -604,7 +600,7 @@ def compress_stream(
604600
total_output = 0
605601
compressor = ZstdCompressor(level_or_option, zstd_dict)
606602
if pledged_input_size is not None and pledged_input_size != 2**64 - 1:
607-
compressor._set_pledged_input_size(pledged_input_size)
603+
compressor._set_pledged_input_size(pledged_input_size) # noqa: SLF001
608604
while data_in := input_stream.read(read_size):
609605
total_input += len(data_in)
610606
data_out = compressor.compress(data_in)
@@ -701,9 +697,17 @@ def decompress_stream(
701697
zstd_version = zstd.zstd_version
702698
zstd_version_info = zstd.zstd_version_info
703699
zstd_support_multithread = CParameter.nbWorkers.bounds() != (0, 0)
704-
compressionLevel_values = namedtuple("values", ["default", "min", "max"])(
700+
701+
702+
class CompressionValues(NamedTuple):
703+
default: int
704+
min: int
705+
max: int
706+
707+
708+
compressionLevel_values = CompressionValues( # noqa: N816
705709
zstd.COMPRESSION_LEVEL_DEFAULT, *CParameter.compressionLevel.bounds()
706710
)
707711

708712
# import here to avoid circular dependency issues
709-
from ._seekable_zstdfile import SeekableFormatError, SeekableZstdFile
713+
from ._seekable_zstdfile import SeekableFormatError, SeekableZstdFile # noqa: E402

0 commit comments

Comments
 (0)