Skip to content

Commit

Permalink
Use walrus operator
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Aug 1, 2024
1 parent 43426ab commit 08c194d
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from jsonyx.tool import JSONNamespace


class _PyVZ2Namespace: # pylint: disable=R0903
# pylint: disable-next=R0903
class _PyVZ2Namespace:
command: Literal["json"]


Expand Down
3 changes: 2 additions & 1 deletion src/jsonyx/_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ def __hash__(self) -> int:
class JSONSyntaxError(SyntaxError):
"""JSON syntax error."""

def __init__( # pylint: disable=R0913
# pylint: disable-next=R0913
def __init__(
self, msg: str, filename: str, doc: str, start: int, end: int = 0,
) -> None:
"""Create new JSON syntax error."""
Expand Down
3 changes: 1 addition & 2 deletions src/jsonyx/_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ def replace(match: Match[str]) -> str:
try:
return _ESCAPE_DCT[s]
except KeyError:
uni: int = ord(s)
if uni >= 0x10000:
if (uni := ord(s)) >= 0x10000:
# surrogate pair
uni -= 0x10000
uni1: int = 0xd800 | ((uni >> 10) & 0x3ff)
Expand Down
9 changes: 4 additions & 5 deletions src/jsonyx/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@
if TYPE_CHECKING:
from types import ModuleType

pyjson: ModuleType | None = import_fresh_module("jsonyx", blocked=["_jsonyx"])
if cjson := import_fresh_module("jsonyx", fresh=["_jsonyx"]):
# JSONSyntaxError is cached inside the _jsonyx module
cjson.JSONSyntaxError = JSONSyntaxError # type: ignore

pyjson: ModuleType | None = import_fresh_module("jsonyx", blocked=["_jsonyx"])


@pytest.fixture(params=[cjson, pyjson], ids=["cjson", "pyjson"], name="json")
def get_json(request: pytest.FixtureRequest) -> ModuleType:
"""Get JSON module."""
json: ModuleType | None = request.param
if json is None:
json: ModuleType | None
if (json := request.param) is None:
pytest.xfail("module unavailable")
pytest.fail("module unavailable")

return json
return json # type: ignore[no-any-return]
4 changes: 2 additions & 2 deletions src/jsonyx/test/test_loads.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def test_invalid_string(
# Multiple values
("[1, 2, 3]", [1, 2, 3]),
]) # type: ignore
])
def test_array(json: ModuleType, s: str, expected: list[object]) -> None:
"""Test JSON array."""
assert json.loads(s) == expected
Expand Down Expand Up @@ -348,7 +348,7 @@ def test_invalid_array(
# Multiple values
('{"a": 1, "b": 2, "c": 3}', {"a": 1, "b": 2, "c": 3}),
]) # type: ignore
])
def test_object(json: ModuleType, s: str, expected: dict[str, object]) -> None:
"""Test JSON object."""
assert json.loads(s) == expected
Expand Down
3 changes: 2 additions & 1 deletion src/jsonyx/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from jsonyx.allow import EVERYTHING, NOTHING, SURROGATES


class JSONNamespace: # pylint: disable=R0903
# pylint: disable-next=R0903
class JSONNamespace:
"""JSON namespace."""

compact: bool
Expand Down

0 comments on commit 08c194d

Please sign in to comment.