1- # pylint: disable=too-many-lines
1+ # pylint: disable=line-too-long,useless-suppression, too-many-lines
22# --------------------------------------------------------------------------
33#
44# Copyright (c) Microsoft Corporation. All rights reserved.
4848 IO ,
4949 Mapping ,
5050 Callable ,
51- TypeVar ,
5251 MutableMapping ,
53- Type ,
5452 List ,
5553)
5654
6159import xml .etree .ElementTree as ET
6260
6361import isodate # type: ignore
62+ from typing_extensions import Self
6463
6564from azure .core .exceptions import DeserializationError , SerializationError
6665from azure .core .serialization import NULL as CoreNull
6766
6867_BOM = codecs .BOM_UTF8 .decode (encoding = "utf-8" )
6968
70- ModelType = TypeVar ("ModelType" , bound = "Model" )
7169JSON = MutableMapping [str , Any ]
7270
7371
@@ -185,73 +183,7 @@ def deserialize_from_http_generics(cls, body_bytes: Optional[Union[AnyStr, IO]],
185183except NameError :
186184 _long_type = int
187185
188-
189- class UTC (datetime .tzinfo ):
190- """Time Zone info for handling UTC"""
191-
192- def utcoffset (self , dt ):
193- """UTF offset for UTC is 0.
194-
195- :param datetime.datetime dt: The datetime
196- :returns: The offset
197- :rtype: datetime.timedelta
198- """
199- return datetime .timedelta (0 )
200-
201- def tzname (self , dt ):
202- """Timestamp representation.
203-
204- :param datetime.datetime dt: The datetime
205- :returns: The timestamp representation
206- :rtype: str
207- """
208- return "Z"
209-
210- def dst (self , dt ):
211- """No daylight saving for UTC.
212-
213- :param datetime.datetime dt: The datetime
214- :returns: The daylight saving time
215- :rtype: datetime.timedelta
216- """
217- return datetime .timedelta (hours = 1 )
218-
219-
220- try :
221- from datetime import timezone as _FixedOffset # type: ignore
222- except ImportError : # Python 2.7
223-
224- class _FixedOffset (datetime .tzinfo ): # type: ignore
225- """Fixed offset in minutes east from UTC.
226- Copy/pasted from Python doc
227- :param datetime.timedelta offset: offset in timedelta format
228- """
229-
230- def __init__ (self , offset ) -> None :
231- self .__offset = offset
232-
233- def utcoffset (self , dt ):
234- return self .__offset
235-
236- def tzname (self , dt ):
237- return str (self .__offset .total_seconds () / 3600 )
238-
239- def __repr__ (self ):
240- return "<FixedOffset {}>" .format (self .tzname (None ))
241-
242- def dst (self , dt ):
243- return datetime .timedelta (0 )
244-
245- def __getinitargs__ (self ):
246- return (self .__offset ,)
247-
248-
249- try :
250- from datetime import timezone
251-
252- TZ_UTC = timezone .utc
253- except ImportError :
254- TZ_UTC = UTC () # type: ignore
186+ TZ_UTC = datetime .timezone .utc
255187
256188_FLATTEN = re .compile (r"(?<!\\)\." )
257189
@@ -310,7 +242,7 @@ def _create_xml_node(tag, prefix=None, ns=None):
310242 return ET .Element (tag )
311243
312244
313- class Model ( object ) :
245+ class Model :
314246 """Mixin for all client request body/response body models to support
315247 serialization and deserialization.
316248 """
@@ -450,25 +382,25 @@ def _infer_class_models(cls):
450382 return client_models
451383
452384 @classmethod
453- def deserialize (cls : Type [ ModelType ] , data : Any , content_type : Optional [str ] = None ) -> ModelType :
385+ def deserialize (cls , data : Any , content_type : Optional [str ] = None ) -> Self :
454386 """Parse a str using the RestAPI syntax and return a model.
455387
456388 :param str data: A str using RestAPI structure. JSON by default.
457389 :param str content_type: JSON by default, set application/xml if XML.
458390 :returns: An instance of this model
459391 :raises DeserializationError: if something went wrong
460- :rtype: ModelType
392+ :rtype: Self
461393 """
462394 deserializer = Deserializer (cls ._infer_class_models ())
463395 return deserializer (cls .__name__ , data , content_type = content_type ) # type: ignore
464396
465397 @classmethod
466398 def from_dict (
467- cls : Type [ ModelType ] ,
399+ cls ,
468400 data : Any ,
469401 key_extractors : Optional [Callable [[str , Dict [str , Any ], Any ], Any ]] = None ,
470402 content_type : Optional [str ] = None ,
471- ) -> ModelType :
403+ ) -> Self :
472404 """Parse a dict using given key extractor return a model.
473405
474406 By default consider key
@@ -480,7 +412,7 @@ def from_dict(
480412 :param str content_type: JSON by default, set application/xml if XML.
481413 :returns: An instance of this model
482414 :raises DeserializationError: if something went wrong
483- :rtype: ModelType
415+ :rtype: Self
484416 """
485417 deserializer = Deserializer (cls ._infer_class_models ())
486418 deserializer .key_extractors = ( # type: ignore
@@ -563,7 +495,7 @@ def _decode_attribute_map_key(key):
563495 return key .replace ("\\ ." , "." )
564496
565497
566- class Serializer ( object ) : # pylint: disable=too-many-public-methods
498+ class Serializer : # pylint: disable=too-many-public-methods
567499 """Request object model serializer."""
568500
569501 basic_types = {str : "str" , int : "int" , bool : "bool" , float : "float" }
@@ -1429,7 +1361,7 @@ def xml_key_extractor(attr, attr_desc, data): # pylint: disable=unused-argument
14291361 # Iter and wrapped, should have found one node only (the wrap one)
14301362 if len (children ) != 1 :
14311363 raise DeserializationError (
1432- "Tried to deserialize an array not wrapped, and found several nodes '{}'. Maybe you should declare this array as wrapped?" .format ( # pylint: disable=line-too-long
1364+ "Tried to deserialize an array not wrapped, and found several nodes '{}'. Maybe you should declare this array as wrapped?" .format (
14331365 xml_name
14341366 )
14351367 )
@@ -1441,7 +1373,7 @@ def xml_key_extractor(attr, attr_desc, data): # pylint: disable=unused-argument
14411373 return children [0 ]
14421374
14431375
1444- class Deserializer ( object ) :
1376+ class Deserializer :
14451377 """Response object model deserializer.
14461378
14471379 :param dict classes: Class type dictionary for deserializing complex types.
@@ -1683,17 +1615,21 @@ def _instantiate_model(self, response, attrs, additional_properties=None):
16831615 subtype = getattr (response , "_subtype_map" , {})
16841616 try :
16851617 readonly = [
1686- k for k , v in response ._validation .items () if v .get ("readonly" ) # pylint: disable=protected-access
1618+ k
1619+ for k , v in response ._validation .items () # pylint: disable=protected-access # type: ignore
1620+ if v .get ("readonly" )
16871621 ]
16881622 const = [
1689- k for k , v in response ._validation .items () if v .get ("constant" ) # pylint: disable=protected-access
1623+ k
1624+ for k , v in response ._validation .items () # pylint: disable=protected-access # type: ignore
1625+ if v .get ("constant" )
16901626 ]
16911627 kwargs = {k : v for k , v in attrs .items () if k not in subtype and k not in readonly + const }
16921628 response_obj = response (** kwargs )
16931629 for attr in readonly :
16941630 setattr (response_obj , attr , attrs .get (attr ))
16951631 if additional_properties :
1696- response_obj .additional_properties = additional_properties
1632+ response_obj .additional_properties = additional_properties # type: ignore
16971633 return response_obj
16981634 except TypeError as err :
16991635 msg = "Unable to deserialize {} into model {}. " .format (kwargs , response ) # type: ignore
@@ -2047,7 +1983,7 @@ def deserialize_rfc(attr):
20471983 try :
20481984 parsed_date = email .utils .parsedate_tz (attr ) # type: ignore
20491985 date_obj = datetime .datetime (
2050- * parsed_date [:6 ], tzinfo = _FixedOffset (datetime .timedelta (minutes = (parsed_date [9 ] or 0 ) / 60 ))
1986+ * parsed_date [:6 ], tzinfo = datetime . timezone (datetime .timedelta (minutes = (parsed_date [9 ] or 0 ) / 60 ))
20511987 )
20521988 if not date_obj .tzinfo :
20531989 date_obj = date_obj .astimezone (tz = TZ_UTC )
0 commit comments