Skip to content

Commit

Permalink
use ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
n-takumasa committed Feb 2, 2024
1 parent b228531 commit e571537
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
30 changes: 16 additions & 14 deletions jsonc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from __future__ import annotations

from collections.abc import Callable
from copy import deepcopy
from io import StringIO
from tokenize import COMMENT, NL, STRING, TokenInfo, generate_tokens, untokenize
Expand All @@ -26,6 +25,8 @@
from json import JSONDecoder, JSONEncoder # for compatibility

if TYPE_CHECKING:
from collections.abc import Callable

CommentsDict = dict[str, "Comments"] | dict[int, "Comments"]
Comments = str | CommentsDict | tuple[str, CommentsDict]

Expand Down Expand Up @@ -66,7 +67,10 @@ def _remove_c_comment(text: str) -> str:
if text[-1] != "\n":
text = text + "\n"
return re.sub(
_REMOVE_C_COMMENT, lambda x: x.group(1), text, flags=re.DOTALL | re.VERBOSE
_REMOVE_C_COMMENT,
lambda x: x.group(1),
text,
flags=re.DOTALL | re.VERBOSE,
)


Expand Down Expand Up @@ -95,7 +99,8 @@ def _make_comment(text: str, indent=0) -> str:


def _get_comments(
comments: CommentsDict | None, key: str | int
comments: CommentsDict | None,
key: str | int,
) -> tuple[str | None, CommentsDict | None]:
if comments is not None:
comments = comments.pop(key, None)
Expand All @@ -120,7 +125,7 @@ def _warn_unused(
if full_key:
full_key += "."
for k in comments:
warn("Unused comment with key: " + full_key + str(k))
warn("Unused comment with key: " + full_key + str(k)) # TODO # noqa: B028


def load(
Expand Down Expand Up @@ -181,10 +186,7 @@ def loads(

def add_comments(data: str, comments: Comments) -> str:
header, comments = _get_comments({0: deepcopy(comments)}, 0)
if header:
header = _make_comment(header) + "\n"
else:
header = ""
header = _make_comment(header) + "\n" if header else ""
result = []
stack = []
line_shift = 0
Expand All @@ -207,7 +209,7 @@ def add_comments(data: str, comments: Comments) -> str:
comm_coord,
comm_coord,
"",
)
),
)
result.append(
TokenInfo(
Expand All @@ -216,7 +218,7 @@ def add_comments(data: str, comments: Comments) -> str:
comm_coord,
comm_coord,
"",
)
),
)
line_shift += 1

Expand All @@ -238,7 +240,7 @@ def add_comments(data: str, comments: Comments) -> str:
_warn_unused(comments, stack)
comments, array_index, key = stack.pop()

token = TokenInfo(
token = TokenInfo( # TODO # noqa: PLW2901
token.type,
token.string,
(token.start[0] + line_shift, token.start[1]),
Expand All @@ -247,7 +249,7 @@ def add_comments(data: str, comments: Comments) -> str:
)
result.append(token)

assert not stack, "Error when adding comments to JSON"
assert not stack, "Error when adding comments to JSON" # TODO # noqa: S101
return header + untokenize(result)


Expand Down Expand Up @@ -292,7 +294,7 @@ def dumps(
if comments is None:
return data
if indent is None:
warn("Can't add comments to non-indented JSON")
warn("Can't add comments to non-indented JSON") # TODO # noqa: B028
return data

return add_comments(data, comments)
Expand Down Expand Up @@ -336,5 +338,5 @@ def dump(
trailing_comma=trailing_comma,
comments=comments,
**kw,
)
),
)
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,25 @@ python = ">=3.7, <3.13"
[tool.poetry.group.dev.dependencies]
pytest = "*"
tox = ">=3.27,<5.0"
ruff = "^0.2.0"

[tool.ruff]
target-version = "py38"
select = ["ALL"]
ignore = [
"ANN", # flake8-annotations
"D", # pydocstyle
"FIX", # flake8-fixme
"TD", # flake8-todos
"PTH123", # flake8-use-pathlib: builtin-open
"PLR0913", # too-many-arguments
]

[tool.ruff.per-file-ignores]
"__init__.py" = [
"F401", # Pyflakes: unused-import
]
"**/tests/**/*.py" = [
"S101", # flake8-bandit: assert
"PLR2004", # magic-value-comparison
]
7 changes: 4 additions & 3 deletions tests/test_jsonc.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from jsonc import load, loads, dump, dumps
from io import StringIO
from copy import deepcopy
from io import StringIO

import pytest

from jsonc import dump, dumps, load, loads


def test_loads():
assert loads("{// comment \n}") == {}
Expand All @@ -18,7 +19,7 @@ def test_loads():


def test_load():
with open("tests/testfile.jsonc", "r", encoding="utf-8") as f:
with open("tests/testfile.jsonc", encoding="utf-8") as f:
load(f)


Expand Down

0 comments on commit e571537

Please sign in to comment.