@autorest/python 6.9.9 → 6.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,6 +29,7 @@ from .primitive_types import (
29
29
  AnyObjectType,
30
30
  UnixTimeType,
31
31
  SdkCoreType,
32
+ DecimalType,
32
33
  )
33
34
  from .enum_type import EnumType, EnumValue
34
35
  from .base import BaseType
@@ -124,6 +125,7 @@ __all__ = [
124
125
  TYPE_TO_OBJECT = {
125
126
  "integer": IntegerType,
126
127
  "float": FloatType,
128
+ "decimal": DecimalType,
127
129
  "string": StringType,
128
130
  "list": ListType,
129
131
  "dict": DictionaryType,
@@ -4,6 +4,7 @@
4
4
  # license information.
5
5
  # --------------------------------------------------------------------------
6
6
  import datetime
7
+ import decimal
7
8
  from typing import Any, Dict, List, Optional, Union, TYPE_CHECKING
8
9
 
9
10
  from .base import BaseType
@@ -305,6 +306,37 @@ class FloatType(NumberType):
305
306
  return "isinstance({}, float)"
306
307
 
307
308
 
309
+ class DecimalType(NumberType):
310
+ @property
311
+ def serialization_type(self) -> str:
312
+ return "decimal"
313
+
314
+ def docstring_type(self, **kwargs: Any) -> str:
315
+ return "~" + self.type_annotation()
316
+
317
+ def type_annotation(self, **kwargs: Any) -> str:
318
+ return "decimal.Decimal"
319
+
320
+ def docstring_text(self, **kwargs: Any) -> str:
321
+ return self.type_annotation()
322
+
323
+ def get_declaration(self, value: decimal.Decimal) -> str:
324
+ return str(value)
325
+
326
+ def imports(self, **kwargs: Any) -> FileImport:
327
+ file_import = FileImport(self.code_model)
328
+ file_import.add_import("decimal", ImportType.STDLIB)
329
+ return file_import
330
+
331
+ @property
332
+ def default_template_representation_declaration(self) -> str:
333
+ return self.get_declaration(decimal.Decimal("0.0"))
334
+
335
+ @property
336
+ def instance_check_template(self) -> str:
337
+ return "isinstance({}, decimal.Decimal)"
338
+
339
+
308
340
  class StringType(PrimitiveType):
309
341
  def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
310
342
  super().__init__(yaml_data=yaml_data, code_model=code_model)
@@ -8,6 +8,7 @@
8
8
  # pyright: reportGeneralTypeIssues=false
9
9
 
10
10
  import calendar
11
+ import decimal
11
12
  import functools
12
13
  import sys
13
14
  import logging
@@ -144,6 +145,8 @@ class SdkJSONEncoder(JSONEncoder):
144
145
  except TypeError:
145
146
  if isinstance(o, _Null):
146
147
  return None
148
+ if isinstance(o, decimal.Decimal):
149
+ return float(o)
147
150
  if isinstance(o, (bytes, bytearray)):
148
151
  return _serialize_bytes(o, self.format)
149
152
  try:
@@ -275,6 +278,12 @@ def _deserialize_duration(attr):
275
278
  return isodate.parse_duration(attr)
276
279
 
277
280
 
281
+ def _deserialize_decimal(attr):
282
+ if isinstance(attr, decimal.Decimal):
283
+ return attr
284
+ return decimal.Decimal(str(attr))
285
+
286
+
278
287
  _DESERIALIZE_MAPPING = {
279
288
  datetime: _deserialize_datetime,
280
289
  date: _deserialize_date,
@@ -283,6 +292,7 @@ _DESERIALIZE_MAPPING = {
283
292
  bytearray: _deserialize_bytes,
284
293
  timedelta: _deserialize_duration,
285
294
  typing.Any: lambda x: x,
295
+ decimal.Decimal: _deserialize_decimal,
286
296
  }
287
297
 
288
298
  _DESERIALIZE_MAPPING_WITHFORMAT = {
@@ -434,6 +444,8 @@ def _serialize(o, format: typing.Optional[str] = None): # pylint: disable=too-m
434
444
  return tuple(_serialize(x, format) for x in o)
435
445
  if isinstance(o, (bytes, bytearray)):
436
446
  return _serialize_bytes(o, format)
447
+ if isinstance(o, decimal.Decimal):
448
+ return float(o)
437
449
  try:
438
450
  # First try datetime.datetime
439
451
  return _serialize_datetime(o, format)
@@ -656,24 +668,21 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915,
656
668
 
657
669
  try:
658
670
  if annotation._name == "Dict":
659
- key_deserializer = _get_deserialize_callable_from_annotation(annotation.__args__[0], module, rf)
660
671
  value_deserializer = _get_deserialize_callable_from_annotation(annotation.__args__[1], module, rf)
661
672
 
662
673
  def _deserialize_dict(
663
- key_deserializer: typing.Optional[typing.Callable],
664
674
  value_deserializer: typing.Optional[typing.Callable],
665
675
  obj: typing.Dict[typing.Any, typing.Any],
666
676
  ):
667
677
  if obj is None:
668
678
  return obj
669
679
  return {
670
- _deserialize(key_deserializer, k, module): _deserialize(value_deserializer, v, module)
680
+ k: _deserialize(value_deserializer, v, module)
671
681
  for k, v in obj.items()
672
682
  }
673
683
 
674
684
  return functools.partial(
675
685
  _deserialize_dict,
676
- key_deserializer,
677
686
  value_deserializer,
678
687
  )
679
688
  except (AttributeError, IndexError):
@@ -712,19 +721,17 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=R0911, R0915,
712
721
  pass
713
722
 
714
723
  def _deserialize_default(
715
- annotation,
716
- deserializer_from_mapping,
724
+ deserializer,
717
725
  obj,
718
726
  ):
719
727
  if obj is None:
720
728
  return obj
721
- try:
722
- return _deserialize_with_callable(annotation, obj)
723
- except Exception:
724
- pass
725
- return _deserialize_with_callable(deserializer_from_mapping, obj)
729
+ return _deserialize_with_callable(deserializer, obj)
730
+
731
+ if get_deserializer(annotation, rf):
732
+ return functools.partial(_deserialize_default, get_deserializer(annotation, rf))
726
733
 
727
- return functools.partial(_deserialize_default, annotation, get_deserializer(annotation, rf))
734
+ return functools.partial(_deserialize_default, annotation)
728
735
 
729
736
 
730
737
  def _deserialize_with_callable(
@@ -732,7 +739,7 @@ def _deserialize_with_callable(
732
739
  value: typing.Any,
733
740
  ):
734
741
  try:
735
- if value is None:
742
+ if value is None or isinstance(value, _Null):
736
743
  return None
737
744
  if deserializer is None:
738
745
  return value
@@ -760,7 +767,8 @@ def _deserialize(
760
767
  value = value.http_response.json()
761
768
  if rf is None and format:
762
769
  rf = _RestField(format=format)
763
- deserializer = _get_deserialize_callable_from_annotation(deserializer, module, rf)
770
+ if not isinstance(deserializer, functools.partial):
771
+ deserializer = _get_deserialize_callable_from_annotation(deserializer, module, rf)
764
772
  return _deserialize_with_callable(deserializer, value)
765
773
 
766
774
 
@@ -747,7 +747,7 @@ class Serializer(object):
747
747
  :param str data_type: The type to be serialized from.
748
748
  :keyword bool skip_quote: Whether to skip quote the serialized result.
749
749
  Defaults to False.
750
- :rtype: str
750
+ :rtype: str, list
751
751
  :raises: TypeError if serialization fails.
752
752
  :raises: ValueError if data is None
753
753
  """
@@ -1866,7 +1866,7 @@ class Deserializer(object):
1866
1866
  if isinstance(attr, ET.Element):
1867
1867
  attr = attr.text
1868
1868
  try:
1869
- return decimal.Decimal(attr) # type: ignore
1869
+ return decimal.Decimal(str(attr)) # type: ignore
1870
1870
  except decimal.DecimalException as err:
1871
1871
  msg = "Invalid decimal {}".format(attr)
1872
1872
  raise DeserializationError(msg) from err
@@ -2002,6 +2002,7 @@ class Deserializer(object):
2002
2002
  if isinstance(attr, ET.Element):
2003
2003
  attr = int(attr.text) # type: ignore
2004
2004
  try:
2005
+ attr = int(attr)
2005
2006
  date_obj = datetime.datetime.fromtimestamp(attr, TZ_UTC)
2006
2007
  except ValueError as err:
2007
2008
  msg = "Cannot deserialize to unix datetime object."
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autorest/python",
3
- "version": "6.9.9",
3
+ "version": "6.10.0",
4
4
  "description": "The Python extension for generators in AutoRest.",
5
5
  "main": "index.js",
6
6
  "repository": {
package/requirements.txt CHANGED
@@ -8,5 +8,5 @@ MarkupSafe==2.1.2
8
8
  mistune==0.8.4
9
9
  pathspec==0.11.1
10
10
  platformdirs==3.2.0
11
- PyYAML==6.0
11
+ PyYAML==6.0.1
12
12
  tomli==2.0.1