5
5
# Licensed under the MIT License. See License.txt in the project root for
6
6
# license information.
7
7
# --------------------------------------------------------------------------
8
- # pylint: disable=protected-access, arguments-differ, signature-differs, broad-except, too-many-lines
8
+ # pylint: disable=protected-access, broad-except
9
9
10
10
import copy
11
11
import calendar
@@ -373,15 +373,34 @@ def __ne__(self, other: typing.Any) -> bool:
373
373
return not self .__eq__ (other )
374
374
375
375
def keys (self ) -> typing .KeysView [str ]:
376
+ """
377
+ :returns: a set-like object providing a view on D's keys
378
+ :rtype: ~typing.KeysView
379
+ """
376
380
return self ._data .keys ()
377
381
378
382
def values (self ) -> typing .ValuesView [typing .Any ]:
383
+ """
384
+ :returns: an object providing a view on D's values
385
+ :rtype: ~typing.ValuesView
386
+ """
379
387
return self ._data .values ()
380
388
381
389
def items (self ) -> typing .ItemsView [str , typing .Any ]:
390
+ """
391
+ :returns: set-like object providing a view on D's items
392
+ :rtype: ~typing.ItemsView
393
+ """
382
394
return self ._data .items ()
383
395
384
396
def get (self , key : str , default : typing .Any = None ) -> typing .Any :
397
+ """
398
+ Get the value for key if key is in the dictionary, else default.
399
+ :param str key: The key to look up.
400
+ :param any default: The value to return if key is not in the dictionary. Defaults to None
401
+ :returns: D[k] if k in D, else d.
402
+ :rtype: any
403
+ """
385
404
try :
386
405
return self [key ]
387
406
except KeyError :
@@ -397,17 +416,38 @@ def pop(self, key: str, default: _T) -> _T: ...
397
416
def pop (self , key : str , default : typing .Any ) -> typing .Any : ...
398
417
399
418
def pop (self , key : str , default : typing .Any = _UNSET ) -> typing .Any :
419
+ """
420
+ Removes specified key and return the corresponding value.
421
+ :param str key: The key to pop.
422
+ :param any default: The value to return if key is not in the dictionary
423
+ :returns: The value corresponding to the key.
424
+ :rtype: any
425
+ :raises KeyError: If key is not found and default is not given.
426
+ """
400
427
if default is _UNSET :
401
428
return self ._data .pop (key )
402
429
return self ._data .pop (key , default )
403
430
404
431
def popitem (self ) -> typing .Tuple [str , typing .Any ]:
432
+ """
433
+ Removes and returns some (key, value) pair
434
+ :returns: The (key, value) pair.
435
+ :rtype: tuple
436
+ :raises KeyError: if D is empty.
437
+ """
405
438
return self ._data .popitem ()
406
439
407
440
def clear (self ) -> None :
441
+ """
442
+ Remove all items from D.
443
+ """
408
444
self ._data .clear ()
409
445
410
446
def update (self , * args : typing .Any , ** kwargs : typing .Any ) -> None :
447
+ """
448
+ Updates D from mapping/iterable E and F.
449
+ :param any args: Either a mapping object or an iterable of key-value pairs.
450
+ """
411
451
self ._data .update (* args , ** kwargs )
412
452
413
453
@typing .overload
@@ -417,6 +457,13 @@ def setdefault(self, key: str, default: None = None) -> None: ...
417
457
def setdefault (self , key : str , default : typing .Any ) -> typing .Any : ...
418
458
419
459
def setdefault (self , key : str , default : typing .Any = _UNSET ) -> typing .Any :
460
+ """
461
+ Same as calling D.get(k, d), and setting D[k]=d if k not found
462
+ :param str key: The key to look up.
463
+ :param any default: The value to set if key is not in the dictionary
464
+ :returns: D[k] if k in D, else d.
465
+ :rtype: any
466
+ """
420
467
if default is _UNSET :
421
468
return self ._data .setdefault (key )
422
469
return self ._data .setdefault (key , default )
@@ -574,7 +621,7 @@ def __init__(self, *args: typing.Any, **kwargs: typing.Any) -> None:
574
621
def copy (self ) -> "Model" :
575
622
return Model (self .__dict__ )
576
623
577
- def __new__ (cls , * args : typing .Any , ** kwargs : typing .Any ) -> Self : # pylint: disable=unused-argument
624
+ def __new__ (cls , * args : typing .Any , ** kwargs : typing .Any ) -> Self :
578
625
if f"{ cls .__module__ } .{ cls .__qualname__ } " not in cls ._calculated :
579
626
# we know the last nine classes in mro are going to be 'Model', '_MyMutableMapping', 'MutableMapping',
580
627
# 'Mapping', 'Collection', 'Sized', 'Iterable', 'Container' and 'object'
@@ -585,8 +632,8 @@ def __new__(cls, *args: typing.Any, **kwargs: typing.Any) -> Self: # pylint: di
585
632
annotations = {
586
633
k : v
587
634
for mro_class in mros
588
- if hasattr (mro_class , "__annotations__" ) # pylint: disable=no-member
589
- for k , v in mro_class .__annotations__ .items () # pylint: disable=no-member
635
+ if hasattr (mro_class , "__annotations__" )
636
+ for k , v in mro_class .__annotations__ .items ()
590
637
}
591
638
for attr , rf in attr_to_rest_field .items ():
592
639
rf ._module = cls .__module__
@@ -601,8 +648,8 @@ def __new__(cls, *args: typing.Any, **kwargs: typing.Any) -> Self: # pylint: di
601
648
602
649
def __init_subclass__ (cls , discriminator : typing .Optional [str ] = None ) -> None :
603
650
for base in cls .__bases__ :
604
- if hasattr (base , "__mapping__" ): # pylint: disable=no-member
605
- base .__mapping__ [discriminator or cls .__name__ ] = cls # type: ignore # pylint: disable=no-member
651
+ if hasattr (base , "__mapping__" ):
652
+ base .__mapping__ [discriminator or cls .__name__ ] = cls # type: ignore
606
653
607
654
@classmethod
608
655
def _get_discriminator (cls , exist_discriminators ) -> typing .Optional ["_RestField" ]:
@@ -613,7 +660,7 @@ def _get_discriminator(cls, exist_discriminators) -> typing.Optional["_RestField
613
660
614
661
@classmethod
615
662
def _deserialize (cls , data , exist_discriminators ):
616
- if not hasattr (cls , "__mapping__" ): # pylint: disable=no-member
663
+ if not hasattr (cls , "__mapping__" ):
617
664
return cls (data )
618
665
discriminator = cls ._get_discriminator (exist_discriminators )
619
666
if discriminator is None :
@@ -633,7 +680,7 @@ def _deserialize(cls, data, exist_discriminators):
633
680
discriminator_value = data .find (xml_name ).text # pyright: ignore
634
681
else :
635
682
discriminator_value = data .get (discriminator ._rest_name )
636
- mapped_cls = cls .__mapping__ .get (discriminator_value , cls ) # pyright: ignore # pylint: disable=no-member
683
+ mapped_cls = cls .__mapping__ .get (discriminator_value , cls ) # pyright: ignore
637
684
return mapped_cls ._deserialize (data , exist_discriminators )
638
685
639
686
def as_dict (self , * , exclude_readonly : bool = False ) -> typing .Dict [str , typing .Any ]:
@@ -754,7 +801,7 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=too-many-retur
754
801
except AttributeError :
755
802
model_name = annotation
756
803
if module is not None :
757
- annotation = _get_model (module , model_name )
804
+ annotation = _get_model (module , model_name ) # type: ignore
758
805
759
806
try :
760
807
if module and _is_model (annotation ):
@@ -894,6 +941,35 @@ def _deserialize(
894
941
return _deserialize_with_callable (deserializer , value )
895
942
896
943
944
+ def _failsafe_deserialize (
945
+ deserializer : typing .Any ,
946
+ value : typing .Any ,
947
+ module : typing .Optional [str ] = None ,
948
+ rf : typing .Optional ["_RestField" ] = None ,
949
+ format : typing .Optional [str ] = None ,
950
+ ) -> typing .Any :
951
+ try :
952
+ return _deserialize (deserializer , value , module , rf , format )
953
+ except DeserializationError :
954
+ _LOGGER .warning (
955
+ "Ran into a deserialization error. Ignoring since this is failsafe deserialization" , exc_info = True
956
+ )
957
+ return None
958
+
959
+
960
+ def _failsafe_deserialize_xml (
961
+ deserializer : typing .Any ,
962
+ value : typing .Any ,
963
+ ) -> typing .Any :
964
+ try :
965
+ return _deserialize_xml (deserializer , value )
966
+ except DeserializationError :
967
+ _LOGGER .warning (
968
+ "Ran into a deserialization error. Ignoring since this is failsafe deserialization" , exc_info = True
969
+ )
970
+ return None
971
+
972
+
897
973
class _RestField :
898
974
def __init__ (
899
975
self ,
0 commit comments