From ae32725a78048dcacc24582eff66adc4ed44fea2 Mon Sep 17 00:00:00 2001 From: Sean Stewart Date: Sun, 7 Jul 2024 09:26:14 -0400 Subject: [PATCH] chore: rename `format.py` to `interchange.py` --- src/typelib/{format.py => interchange.py} | 9 ++++++++- tests/unit/{test_format.py => test_interchange.py} | 7 +++++-- 2 files changed, 13 insertions(+), 3 deletions(-) rename src/typelib/{format.py => interchange.py} (76%) rename tests/unit/{test_format.py => test_interchange.py} (95%) diff --git a/src/typelib/format.py b/src/typelib/interchange.py similarity index 76% rename from src/typelib/format.py rename to src/typelib/interchange.py index f240bf3..def149f 100644 --- a/src/typelib/format.py +++ b/src/typelib/interchange.py @@ -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 @@ -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, diff --git a/tests/unit/test_format.py b/tests/unit/test_interchange.py similarity index 95% rename from tests/unit/test_format.py rename to tests/unit/test_interchange.py index f88ac5e..4e94ee9 100644 --- a/tests/unit/test_format.py +++ b/tests/unit/test_interchange.py @@ -7,7 +7,7 @@ import uuid import pytest -from typelib import compat, format, refs +from typelib import compat, interchange, refs from tests import models @@ -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