Skip to content

Commit

Permalink
chore: switch to ruff lint and format
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed May 7, 2024
1 parent bf01a25 commit e5e09e9
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 332 deletions.
37 changes: 5 additions & 32 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
repos:
- repo: https://github.com/psf/black
rev: 24.4.2
hooks:
- id: black

- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
exclude: docs/.*

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
Expand All @@ -19,31 +8,15 @@ repos:
exclude: ^tests/(toml-test|toml-spec-tests)/.*
- id: debug-statements

- repo: https://github.com/asottile/yesqa
rev: v1.5.0
hooks:
- id: yesqa
additional_dependencies: &flake8_deps
- flake8-broken-line
- flake8-bugbear
- flake8-comprehensions
- flake8-eradicate
- flake8-quotes
- flake8-simplify
- flake8-tidy-imports
- flake8-typing-imports
- flake8-use-fstring
- pep8-naming
exclude: ^tomlkit/items\.py

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.2
hooks:
- id: pyupgrade
args: [--py37-plus]

- repo: https://github.com/pycqa/flake8
rev: 7.0.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.4.3'
hooks:
- id: flake8
additional_dependencies: *flake8_deps
- id: ruff
args: [--fix, --exit-non-zero-on-fix, --show-fixes]
- id: ruff-format
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

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

from tomlkit import __version__ # noqa: E402
from tomlkit import __version__


# -- Project information -----------------------------------------------------
Expand Down
616 changes: 363 additions & 253 deletions poetry.lock

Large diffs are not rendered by default.

43 changes: 19 additions & 24 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,27 @@ mypy = "^0.990"
Sphinx = "^4.3.2"
furo = "^2022.9.29"

[tool.black]
line-length = 88
include = '\.pyi?$'
exclude = '''
/(
\.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| build
| dist
| tests/toml-test
)/
'''
[tool.ruff.lint]
extend-select = [
"I", # isort
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"PGH", # pygrep-hooks
"RUF", # ruff
"W", # pycodestyle
"YTT", # flake8-2020
]
extend-ignore = ["B018", "B019", "RUF018"]

[tool.isort]
profile = "black"
force_single_line = true
atomic = true
lines_after_imports = 2
lines_between_types = 1
[tool.ruff.lint.mccabe]
max-complexity = 10

known_first_party = ["tomlkit"]
known_third_party = ["pytest"]
[tool.ruff.lint.isort]
known-first-party = ["tomlkit"]
known-third-party = ["pytest"]
force-single-line = true
lines-after-imports = 2
lines-between-types = 1

[build-system]
requires = ["poetry-core>=1.0.0a9"]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_toml_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ def test_toml_document_unwrap():
doc = parse(content)
unwrapped = doc.unwrap()
assert_is_ppo(unwrapped, dict)
assert_is_ppo(list(unwrapped.keys())[0], str)
assert_is_ppo(next(iter(unwrapped)), str)
assert_is_ppo(unwrapped["tool"], dict)
assert_is_ppo(list(unwrapped["tool"].keys())[0], str)
assert_is_ppo(next(iter(unwrapped["tool"])), str)
assert_is_ppo(unwrapped["tool"]["poetry"]["name"], str)


Expand Down
6 changes: 3 additions & 3 deletions tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@


def test_write_backslash():
d = {"foo": "\\e\u25E6\r"}
d = {"foo": "\\e\u25e6\r"}

expected = """foo = "\\\\e\u25E6\\r"
expected = """foo = "\\\\e\u25e6\\r"
"""

assert expected == dumps(d)
assert loads(dumps(d))["foo"] == "\\e\u25E6\r"
assert loads(dumps(d))["foo"] == "\\e\u25e6\r"


def test_escape_special_characters_in_key():
Expand Down
8 changes: 4 additions & 4 deletions tomlkit/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
# Importing from builtins is preferred over simple assignment, see issues:
# https://github.com/python/mypy/issues/8715
# https://github.com/python/mypy/issues/10068
from builtins import dict as _CustomDict # noqa: N812
from builtins import float as _CustomFloat # noqa: N812
from builtins import int as _CustomInt # noqa: N812
from builtins import list as _CustomList # noqa: N812
from builtins import dict as _CustomDict
from builtins import float as _CustomFloat
from builtins import int as _CustomInt
from builtins import list as _CustomList
from typing import Callable
from typing import Concatenate
from typing import ParamSpec
Expand Down
5 changes: 1 addition & 4 deletions tomlkit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def datetime(raw: str) -> DateTime:
return item(value)


def array(raw: str = None) -> Array:
def array(raw: str = "[]") -> Array:
"""Create an array item for its string representation.
:Example:
Expand All @@ -172,9 +172,6 @@ def array(raw: str = None) -> Array:
>>> a
[1, 2, 3]
"""
if raw is None:
raw = "[]"

return value(raw)


Expand Down
4 changes: 2 additions & 2 deletions tomlkit/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def _raw_append(self, key: Key | None, item: Item) -> None:
if key is not None and not isinstance(current, Table):
raise KeyAlreadyPresent(key)

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

Expand Down Expand Up @@ -447,7 +447,7 @@ def _insert_at(self, idx: int, key: Key | str, item: Any) -> Container:
current_idx = self._map[key]
if not isinstance(current_idx, tuple):
current_idx = (current_idx,)
self._map[key] = current_idx + (idx,)
self._map[key] = (*current_idx, idx)
else:
self._map[key] = idx
self._body.insert(idx, (key, item))
Expand Down
4 changes: 2 additions & 2 deletions tomlkit/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class UnexpectedCharError(ParseError):
"""

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

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

Expand Down Expand Up @@ -148,7 +148,7 @@ class InvalidCharInStringError(ParseError):
"""

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

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

Expand Down
2 changes: 1 addition & 1 deletion tomlkit/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ def as_string(self) -> str:
return self._s

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

def _getstate(self, protocol=3):
return self._s, self._fixed
Expand Down
6 changes: 3 additions & 3 deletions tomlkit/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def _parse_value(self) -> Item:
raw,
)
except ValueError:
raise self.parse_error(InvalidDateTimeError)
raise self.parse_error(InvalidDateTimeError) from None

if m.group(1):
try:
Expand Down Expand Up @@ -513,7 +513,7 @@ def _parse_value(self) -> Item:
raw + time_part,
)
except ValueError:
raise self.parse_error(InvalidDateError)
raise self.parse_error(InvalidDateError) from None

if m.group(5):
try:
Expand All @@ -529,7 +529,7 @@ def _parse_value(self) -> Item:
raw,
)
except ValueError:
raise self.parse_error(InvalidTimeError)
raise self.parse_error(InvalidTimeError) from None

item = self._parse_number(raw, trivia)
if item is not None:
Expand Down
2 changes: 1 addition & 1 deletion tomlkit/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def inc(self, exception: type[ParseError] | None = None) -> bool:
self._idx = len(self)
self._current = self.EOF
if exception:
raise self.parse_error(exception)
raise self.parse_error(exception) from None

return False

Expand Down

0 comments on commit e5e09e9

Please sign in to comment.