Skip to content

Commit f8b1ddf

Browse files
committed
chore: use ruff linter
1 parent d0bf87a commit f8b1ddf

33 files changed

+295
-336
lines changed

.pylintrc

Lines changed: 0 additions & 22 deletions
This file was deleted.

.vscode/settings.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,5 @@
3535
"editor.tabSize": 2
3636
},
3737
"python.envFile": "${workspaceFolder}/.vscode/env",
38-
"python.linting.pylintEnabled": true,
3938
"python.testing.pytestEnabled": true
4039
}

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ adheres to [Semantic Versioning](https://semver.org/).
2020
- Update GitHub actions dependencies
2121
- Add tests for PyPy 3.10 and 3.11
2222
- Improve tox & CI pipelines
23-
- Use ruff as formatter
23+
- Use ruff as linter and formatter
2424

2525
## [0.5.0] - 2023-02-27
2626

dev-requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
build==1.3.0
66

77
# lint
8-
pylint==2.16.2
98
ruff==0.13.1
109

1110
# typing

pyproject.toml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,41 @@
55
[tool.ruff]
66
src = ["src"]
77
target-version = "py39"
8+
9+
[tool.ruff.lint]
10+
select = ["ALL"]
11+
ignore = [
12+
"C901",
13+
"COM812",
14+
"D",
15+
"E501",
16+
"EM",
17+
"ERA001",
18+
"FA100",
19+
"PLR0912",
20+
"PLR0913",
21+
"TRY003",
22+
"TRY301",
23+
]
24+
allowed-confusables = ["α", "β", "γ", "δ", "ε", "ζ", "η", "θ"]
25+
26+
[tool.ruff.lint.per-file-ignores]
27+
"tests/**" = [
28+
"B018",
29+
"FBT",
30+
"INP001",
31+
"PLR0915",
32+
"PLR2004",
33+
"S101",
34+
"SLF001",
35+
"TC001",
36+
]
37+
38+
[tool.ruff.lint.isort]
39+
force-sort-within-sections = true
40+
known-first-party = ["xz"]
41+
42+
[tool.ruff.lint.flake8-pytest-style]
43+
fixture-parentheses = false
44+
mark-parentheses = false
45+
parametrize-names-type = "list"

src/xz/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,18 @@
3939
from xz.open import xz_open
4040
from xz.strategy import KeepBlockReadStrategy, RollingBlockReadStrategy
4141

42-
# pylint: disable=redefined-builtin
43-
open = xz_open
44-
# pylint: enable=redefined-builtin
42+
open = xz_open # noqa: A001
4543

4644

47-
__all__ = (
48-
"__version__",
45+
__all__: tuple[str, ...] = (
4946
"KeepBlockReadStrategy",
5047
"RollingBlockReadStrategy",
5148
"XZError",
5249
"XZFile",
50+
"__version__",
5351
"open",
52+
)
53+
__all__ += (
5454
# re-export from lzma for easy access
5555
"CHECK_CRC32",
5656
"CHECK_CRC64",

src/xz/block.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from io import DEFAULT_BUFFER_SIZE, SEEK_SET
22
from lzma import FORMAT_XZ, LZMACompressor, LZMADecompressor, LZMAError
3-
from typing import Optional, Tuple, Union
3+
from typing import Optional, Union
44

55
from xz.common import (
66
XZError,
@@ -45,7 +45,6 @@ def decompress(self, pos: int, size: int) -> bytes:
4545

4646
skip_before = pos - self.pos
4747

48-
# pylint: disable=using-constant-test
4948
if self.decompressor.eof:
5049
raise XZError("block: decompressor eof")
5150

@@ -95,7 +94,7 @@ def _write(self, data: bytes) -> None:
9594
def compress(self, data: bytes) -> None:
9695
self._write(self.compressor.compress(data))
9796

98-
def finish(self) -> Tuple[int, int]:
97+
def finish(self) -> tuple[int, int]:
9998
data = self.compressor.flush()
10099

101100
# footer
@@ -124,7 +123,7 @@ def __init__(
124123
preset: _LZMAPresetType = None,
125124
filters: _LZMAFiltersType = None,
126125
block_read_strategy: Optional[_BlockReadStrategyType] = None,
127-
):
126+
) -> None:
128127
super().__init__(uncompressed_size)
129128
self.fileobj = fileobj
130129
self.check = check

src/xz/common.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
# ruff: noqa: PLR2004
2+
13
from binascii import crc32 as crc32int
24
import lzma
35
from struct import pack, unpack
4-
from typing import List, Tuple, cast
6+
from typing import cast
57

68
HEADER_MAGIC = b"\xfd7zXZ\x00"
79
FOOTER_MAGIC = b"YZ"
@@ -20,7 +22,7 @@ def encode_mbi(value: int) -> bytes:
2022
return data
2123

2224

23-
def decode_mbi(data: bytes) -> Tuple[int, int]:
25+
def decode_mbi(data: bytes) -> tuple[int, int]:
2426
value = 0
2527
for size, byte in enumerate(data):
2628
value |= (byte & 0x7F) << (size * 7)
@@ -52,7 +54,7 @@ def create_xz_header(check: int) -> bytes:
5254
return HEADER_MAGIC + flags + crc32(flags)
5355

5456

55-
def create_xz_index_footer(check: int, records: List[Tuple[int, int]]) -> bytes:
57+
def create_xz_index_footer(check: int, records: list[tuple[int, int]]) -> bytes:
5658
if not 0 <= check <= 0xF:
5759
raise XZError("footer check")
5860
# index
@@ -79,15 +81,15 @@ def parse_xz_header(header: bytes) -> int:
7981
if crc32(header[6:8]) != header[8:12]:
8082
raise XZError("header crc32")
8183
flag_first_byte, check = cast(
82-
Tuple[int, int],
84+
"tuple[int, int]",
8385
unpack("<BB", header[6:8]),
8486
)
8587
if flag_first_byte or not 0 <= check <= 0xF:
8688
raise XZError("header flags")
8789
return check
8890

8991

90-
def parse_xz_index(index: bytes) -> List[Tuple[int, int]]:
92+
def parse_xz_index(index: bytes) -> list[tuple[int, int]]:
9193
if len(index) < 8 or len(index) % 4:
9294
raise XZError("index length")
9395
index = memoryview(index)
@@ -119,15 +121,15 @@ def parse_xz_index(index: bytes) -> List[Tuple[int, int]]:
119121
return records
120122

121123

122-
def parse_xz_footer(footer: bytes) -> Tuple[int, int]:
124+
def parse_xz_footer(footer: bytes) -> tuple[int, int]:
123125
if len(footer) != 12:
124126
raise XZError("footer length")
125127
if footer[10:12] != FOOTER_MAGIC:
126128
raise XZError("footer magic")
127129
if crc32(footer[4:10]) != footer[:4]:
128130
raise XZError("footer crc32")
129131
backward_size, flag_first_byte, check = cast(
130-
Tuple[int, int, int],
132+
"tuple[int, int, int]",
131133
unpack("<IBB", footer[4:10]),
132134
)
133135
backward_size = (backward_size + 1) * 4
@@ -136,5 +138,5 @@ def parse_xz_footer(footer: bytes) -> Tuple[int, int]:
136138
return (check, backward_size)
137139

138140

139-
# find default value for check implicitely used by lzma
141+
# find default value for check implicitly used by lzma
140142
DEFAULT_CHECK = parse_xz_header(lzma.compress(b"")[:12])

src/xz/file.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from io import SEEK_CUR, SEEK_END
22
import os
33
import sys
4-
from typing import BinaryIO, List, Optional, cast
4+
from typing import BinaryIO, Optional, cast
55
import warnings
66

77
from xz.common import DEFAULT_CHECK, XZError
@@ -83,8 +83,7 @@ def __init__(
8383

8484
# get fileobj
8585
if isinstance(filename, (str, bytes, os.PathLike)):
86-
# pylint: disable=consider-using-with, unspecified-encoding
87-
self.fileobj = cast(BinaryIO, open(filename, self._mode + "b"))
86+
self.fileobj = cast("BinaryIO", open(filename, self._mode + "b")) # noqa: PTH123, SIM115
8887
self._close_fileobj = True
8988
elif hasattr(filename, "read"): # weak check but better than nothing
9089
self.fileobj = filename
@@ -141,6 +140,7 @@ def close(self) -> None:
141140
"Empty XZFile: nothing was written, "
142141
"so output is empty (and not a valid xz file).",
143142
RuntimeWarning,
143+
stacklevel=2,
144144
)
145145
finally:
146146
if self._close_fileobj:
@@ -151,11 +151,11 @@ def close(self) -> None:
151151
pass
152152

153153
@property
154-
def stream_boundaries(self) -> List[int]:
154+
def stream_boundaries(self) -> list[int]:
155155
return list(self._fileobjs)
156156

157157
@property
158-
def block_boundaries(self) -> List[int]:
158+
def block_boundaries(self) -> list[int]:
159159
return [
160160
stream_pos + block_boundary
161161
for stream_pos, stream in self._fileobjs.items()

src/xz/io.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,14 @@ def __len__(self) -> int:
3434
return self._length
3535

3636
def _check_not_closed(self) -> None:
37-
# https://github.com/PyCQA/pylint/issues/3484
38-
# pylint: disable=using-constant-test
3937
if self.closed:
4038
raise ValueError("I/O operation on closed file")
4139

4240
def fileno(self) -> int:
4341
try:
44-
return cast(BinaryIO, self.fileobj).fileno() # type: ignore[attr-defined]
42+
return cast("BinaryIO", self.fileobj).fileno() # type: ignore[attr-defined]
4543
except AttributeError:
46-
raise UnsupportedOperation("fileno") # pylint: disable=raise-missing-from
44+
raise UnsupportedOperation("fileno") from None
4745

4846
def seekable(self) -> bool:
4947
"""Return a bool indicating whether object supports random access."""
@@ -188,7 +186,7 @@ def close(self) -> None:
188186

189187
# the methods below are expected to be implemented by subclasses
190188

191-
def _read(self, size: int) -> bytes: # pragma: no cover
189+
def _read(self, size: int) -> bytes: # pragma: no cover # noqa: ARG002
192190
"""Read and return up to size bytes, where size is an int.
193191
194192
The size will not exceed the number of bytes between self._pos and
@@ -205,7 +203,7 @@ def _write_before(self) -> None:
205203
def _write_after(self) -> None:
206204
"""This method is called after the last write operation (usually on file close)."""
207205

208-
def _write(self, data: bytes) -> int: # pragma: no cover
206+
def _write(self, data: bytes) -> int: # pragma: no cover # noqa: ARG002
209207
"""Writes as many bytes from data as possible, and return the number
210208
of bytes written.
211209
@@ -217,7 +215,7 @@ def _write(self, data: bytes) -> int: # pragma: no cover
217215
"""
218216
raise UnsupportedOperation("write")
219217

220-
def _truncate(self, size: int) -> None: # pragma: no cover
218+
def _truncate(self, size: int) -> None: # pragma: no cover # noqa: ARG002
221219
"""Truncate the file to the given size.
222220
This resizing can extend or reduce the current file size.
223221
@@ -286,7 +284,7 @@ def _write_after(self) -> None:
286284
if self._fileobjs:
287285
last_fileobj = self._fileobjs.last_item
288286
if last_fileobj:
289-
last_fileobj._write_end() # pylint: disable=protected-access
287+
last_fileobj._write_end() # noqa: SLF001
290288
else:
291289
del self._fileobjs[self._fileobjs.last_key]
292290

@@ -301,7 +299,7 @@ def _write(self, data: bytes) -> int:
301299
fileobj = self._get_fileobj()
302300

303301
# newly created fileobj should be writable
304-
# otherwire this will raise UnsupportedOperation
302+
# otherwise this will raise UnsupportedOperation
305303
return fileobj.write(data)
306304

307305
def _truncate(self, size: int) -> None:
@@ -329,7 +327,7 @@ def _change_fileobj(self) -> None:
329327
last_fileobj = self._fileobjs.last_item
330328
if last_fileobj:
331329
if last_fileobj.writable():
332-
last_fileobj._write_end() # pylint: disable=protected-access
330+
last_fileobj._write_end() # noqa: SLF001
333331
else:
334332
del self._fileobjs[self._fileobjs.last_key]
335333

0 commit comments

Comments
 (0)