99 get_origin ,
1010 Mapping ,
1111 TYPE_CHECKING ,
12+ Literal ,
1213)
1314from unittest import TestCase
1415
@@ -49,6 +50,7 @@ class SimpleTypesContainer(MappingConstructible, JsonSerializable):
4950 d : float = 0.0
5051 e : str = "abc"
5152 f : type = int
53+ g : Literal [0 , 1 , True , False , "on" , "off" ] = 0
5254
5355
5456@dataclass ()
@@ -122,13 +124,29 @@ def test_assumptions(self):
122124class JsonSerializableTest (TestCase ):
123125 def test_simple_ok (self ):
124126 self .assertEqual (
125- {"a" : None , "b" : False , "c" : 0 , "d" : 0.0 , "e" : "abc" , "f" : "int" },
127+ {
128+ "a" : None ,
129+ "b" : False ,
130+ "c" : 0 ,
131+ "d" : 0.0 ,
132+ "e" : "abc" ,
133+ "f" : "<class 'int'>" ,
134+ "g" : 0 ,
135+ },
126136 SimpleTypesContainer ().to_json (),
127137 )
128138 self .assertEqual (
129- {"a" : "?" , "b" : True , "c" : 12 , "d" : 34.56 , "e" : "uvw" , "f" : "bool" },
139+ {
140+ "a" : "?" ,
141+ "b" : True ,
142+ "c" : 12 ,
143+ "d" : 34.56 ,
144+ "e" : "uvw" ,
145+ "f" : "<class 'bool'>" ,
146+ "g" : "off" ,
147+ },
130148 SimpleTypesContainer (
131- a = "?" , b = True , c = 12 , d = 34.56 , e = "uvw" , f = bool
149+ a = "?" , b = True , c = 12 , d = 34.56 , e = "uvw" , f = bool , g = "off"
132150 ).to_json (),
133151 )
134152
@@ -144,7 +162,15 @@ def test_complex_ok(self):
144162 )
145163 self .assertEqual (
146164 {
147- "p" : {"a" : None , "b" : False , "c" : 0 , "d" : 0.0 , "e" : "abc" , "f" : "int" },
165+ "p" : {
166+ "a" : None ,
167+ "b" : False ,
168+ "c" : 0 ,
169+ "d" : 0.0 ,
170+ "e" : "abc" ,
171+ "f" : "<class 'int'>" ,
172+ "g" : 0 ,
173+ },
148174 "q" : {"p" : True , "q" : False },
149175 "r" : {
150176 "u" : {
@@ -153,27 +179,38 @@ def test_complex_ok(self):
153179 "c" : 0 ,
154180 "d" : 0.0 ,
155181 "e" : "abc" ,
156- "f" : "int" ,
182+ "f" : "<class 'int'>" ,
183+ "g" : 0 ,
157184 },
158185 "v" : {
159186 "a" : None ,
160187 "b" : False ,
161188 "c" : 0 ,
162189 "d" : 0.0 ,
163190 "e" : "abc" ,
164- "f" : "int" ,
191+ "f" : "<class 'int'>" ,
192+ "g" : 0 ,
165193 },
166194 },
167195 "s" : [1 , 2 , 3 ],
168196 "t" : [
169- {"a" : None , "b" : False , "c" : 5 , "d" : 6.7 , "e" : "abc" , "f" : "int" },
197+ {
198+ "a" : None ,
199+ "b" : False ,
200+ "c" : 5 ,
201+ "d" : 6.7 ,
202+ "e" : "abc" ,
203+ "f" : "<class 'int'>" ,
204+ "g" : 0 ,
205+ },
170206 {
171207 "a" : None ,
172208 "b" : False ,
173209 "c" : 8 ,
174210 "d" : 9.1 ,
175211 "e" : "abc" ,
176- "f" : "SimpleTypesContainer" ,
212+ "f" : "<class 'tests.util.test_codec.SimpleTypesContainer'>" ,
213+ "g" : 0 ,
177214 },
178215 ],
179216 "u" : None ,
@@ -288,7 +325,7 @@ def test_no_types_ok(self):
288325class MappingConstructibleTest (TestCase ):
289326
290327 def test_simple_ok (self ):
291- kwargs = dict (a = "?" , b = True , c = 12 , d = 34.56 , e = "uvw" , f = bytes )
328+ kwargs = dict (a = "?" , b = True , c = 12 , d = 34.56 , e = "uvw" , f = bytes , g = "on" )
292329 container = SimpleTypesContainer (** kwargs )
293330 self .assertEqual (container , SimpleTypesContainer .from_value (kwargs ))
294331 self .assertIs (container , SimpleTypesContainer .from_value (container ))
@@ -360,6 +397,12 @@ def test_simple_fail(self):
360397 ):
361398 SimpleTypesContainer .from_value ({"b" : None }, value_name = "stc" )
362399
400+ with pytest .raises (
401+ TypeError ,
402+ match = r"stc.g must be one of 0, 1, True, False, 'on', 'off', but got 74" ,
403+ ):
404+ SimpleTypesContainer .from_value ({"g" : 74 }, value_name = "stc" )
405+
363406 with pytest .raises (
364407 TypeError , match = "x is not a member of stc of type SimpleTypesContainer"
365408 ):
@@ -460,6 +503,7 @@ def test_resolves_types(self):
460503 "d" ,
461504 "e" ,
462505 "f" ,
506+ "g" ,
463507 "p" ,
464508 "q" ,
465509 "r" ,
0 commit comments