Skip to content

Commit e5e09e9

Browse files
committed
chore: switch to ruff lint and format
Signed-off-by: Frost Ming <[email protected]>
1 parent bf01a25 commit e5e09e9

File tree

13 files changed

+407
-332
lines changed

13 files changed

+407
-332
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
11
repos:
2-
- repo: https://github.com/psf/black
3-
rev: 24.4.2
4-
hooks:
5-
- id: black
6-
7-
- repo: https://github.com/pycqa/isort
8-
rev: 5.13.2
9-
hooks:
10-
- id: isort
11-
exclude: docs/.*
12-
132
- repo: https://github.com/pre-commit/pre-commit-hooks
143
rev: v4.6.0
154
hooks:
@@ -19,31 +8,15 @@ repos:
198
exclude: ^tests/(toml-test|toml-spec-tests)/.*
209
- id: debug-statements
2110

22-
- repo: https://github.com/asottile/yesqa
23-
rev: v1.5.0
24-
hooks:
25-
- id: yesqa
26-
additional_dependencies: &flake8_deps
27-
- flake8-broken-line
28-
- flake8-bugbear
29-
- flake8-comprehensions
30-
- flake8-eradicate
31-
- flake8-quotes
32-
- flake8-simplify
33-
- flake8-tidy-imports
34-
- flake8-typing-imports
35-
- flake8-use-fstring
36-
- pep8-naming
37-
exclude: ^tomlkit/items\.py
38-
3911
- repo: https://github.com/asottile/pyupgrade
4012
rev: v3.15.2
4113
hooks:
4214
- id: pyupgrade
4315
args: [--py37-plus]
4416

45-
- repo: https://github.com/pycqa/flake8
46-
rev: 7.0.0
17+
- repo: https://github.com/astral-sh/ruff-pre-commit
18+
rev: 'v0.4.3'
4719
hooks:
48-
- id: flake8
49-
additional_dependencies: *flake8_deps
20+
- id: ruff
21+
args: [--fix, --exit-non-zero-on-fix, --show-fixes]
22+
- id: ruff-format

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
1818

19-
from tomlkit import __version__ # noqa: E402
19+
from tomlkit import __version__
2020

2121

2222
# -- Project information -----------------------------------------------------

poetry.lock

Lines changed: 363 additions & 253 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,27 @@ mypy = "^0.990"
3232
Sphinx = "^4.3.2"
3333
furo = "^2022.9.29"
3434

35-
[tool.black]
36-
line-length = 88
37-
include = '\.pyi?$'
38-
exclude = '''
39-
/(
40-
\.git
41-
| \.hg
42-
| \.mypy_cache
43-
| \.tox
44-
| \.venv
45-
| _build
46-
| build
47-
| dist
48-
| tests/toml-test
49-
)/
50-
'''
35+
[tool.ruff.lint]
36+
extend-select = [
37+
"I", # isort
38+
"B", # flake8-bugbear
39+
"C4", # flake8-comprehensions
40+
"PGH", # pygrep-hooks
41+
"RUF", # ruff
42+
"W", # pycodestyle
43+
"YTT", # flake8-2020
44+
]
45+
extend-ignore = ["B018", "B019", "RUF018"]
5146

52-
[tool.isort]
53-
profile = "black"
54-
force_single_line = true
55-
atomic = true
56-
lines_after_imports = 2
57-
lines_between_types = 1
47+
[tool.ruff.lint.mccabe]
48+
max-complexity = 10
5849

59-
known_first_party = ["tomlkit"]
60-
known_third_party = ["pytest"]
50+
[tool.ruff.lint.isort]
51+
known-first-party = ["tomlkit"]
52+
known-third-party = ["pytest"]
53+
force-single-line = true
54+
lines-after-imports = 2
55+
lines-between-types = 1
6156

6257
[build-system]
6358
requires = ["poetry-core>=1.0.0a9"]

tests/test_toml_document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ def test_toml_document_unwrap():
163163
doc = parse(content)
164164
unwrapped = doc.unwrap()
165165
assert_is_ppo(unwrapped, dict)
166-
assert_is_ppo(list(unwrapped.keys())[0], str)
166+
assert_is_ppo(next(iter(unwrapped)), str)
167167
assert_is_ppo(unwrapped["tool"], dict)
168-
assert_is_ppo(list(unwrapped["tool"].keys())[0], str)
168+
assert_is_ppo(next(iter(unwrapped["tool"])), str)
169169
assert_is_ppo(unwrapped["tool"]["poetry"]["name"], str)
170170

171171

tests/test_write.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44

55
def test_write_backslash():
6-
d = {"foo": "\\e\u25E6\r"}
6+
d = {"foo": "\\e\u25e6\r"}
77

8-
expected = """foo = "\\\\e\u25E6\\r"
8+
expected = """foo = "\\\\e\u25e6\\r"
99
"""
1010

1111
assert expected == dumps(d)
12-
assert loads(dumps(d))["foo"] == "\\e\u25E6\r"
12+
assert loads(dumps(d))["foo"] == "\\e\u25e6\r"
1313

1414

1515
def test_escape_special_characters_in_key():

tomlkit/_types.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
# Importing from builtins is preferred over simple assignment, see issues:
2020
# https://github.com/python/mypy/issues/8715
2121
# https://github.com/python/mypy/issues/10068
22-
from builtins import dict as _CustomDict # noqa: N812
23-
from builtins import float as _CustomFloat # noqa: N812
24-
from builtins import int as _CustomInt # noqa: N812
25-
from builtins import list as _CustomList # noqa: N812
22+
from builtins import dict as _CustomDict
23+
from builtins import float as _CustomFloat
24+
from builtins import int as _CustomInt
25+
from builtins import list as _CustomList
2626
from typing import Callable
2727
from typing import Concatenate
2828
from typing import ParamSpec

tomlkit/api.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def datetime(raw: str) -> DateTime:
160160
return item(value)
161161

162162

163-
def array(raw: str = None) -> Array:
163+
def array(raw: str = "[]") -> Array:
164164
"""Create an array item for its string representation.
165165
166166
:Example:
@@ -172,9 +172,6 @@ def array(raw: str = None) -> Array:
172172
>>> a
173173
[1, 2, 3]
174174
"""
175-
if raw is None:
176-
raw = "[]"
177-
178175
return value(raw)
179176

180177

tomlkit/container.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def _raw_append(self, key: Key | None, item: Item) -> None:
312312
if key is not None and not isinstance(current, Table):
313313
raise KeyAlreadyPresent(key)
314314

315-
self._map[key] = current_idx + (len(self._body),)
315+
self._map[key] = (*current_idx, len(self._body))
316316
elif key is not None:
317317
self._map[key] = len(self._body)
318318

@@ -447,7 +447,7 @@ def _insert_at(self, idx: int, key: Key | str, item: Any) -> Container:
447447
current_idx = self._map[key]
448448
if not isinstance(current_idx, tuple):
449449
current_idx = (current_idx,)
450-
self._map[key] = current_idx + (idx,)
450+
self._map[key] = (*current_idx, idx)
451451
else:
452452
self._map[key] = idx
453453
self._body.insert(idx, (key, item))

tomlkit/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class UnexpectedCharError(ParseError):
115115
"""
116116

117117
def __init__(self, line: int, col: int, char: str) -> None:
118-
message = f"Unexpected character: {repr(char)}"
118+
message = f"Unexpected character: {char!r}"
119119

120120
super().__init__(line, col, message=message)
121121

@@ -148,7 +148,7 @@ class InvalidCharInStringError(ParseError):
148148
"""
149149

150150
def __init__(self, line: int, col: int, char: str) -> None:
151-
message = f"Invalid character {repr(char)} in string"
151+
message = f"Invalid character {char!r} in string"
152152

153153
super().__init__(line, col, message=message)
154154

tomlkit/items.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ def as_string(self) -> str:
556556
return self._s
557557

558558
def __repr__(self) -> str:
559-
return f"<{self.__class__.__name__} {repr(self._s)}>"
559+
return f"<{self.__class__.__name__} {self._s!r}>"
560560

561561
def _getstate(self, protocol=3):
562562
return self._s, self._fixed

tomlkit/parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def _parse_value(self) -> Item:
481481
raw,
482482
)
483483
except ValueError:
484-
raise self.parse_error(InvalidDateTimeError)
484+
raise self.parse_error(InvalidDateTimeError) from None
485485

486486
if m.group(1):
487487
try:
@@ -513,7 +513,7 @@ def _parse_value(self) -> Item:
513513
raw + time_part,
514514
)
515515
except ValueError:
516-
raise self.parse_error(InvalidDateError)
516+
raise self.parse_error(InvalidDateError) from None
517517

518518
if m.group(5):
519519
try:
@@ -529,7 +529,7 @@ def _parse_value(self) -> Item:
529529
raw,
530530
)
531531
except ValueError:
532-
raise self.parse_error(InvalidTimeError)
532+
raise self.parse_error(InvalidTimeError) from None
533533

534534
item = self._parse_number(raw, trivia)
535535
if item is not None:

tomlkit/source.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def inc(self, exception: type[ParseError] | None = None) -> bool:
119119
self._idx = len(self)
120120
self._current = self.EOF
121121
if exception:
122-
raise self.parse_error(exception)
122+
raise self.parse_error(exception) from None
123123

124124
return False
125125

0 commit comments

Comments
 (0)