@@ -84,27 +84,50 @@ def unstructure_tagged_union(
8484 return res
8585
8686 if default is NOTHING :
87+ if getattr (converter , "forbid_extra_keys" , False ):
8788
88- def structure_tagged_union (
89- val : dict , _ , _tag_to_cl = tag_to_hook , _tag_name = tag_name
90- ) -> union :
91- val = val .copy ()
92- return _tag_to_cl [val .pop (_tag_name )](val )
89+ def structure_tagged_union (
90+ val : dict , _ , _tag_to_cl = tag_to_hook , _tag_name = tag_name
91+ ) -> union :
92+ val = val .copy ()
93+ return _tag_to_cl [val .pop (_tag_name )](val )
94+
95+ else :
96+
97+ def structure_tagged_union (
98+ val : dict , _ , _tag_to_cl = tag_to_hook , _tag_name = tag_name
99+ ) -> union :
100+ return _tag_to_cl [val [_tag_name ]](val )
93101
94102 else :
103+ if getattr (converter , "forbid_extra_keys" , False ):
104+
105+ def structure_tagged_union (
106+ val : dict ,
107+ _ ,
108+ _tag_to_hook = tag_to_hook ,
109+ _tag_name = tag_name ,
110+ _dh = default_handler ,
111+ _default = default ,
112+ ) -> union :
113+ if _tag_name in val :
114+ val = val .copy ()
115+ return _tag_to_hook [val .pop (_tag_name )](val )
116+ return _dh (val , _default )
95117
96- def structure_tagged_union (
97- val : dict ,
98- _ ,
99- _tag_to_hook = tag_to_hook ,
100- _tag_name = tag_name ,
101- _dh = default_handler ,
102- _default = default ,
103- ) -> union :
104- if _tag_name in val :
105- val = val .copy ()
106- return _tag_to_hook [val .pop (_tag_name )](val )
107- return _dh (val , _default )
118+ else :
119+
120+ def structure_tagged_union (
121+ val : dict ,
122+ _ ,
123+ _tag_to_hook = tag_to_hook ,
124+ _tag_name = tag_name ,
125+ _dh = default_handler ,
126+ _default = default ,
127+ ) -> union :
128+ if _tag_name in val :
129+ return _tag_to_hook [val [_tag_name ]](val )
130+ return _dh (val , _default )
108131
109132 converter .register_unstructure_hook (union , unstructure_tagged_union )
110133 converter .register_structure_hook (union , structure_tagged_union )
0 commit comments