Skip to content

Commit

Permalink
Rename base codec to basic codec and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Nov 19, 2023
1 parent a0c6dc5 commit a3a17bf
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 162 deletions.
423 changes: 324 additions & 99 deletions README.md

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions mashumaro/codecs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from .base import Decoder, Encoder, decode, encode
from .basic import BasicDecoder, BasicEncoder

__all__ = [
"Decoder",
"Encoder",
"decode",
"encode",
"BasicDecoder",
"BasicEncoder",
]
19 changes: 12 additions & 7 deletions mashumaro/codecs/base.py → mashumaro/codecs/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
T = TypeVar("T")


class Decoder(Generic[T]):
class BasicDecoder(Generic[T]):
@overload
def __init__(
self,
Expand Down Expand Up @@ -55,14 +55,15 @@ def decode(self, data: Any) -> T: # pragma: no cover
...


class Encoder(Generic[T]):
class BasicEncoder(Generic[T]):
@overload
def __init__(
self,
shape_type: Type[T],
*,
default_dialect: Optional[Type[Dialect]] = None,
post_encoder_func: Optional[Callable[[Any], Any]] = None,
support_subclasses: bool = False,
):
...

Expand All @@ -73,6 +74,7 @@ def __init__(
*,
default_dialect: Optional[Type[Dialect]] = None,
post_encoder_func: Optional[Callable[[Any], Any]] = None,
support_subclasses: bool = False,
):
...

Expand All @@ -82,28 +84,31 @@ def __init__(
*,
default_dialect: Optional[Type[Dialect]] = None,
post_encoder_func: Optional[Callable[[Any], Any]] = None,
support_subclasses: bool = False,
):
code_builder = CodecCodeBuilder.new(
type_args=get_args(shape_type), default_dialect=default_dialect
)
code_builder.add_encode_method(shape_type, self, post_encoder_func)
code_builder.add_encode_method(
shape_type, self, post_encoder_func, support_subclasses
)

@final
def encode(self, obj: T) -> Any: # pragma: no cover
...


def decode(data: Any, shape_type: Union[Type[T], Any]) -> T:
return Decoder(shape_type).decode(data)
return BasicDecoder(shape_type).decode(data)


def encode(obj: T, shape_type: Union[Type[T], Any]) -> Any:
return Encoder(shape_type).encode(obj)
return BasicEncoder(shape_type).encode(obj)


__all__ = [
"Decoder",
"Encoder",
"BasicDecoder",
"BasicEncoder",
"decode",
"encode",
]
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import pytest
from typing_extensions import Literal

from mashumaro.codecs import Decoder, Encoder, decode, encode
from mashumaro.codecs import BasicDecoder, BasicEncoder
from mashumaro.codecs.basic import decode, encode
from mashumaro.dialect import Dialect
from tests.entities import (
DataClassWithoutMixin,
Expand Down Expand Up @@ -119,31 +120,31 @@ def test_encode(shape_type, encoded, decoded):


def test_decoder_with_default_dialect():
decoder = Decoder(List[date], default_dialect=MyDialect)
decoder = BasicDecoder(List[date], default_dialect=MyDialect)
assert decoder.decode([738785, 738786]) == [
date(2023, 9, 22),
date(2023, 9, 23),
]


def test_encoder_with_default_dialect():
encoder = Encoder(List[date], default_dialect=MyDialect)
encoder = BasicEncoder(List[date], default_dialect=MyDialect)
assert encoder.encode([date(2023, 9, 22), date(2023, 9, 23)]) == [
738785,
738786,
]


def test_pre_decoder_func():
decoder = Decoder(List[date], pre_decoder_func=lambda v: v.split(","))
decoder = BasicDecoder(List[date], pre_decoder_func=lambda v: v.split(","))
assert decoder.decode("2023-09-22,2023-09-23") == [
date(2023, 9, 22),
date(2023, 9, 23),
]


def test_post_encoder_func():
encoder = Encoder(List[date], post_encoder_func=lambda v: ",".join(v))
encoder = BasicEncoder(List[date], post_encoder_func=lambda v: ",".join(v))
assert (
encoder.encode(
[
Expand All @@ -160,7 +161,7 @@ def test_post_encoder_func():
[[Union[date, datetime], "foo"], [Literal["foo"], "bar"]],
)
def test_value_error_on_decode(shape_type, invalid_value):
decoder = Decoder(shape_type)
decoder = BasicDecoder(shape_type)
with pytest.raises(ValueError) as e:
decoder.decode(invalid_value)
assert type(e.value) is ValueError
Expand All @@ -171,7 +172,7 @@ def test_value_error_on_decode(shape_type, invalid_value):
[[Union[date, datetime], "foo"], [Literal["foo"], "bar"]],
)
def test_value_error_on_encode(shape_type, invalid_value):
encoder = Encoder(shape_type)
encoder = BasicEncoder(shape_type)
with pytest.raises(ValueError) as e:
encoder.encode(invalid_value)
assert type(e.value) is ValueError
Expand All @@ -189,8 +190,8 @@ class MyClass:
l1: Literal["l1"]
l2: Literal["l2"]

decoder = Decoder(MyClass)
encoder = Encoder(MyClass)
decoder = BasicDecoder(MyClass)
encoder = BasicEncoder(MyClass)
data = {
"td1": {"x": "2023-11-15", "y": 1},
"td2": {"x": "2023-11-15", "y": 2},
Expand Down Expand Up @@ -221,8 +222,8 @@ class MyClass:
x1: Foo
x2: Bar

decoder = Decoder(MyClass)
encoder = Encoder(MyClass)
decoder = BasicDecoder(MyClass)
encoder = BasicEncoder(MyClass)
data = {"x1": {"foo": "foo"}, "x2": {"bar": "bar"}}
obj = MyClass(x1=Foo("foo"), x2=Bar("bar"))
assert decoder.decode(data) == obj
Expand All @@ -235,8 +236,8 @@ class MyClass:
x1: MyGenericDataClass[str]
x2: MyGenericDataClass[date]

decoder = Decoder(MyClass)
encoder = Encoder(MyClass)
decoder = BasicDecoder(MyClass)
encoder = BasicEncoder(MyClass)
data = {"x1": {"x": "2023-11-15"}, "x2": {"x": "2023-11-15"}}
obj = MyClass(
x1=MyGenericDataClass("2023-11-15"),
Expand Down
35 changes: 18 additions & 17 deletions tests/test_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
from typing_extensions import Final, LiteralString

from mashumaro import DataClassDictMixin
from mashumaro.codecs import Decoder, Encoder, decode, encode
from mashumaro.codecs import BasicDecoder, BasicEncoder
from mashumaro.codecs.basic import decode, encode
from mashumaro.config import BaseConfig
from mashumaro.core.const import PEP_585_COMPATIBLE, PY_39_MIN
from mashumaro.exceptions import (
Expand Down Expand Up @@ -1223,8 +1224,8 @@ class DataClass(DataClassDictMixin):
assert DataClass.from_dict({"x": ["1", "2.0"]}) == obj
assert obj.to_dict() == {"x": [1, 2.0]}

decoder = Decoder(DataClass)
encoder = Encoder(DataClass)
decoder = BasicDecoder(DataClass)
encoder = BasicEncoder(DataClass)
assert decoder.decode({"x": ["1", "2.0"]}) == obj
assert encoder.encode(obj) == {"x": [1, 2.0]}

Expand All @@ -1238,8 +1239,8 @@ class DataClass(DataClassDictMixin):
assert DataClass.from_dict({"x": ["1"]}) == obj
assert obj.to_dict() == {"x": [1, 2.0]}

decoder = Decoder(DataClass)
encoder = Encoder(DataClass)
decoder = BasicDecoder(DataClass)
encoder = BasicEncoder(DataClass)
assert decoder.decode({"x": ["1"]}) == obj
assert encoder.encode(obj) == {"x": [1, 2.0]}

Expand All @@ -1253,8 +1254,8 @@ class DataClass(DataClassDictMixin):
assert DataClass.from_dict({"x": ["1", "2.0"]}) == obj
assert obj.to_dict() == {"x": ["1", "2.0"]}

decoder = Decoder(DataClass)
encoder = Encoder(DataClass)
decoder = BasicDecoder(DataClass)
encoder = BasicEncoder(DataClass)
assert decoder.decode({"x": ["1", "2.0"]}) == obj
assert encoder.encode(obj) == {"x": ["1", "2.0"]}

Expand Down Expand Up @@ -1380,8 +1381,8 @@ class DataClass(DataClassDictMixin):
assert DataClass.from_dict({"x": {"x": 33, "y": 42}}) == obj
assert obj.to_dict() == {"x": {"x": 33, "y": 42}}

decoder = Decoder(DataClass)
encoder = Encoder(DataClass)
decoder = BasicDecoder(DataClass)
encoder = BasicEncoder(DataClass)
assert decoder.decode({"x": {"x": 33, "y": 42}}) == obj
assert encoder.encode(obj) == {"x": {"x": 33, "y": 42}}

Expand All @@ -1399,8 +1400,8 @@ class DataClass(DataClassDictMixin):
assert DataClass.from_dict({"x": {"x": 33, "y": 42}}) == obj
assert obj.to_dict() == {"x": {"x": 33, "y": 42}}

decoder = Decoder(DataClass)
encoder = Encoder(DataClass)
decoder = BasicDecoder(DataClass)
encoder = BasicEncoder(DataClass)
assert decoder.decode({"x": {"x": 33, "y": 42}}) == obj
assert encoder.encode(obj) == {"x": {"x": 33, "y": 42}}

Expand All @@ -1414,8 +1415,8 @@ class DataClass(DataClassDictMixin):
assert DataClass.from_dict({"x": {"x": "2023-01-22", "y": "42"}}) == obj
assert obj.to_dict() == {"x": {"x": "2023-01-22", "y": 42}}

decoder = Decoder(DataClass)
encoder = Encoder(DataClass)
decoder = BasicDecoder(DataClass)
encoder = BasicEncoder(DataClass)
assert decoder.decode({"x": {"x": "2023-01-22", "y": "42"}}) == obj
assert encoder.encode(obj) == {"x": {"x": "2023-01-22", "y": 42}}

Expand All @@ -1429,8 +1430,8 @@ class DataClass(DataClassDictMixin):
assert DataClass.from_dict({"x": {"x": "2023-01-22", "y": "42"}}) == obj
assert obj.to_dict() == {"x": {"x": "2023-01-22", "y": 42}}

decoder = Decoder(DataClass)
encoder = Encoder(DataClass)
decoder = BasicDecoder(DataClass)
encoder = BasicEncoder(DataClass)
assert decoder.decode({"x": {"x": "2023-01-22", "y": "42"}}) == obj
assert encoder.encode(obj) == {"x": {"x": "2023-01-22", "y": 42}}

Expand Down Expand Up @@ -1503,14 +1504,14 @@ class Config(BaseConfig):
@pytest.mark.parametrize("value_info", inner_values)
def test_decoder(value_info):
x_type, x_value, x_value_dumped = value_info
decoder = Decoder(x_type)
decoder = BasicDecoder(x_type)
assert decoder.decode(x_value_dumped) == x_value
assert decode(x_value_dumped, x_type) == x_value


@pytest.mark.parametrize("value_info", inner_values)
def test_encoder(value_info):
x_type, x_value, x_value_dumped = value_info
encoder = Encoder(x_type)
encoder = BasicEncoder(x_type)
assert encoder.encode(x_value) == x_value_dumped
assert encode(x_value, x_type) == x_value_dumped
10 changes: 5 additions & 5 deletions tests/test_discriminated_unions/test_dialects.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from typing_extensions import Annotated, Literal

from mashumaro.codecs import Decoder
from mashumaro.codecs import BasicDecoder
from mashumaro.codecs.orjson import ORJSONDecoder
from mashumaro.config import ADD_DIALECT_SUPPORT, BaseConfig
from mashumaro.dialect import Dialect
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_passing_dialect_to_config_based_variant_subtypes():
assert Variant1.from_dict(
{"type": 1, "x": "2023-06-03", "y": [["1", 2], ["3", 4]]}
) == Variant1Subtype1(date(2023, 6, 3), {"1": 2, "3": 4})
decoder1 = Decoder(_Variant1, default_dialect=DefaultDialect)
decoder1 = BasicDecoder(_Variant1, default_dialect=DefaultDialect)
assert decoder1.decode(
{"type": 1, "x": "2023-06-03", "y": [["1", 2], ["3", 4]]}
) == _Variant1Subtype1(date(2023, 6, 3), {"1": 2, "3": 4})
Expand All @@ -215,7 +215,7 @@ def test_passing_dialect_to_config_based_variant_subtypes():
assert Variant1.from_dict(
{"type": 1, "x": 738674, "y": {"1": 2, "3": 4}}, dialect=MyDialect
) == Variant1Subtype1(date(2023, 6, 3), {"1": 2, "3": 4})
decoder3 = Decoder(_Variant1, default_dialect=MyDialect)
decoder3 = BasicDecoder(_Variant1, default_dialect=MyDialect)
assert decoder3.decode(
{"type": 1, "x": 738674, "y": {"1": 2, "3": 4}}
) == _Variant1Subtype1(date(2023, 6, 3), {"1": 2, "3": 4})
Expand Down Expand Up @@ -269,7 +269,7 @@ def test_passing_dialect_to_annotation_based_variant_subtypes():
assert Variant2Wrapper.from_dict(
{"x": {"type": 1, "x": "2023-06-03", "y": [["1", 2], ["3", 4]]}}
) == Variant2Wrapper(Variant2Subtype1(date(2023, 6, 3), {"1": 2, "3": 4}))
decoder1 = Decoder(_Variant2Wrapper)
decoder1 = BasicDecoder(_Variant2Wrapper)
assert decoder1.decode(
{"x": {"type": 1, "x": "2023-06-03", "y": [["1", 2], ["3", 4]]}}
) == _Variant2Wrapper(
Expand Down Expand Up @@ -341,7 +341,7 @@ def test_passing_dialect_to_annotation_based_union_subtypes():
assert Variant34Wrapper.from_dict(
{"x": {"type": 3, "x": "2023-06-03", "y": [["1", 2], ["3", 4]]}}
) == Variant34Wrapper(Variant3Subtype(date(2023, 6, 3), {"1": 2, "3": 4}))
decoder1 = Decoder(_Variant34Wrapper)
decoder1 = BasicDecoder(_Variant34Wrapper)
assert decoder1.decode(
{"x": {"type": 3, "x": "2023-06-03", "y": [["1", 2], ["3", 4]]}}
) == _Variant34Wrapper(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_discriminated_unions/test_parent_by_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing_extensions import Annotated, Literal

from mashumaro import DataClassDictMixin
from mashumaro.codecs import decode
from mashumaro.codecs.basic import decode
from mashumaro.exceptions import InvalidFieldValue
from mashumaro.types import Discriminator

Expand Down
2 changes: 1 addition & 1 deletion tests/test_discriminated_unions/test_parent_via_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing_extensions import Literal

from mashumaro import DataClassDictMixin
from mashumaro.codecs import decode, encode
from mashumaro.codecs.basic import decode
from mashumaro.config import BaseConfig
from mashumaro.exceptions import (
InvalidFieldValue,
Expand Down
Loading

0 comments on commit a3a17bf

Please sign in to comment.