22
33import unittest
44import uuid
5- from dataclasses import dataclass , field
5+ from dataclasses import InitVar , dataclass , field
66from enum import Enum
7- from typing import Dict , Optional
7+ from typing import ClassVar , Dict , Optional
88
99import orjson
1010
@@ -51,9 +51,12 @@ class Datasubclass(Dataclass1):
5151
5252@dataclass
5353class Slotsdataclass :
54- __slots__ = ("a" , "b" )
54+ __slots__ = ("a" , "b" , "_c" , "d" )
5555 a : str
5656 b : int
57+ _c : str
58+ d : InitVar [str ]
59+ cls_var : ClassVar [str ] = "cls"
5760
5861
5962@dataclass
@@ -70,6 +73,18 @@ class UnsortedDataclass:
7073 d : Optional [Dict ]
7174
7275
76+ @dataclass
77+ class InitDataclass :
78+ a : InitVar [str ]
79+ b : InitVar [str ]
80+ cls_var : ClassVar [str ] = "cls"
81+ ab : str = ""
82+
83+ def __post_init__ (self , a : str , b : str ):
84+ self ._other = 1
85+ self .ab = f"{ a } { b } "
86+
87+
7388class DataclassTests (unittest .TestCase ):
7489 def test_dataclass (self ):
7590 """
@@ -146,9 +161,9 @@ def test_dataclass_subclass(self):
146161
147162 def test_dataclass_slots (self ):
148163 """
149- dumps() dataclass with __slots__
164+ dumps() dataclass with __slots__ does not include under attributes, InitVar, or ClassVar
150165 """
151- obj = Slotsdataclass ("a" , 1 )
166+ obj = Slotsdataclass ("a" , 1 , "c" , "d" )
152167 assert "__dict__" not in dir (obj )
153168 self .assertEqual (orjson .dumps (obj ), b'{"a":"a","b":1}' )
154169
@@ -191,9 +206,18 @@ def test_dataclass_sort_sub(self):
191206 b'{"c":1,"b":2,"a":3,"d":{"e":1,"f":2}}' ,
192207 )
193208
209+ def test_dataclass_under (self ):
210+ """
211+ dumps() does not include under attributes, InitVar, or ClassVar
212+ """
213+ obj = InitDataclass ("zxc" , "vbn" )
214+ self .assertEqual (
215+ orjson .dumps (obj ), b'{"ab":"zxc vbn"}' ,
216+ )
217+
194218 def test_dataclass_option (self ):
195219 """
196- dumps() accepts deprecated OPT_SERIALIZE_DATACALSS
220+ dumps() accepts deprecated OPT_SERIALIZE_DATACLASS
197221 """
198222 obj = Dataclass1 ("a" , 1 , None )
199223 self .assertEqual (
0 commit comments