Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rfc8785: docstring coverage #2

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

- uses: actions/setup-python@v5
with:
python-version: "3.x"
python-version-file: pyproject.toml
cache: "pip"
cache-dependency-path: pyproject.toml

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:

- uses: actions/setup-python@v5
with:
python-version: "3.8"
python-version-file: pyproject.toml
cache: "pip"
cache-dependency-path: pyproject.toml

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

- uses: actions/setup-python@v5
with:
python-version: ">= 3.8"
python-version-file: pyproject.toml
cache: "pip"
cache-dependency-path: pyproject.toml

Expand Down
3 changes: 1 addition & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ jobs:
strategy:
matrix:
python:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
3 changes: 2 additions & 1 deletion src/rfc8785/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

__version__ = "0.0.1"

from ._impl import CanonicalizationError, IntegerDomainError, dump, dumps
from ._impl import CanonicalizationError, FloatDomainError, IntegerDomainError, dump, dumps

__all__ = [
"CanonicalizationError",
"IntegerDomainError",
"FloatDomainError",
"dump",
"dumps",
]
7 changes: 7 additions & 0 deletions src/rfc8785/_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class IntegerDomainError(CanonicalizationError):
"""

def __init__(self, n: int) -> None:
"""
Initialize an `IntegerDomainError`.
"""
super().__init__(f"{n} exceeds safe integer domain for JSON floats")


Expand All @@ -59,6 +62,10 @@ class FloatDomainError(CanonicalizationError):
"""

def __init__(self, f: float) -> None:
"""
Initialize an `FloatDomainError`.
"""

super().__init__(f"{f} is not representable in JCS")


Expand Down
23 changes: 23 additions & 0 deletions test/test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,26 @@ def test_es6_float_stringification_full(es6_test_file):

actual = sink.getvalue().decode()
assert actual == expected


def test_integer_domain():
impl.dumps(impl._INT_MAX)
with pytest.raises(impl.IntegerDomainError):
impl.dumps(impl._INT_MAX + 1)

impl.dumps(impl._INT_MIN)
with pytest.raises(impl.IntegerDomainError):
impl.dumps(impl._INT_MIN - 1)


def test_string_invalid_utf8():
# escaped surrogate is fine
impl.dumps("\\udead")
with pytest.raises(impl.CanonicalizationError):
impl.dumps("\udead")


def test_dumps_invalid_type():
with pytest.raises(impl.CanonicalizationError):
# set is not serializable
impl.dumps({1, 2, 3})
8 changes: 8 additions & 0 deletions test/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ def test_roundtrip(vector, name):

actual_deserialized = json.loads(actual_output)
assert actual_deserialized == py_input


def test_exception_hierarchy():
assert issubclass(rfc8785.CanonicalizationError, ValueError)
assert issubclass(rfc8785.IntegerDomainError, rfc8785.CanonicalizationError)
assert issubclass(rfc8785.FloatDomainError, rfc8785.CanonicalizationError)
assert not issubclass(rfc8785.IntegerDomainError, rfc8785.FloatDomainError)
assert not issubclass(rfc8785.FloatDomainError, rfc8785.IntegerDomainError)