From 543356e4add2e215f90d36e011efc489e0d2e936 Mon Sep 17 00:00:00 2001 From: Alexander Tikhonov Date: Thu, 8 Apr 2021 12:10:23 +0300 Subject: [PATCH] Add support for __slots__ --- mashumaro/serializer/base/dict.py | 2 ++ mashumaro/serializer/json.py | 2 ++ mashumaro/serializer/msgpack.py | 2 ++ mashumaro/serializer/yaml.py | 2 ++ tests/test_aliases.py | 2 +- tests/test_common.py | 52 +++++++++++++++++++++++++++++++ tests/test_config.py | 5 +-- 7 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 tests/test_common.py diff --git a/mashumaro/serializer/base/dict.py b/mashumaro/serializer/base/dict.py index 8bea42fa..64961072 100644 --- a/mashumaro/serializer/base/dict.py +++ b/mashumaro/serializer/base/dict.py @@ -6,6 +6,8 @@ class DataClassDictMixin: + __slots__ = () + def __init_subclass__(cls: Type[T], **kwargs): builder = CodeBuilder(cls) exc = None diff --git a/mashumaro/serializer/json.py b/mashumaro/serializer/json.py index 203193cf..70111559 100644 --- a/mashumaro/serializer/json.py +++ b/mashumaro/serializer/json.py @@ -26,6 +26,8 @@ def __call__(self, s: EncodedData, **kwargs) -> Dict[Any, Any]: class DataClassJSONMixin(DataClassDictMixin): + __slots__ = () + def to_json( self: T, encoder: Encoder = json.dumps, diff --git a/mashumaro/serializer/msgpack.py b/mashumaro/serializer/msgpack.py index e771ddd9..9cd49d65 100644 --- a/mashumaro/serializer/msgpack.py +++ b/mashumaro/serializer/msgpack.py @@ -27,6 +27,8 @@ def __call__(self, packed: EncodedData, **kwargs) -> Dict[Any, Any]: class DataClassMessagePackMixin(DataClassDictMixin): + __slots__ = () + def to_msgpack( self: T, encoder: Encoder = partial(msgpack.packb, use_bin_type=True), diff --git a/mashumaro/serializer/yaml.py b/mashumaro/serializer/yaml.py index 03c4c76d..0dafa3d0 100644 --- a/mashumaro/serializer/yaml.py +++ b/mashumaro/serializer/yaml.py @@ -26,6 +26,8 @@ def __call__(self, packed: EncodedData, **kwargs) -> Dict[Any, Any]: class DataClassYAMLMixin(DataClassDictMixin): + __slots__ = () + def to_yaml( self: T, encoder: Encoder = yaml.dump, # type: ignore diff --git a/tests/test_aliases.py b/tests/test_aliases.py index 4abb76f5..fc9df05d 100644 --- a/tests/test_aliases.py +++ b/tests/test_aliases.py @@ -5,9 +5,9 @@ from mashumaro import DataClassDictMixin from mashumaro.config import ( - BaseConfig, TO_DICT_ADD_BY_ALIAS_FLAG, TO_DICT_ADD_OMIT_NONE_FLAG, + BaseConfig, ) from mashumaro.exceptions import MissingField diff --git a/tests/test_common.py b/tests/test_common.py new file mode 100644 index 00000000..48fb0539 --- /dev/null +++ b/tests/test_common.py @@ -0,0 +1,52 @@ +from dataclasses import dataclass + +import pytest + +from mashumaro import ( + DataClassDictMixin, + DataClassJSONMixin, + DataClassMessagePackMixin, + DataClassYAMLMixin, +) + + +def test_slots(): + @dataclass + class RegularDataClass: + __slots__ = ("number",) + number: int + + @dataclass + class DictDataClass(DataClassDictMixin): + __slots__ = ("number",) + number: int + + @dataclass + class JSONDataClass(DataClassJSONMixin): + __slots__ = ("number",) + number: int + + @dataclass + class MessagePackDataClass(DataClassMessagePackMixin): + __slots__ = ("number",) + number: int + + @dataclass + class YAMLDataClass(DataClassYAMLMixin): + __slots__ = ("number",) + number: int + + for cls in ( + RegularDataClass, + DictDataClass, + JSONDataClass, + MessagePackDataClass, + YAMLDataClass, + ): + instance = cls(1) + with pytest.raises(AttributeError) as e: + instance.new_attribute = 2 + assert ( + str(e.value) + == f"'{cls.__name__}' object has no attribute 'new_attribute'" + ) diff --git a/tests/test_config.py b/tests/test_config.py index 824fedea..4156a0b4 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -4,10 +4,7 @@ import pytest from mashumaro import DataClassDictMixin -from mashumaro.config import ( - TO_DICT_ADD_OMIT_NONE_FLAG, - BaseConfig, -) +from mashumaro.config import TO_DICT_ADD_OMIT_NONE_FLAG, BaseConfig from mashumaro.types import SerializationStrategy from .entities import (