Skip to content

Commit

Permalink
Add support for __slots__
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Apr 8, 2021
1 parent 41f615c commit 543356e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 5 deletions.
2 changes: 2 additions & 0 deletions mashumaro/serializer/base/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@


class DataClassDictMixin:
__slots__ = ()

def __init_subclass__(cls: Type[T], **kwargs):
builder = CodeBuilder(cls)
exc = None
Expand Down
2 changes: 2 additions & 0 deletions mashumaro/serializer/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions mashumaro/serializer/msgpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions mashumaro/serializer/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
52 changes: 52 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -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'"
)
5 changes: 1 addition & 4 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 543356e

Please sign in to comment.