44(for serializers)
55
66"""
7+ from __future__ import annotations
8+
9+
710try :
811 import marshmallow
912except ImportError as exc : # pragma: no cover
1013 raise ImportError ("Package 'marshmallow' is required to use this module." ) from exc
1114
1215import datetime
13- from typing import Optional
16+ from typing import Any , Mapping , Optional
1417
1518import marshmallow .fields
1619
@@ -46,13 +49,18 @@ class RutField(marshmallow.fields.Field):
4649
4750 default_error_messages = {
4851 'invalid' : 'Not a syntactically valid RUT.' ,
52+ 'type' : 'Invalid type.' ,
4953 }
5054
51- def _serialize (self , value : Optional [object ], attr : str , obj : object ) -> Optional [str ]:
55+ def _serialize (
56+ self , value : Optional [object ], attr : str | None , obj : object , ** kwargs : Any
57+ ) -> Optional [str ]:
5258 validated = self ._validated (value )
5359 return validated .canonical if validated is not None else None
5460
55- def _deserialize (self , value : str , attr : str , data : dict ) -> Optional [Rut ]:
61+ def _deserialize (
62+ self , value : str , attr : str | None , data : Mapping [str , Any ] | None , ** kwargs : Any
63+ ) -> Optional [Rut ]:
5664 return self ._validated (value )
5765
5866 def _validated (self , value : Optional [object ]) -> Optional [Rut ]:
@@ -61,10 +69,10 @@ def _validated(self, value: Optional[object]) -> Optional[Rut]:
6169 else :
6270 try :
6371 validated = Rut (value , validate_dv = False ) # type: ignore
64- except TypeError :
65- self .fail ('type' )
66- except ValueError :
67- self .fail ('invalid' )
72+ except TypeError as exc :
73+ raise self .make_error ('type' ) from exc
74+ except ValueError as exc :
75+ raise self .make_error ('invalid' ) from exc
6876 return validated
6977
7078
@@ -89,13 +97,18 @@ class TipoDteField(marshmallow.fields.Field):
8997
9098 default_error_messages = {
9199 'invalid' : 'Not a valid Tipo DTE.' ,
100+ 'type' : 'Invalid type.' ,
92101 }
93102
94- def _serialize (self , value : Optional [object ], attr : str , obj : object ) -> Optional [int ]:
103+ def _serialize (
104+ self , value : Optional [object ], attr : str | None , obj : object , ** kwargs : Any
105+ ) -> Optional [int ]:
95106 validated : Optional [TipoDte ] = self ._validated (value )
96107 return validated .value if validated is not None else None
97108
98- def _deserialize (self , value : object , attr : str , data : dict ) -> Optional [TipoDte ]:
109+ def _deserialize (
110+ self , value : object , attr : str | None , data : Mapping [str , Any ] | None , ** kwargs : Any
111+ ) -> Optional [TipoDte ]:
99112 return self ._validated (value )
100113
101114 def _validated (self , value : Optional [object ]) -> Optional [TipoDte ]:
@@ -104,21 +117,21 @@ def _validated(self, value: Optional[object]) -> Optional[TipoDte]:
104117 else :
105118 if isinstance (value , bool ):
106119 # is value is bool, `isinstance(value, int)` is True and `int(value)` works!
107- self .fail ('type' )
120+ raise self .make_error ('type' )
108121 try :
109122 value = int (value ) # type: ignore
110- except ValueError :
123+ except ValueError as exc :
111124 # `int('x')` raises 'ValueError', not 'TypeError'
112- self .fail ('type' )
113- except TypeError :
125+ raise self .make_error ('type' ) from exc
126+ except TypeError as exc :
114127 # `int(date(2018, 10, 10))` raises 'TypeError', unlike `int('x')`
115- self .fail ('type' )
128+ raise self .make_error ('type' ) from exc
116129
117130 try :
118131 validated = TipoDte (value ) # type: ignore
119- except ValueError :
132+ except ValueError as exc :
120133 # TipoDte('x') raises 'ValueError', not 'TypeError'
121- self .fail ('invalid' )
134+ raise self .make_error ('invalid' ) from exc
122135 return validated
123136
124137
@@ -142,13 +155,18 @@ class RcvTipoDoctoField(marshmallow.fields.Field):
142155
143156 default_error_messages = {
144157 'invalid' : "Not a valid RCV's Tipo de Documento." ,
158+ 'type' : "Invalid type." ,
145159 }
146160
147- def _serialize (self , value : Optional [object ], attr : str , obj : object ) -> Optional [int ]:
161+ def _serialize (
162+ self , value : Optional [object ], attr : str | None , obj : object , ** kwargs : Any
163+ ) -> Optional [int ]:
148164 validated : Optional [RcvTipoDocto ] = self ._validated (value )
149165 return validated .value if validated is not None else None
150166
151- def _deserialize (self , value : object , attr : str , data : dict ) -> Optional [RcvTipoDocto ]:
167+ def _deserialize (
168+ self , value : object , attr : str | None , data : Mapping [str , Any ] | None , ** kwargs : Any
169+ ) -> Optional [RcvTipoDocto ]:
152170 return self ._validated (value )
153171
154172 def _validated (self , value : Optional [object ]) -> Optional [RcvTipoDocto ]:
@@ -157,21 +175,21 @@ def _validated(self, value: Optional[object]) -> Optional[RcvTipoDocto]:
157175 else :
158176 if isinstance (value , bool ):
159177 # is value is bool, `isinstance(value, int)` is True and `int(value)` works!
160- self .fail ('type' )
178+ raise self .make_error ('type' )
161179 try :
162180 value = int (value ) # type: ignore
163- except ValueError :
181+ except ValueError as exc :
164182 # `int('x')` raises 'ValueError', not 'TypeError'
165- self .fail ('type' )
166- except TypeError :
183+ raise self .make_error ('type' ) from exc
184+ except TypeError as exc :
167185 # `int(date(2018, 10, 10))` raises 'TypeError', unlike `int('x')`
168- self .fail ('type' )
186+ raise self .make_error ('type' ) from exc
169187
170188 try :
171189 validated = RcvTipoDocto (value ) # type: ignore
172- except ValueError :
190+ except ValueError as exc :
173191 # RcvTipoDocto('x') raises 'ValueError', not 'TypeError'
174- self .fail ('invalid' )
192+ raise self .make_error ('invalid' ) from exc
175193 return validated
176194
177195
@@ -186,14 +204,19 @@ class RcvPeriodoTributarioField(marshmallow.fields.Field):
186204
187205 default_error_messages = {
188206 'invalid' : "Not a valid RCV Periodo Tributario." ,
207+ 'type' : "Invalid type." ,
189208 }
190209 _string_format = '%Y-%m' # Example: '2019-12'
191210
192- def _serialize (self , value : Optional [object ], attr : str , obj : object ) -> Optional [str ]:
211+ def _serialize (
212+ self , value : Optional [object ], attr : str | None , obj : object , ** kwargs : Any
213+ ) -> Optional [str ]:
193214 validated : Optional [RcvPeriodoTributario ] = self ._validated (value )
194215 return validated .as_date ().strftime (self ._string_format ) if validated is not None else None
195216
196- def _deserialize (self , value : object , attr : str , data : dict ) -> Optional [RcvPeriodoTributario ]:
217+ def _deserialize (
218+ self , value : object , attr : str | None , data : Mapping [str , Any ] | None , ** kwargs : Any
219+ ) -> Optional [RcvPeriodoTributario ]:
197220 return self ._validated (value )
198221
199222 def _validated (self , value : Optional [object ]) -> Optional [RcvPeriodoTributario ]:
@@ -203,10 +226,10 @@ def _validated(self, value: Optional[object]) -> Optional[RcvPeriodoTributario]:
203226 try :
204227 value = datetime .datetime .strptime (value , self ._string_format ) # type: ignore
205228 value = value .date ()
206- except ValueError :
207- self .fail ('invalid' )
208- except TypeError :
209- self .fail ('type' )
229+ except ValueError as exc :
230+ raise self .make_error ('invalid' ) from exc
231+ except TypeError as exc :
232+ raise self .make_error ('type' ) from exc
210233
211234 validated = RcvPeriodoTributario .from_date (value ) # type: ignore
212235
0 commit comments