Skip to content

Commit

Permalink
chore: rename format.py to interchange.py
Browse files Browse the repository at this point in the history
  • Loading branch information
seandstewart committed Jul 7, 2024
1 parent 6996275 commit ae32725
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/typelib/format.py → src/typelib/interchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dataclasses
import typing as tp

from typelib import classes, compat
from typelib import classes, compat, inspection
from typelib import codec as mcodec
from typelib import marshal as mmarshal
from typelib import unmarshal as munmarshal
Expand All @@ -21,6 +21,13 @@ def protocol(
) -> InterchangeProtocol[T]:
marshal = marshaller or mmarshal.marshaller(typ=t)
unmarshal = unmarshaller or munmarshal.unmarshaller(typ=t)
if inspection.isbytestype(t) and codec is None:
codec = mcodec.Codec(
marshaller=marshal,
unmarshaller=unmarshal,
encoder=lambda v: v, # type: ignore[arg-type,return-value]
decoder=lambda v: v, # type: ignore[arg-type,return-value]
)
codec = codec or mcodec.Codec(
marshaller=marshal,
unmarshaller=unmarshal,
Expand Down
7 changes: 5 additions & 2 deletions tests/unit/test_format.py → tests/unit/test_interchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import uuid

import pytest
from typelib import compat, format, refs
from typelib import compat, interchange, refs

from tests import models

Expand Down Expand Up @@ -175,10 +175,13 @@ def test_protocol(
given_type, given_input, expected_unmarshal_output, expected_marshal_output
):
# Given
proto = format.protocol(given_type)
proto = interchange.protocol(given_type)
# When
unmarshal_output = proto.unmarshal(given_input)
marshal_output = proto.marshal(unmarshal_output)
encode_output = proto.codec.encode(unmarshal_output)
decode_output = proto.codec.decode(encode_output)
# Then
assert unmarshal_output == expected_unmarshal_output
assert marshal_output == expected_marshal_output
assert decode_output == expected_unmarshal_output

0 comments on commit ae32725

Please sign in to comment.