Skip to content

Commit f8e63fc

Browse files
api-clients-generation-pipeline[bot]therveci.datadog-api-spec
authored
Do type conversion all the time in Python (#757)
* Do type conversion all the time Don't conditionally do type conversion, which is useful to do basic casting (like into to float). * Add test * Regenerate client from commit 63335df of spec repo Co-authored-by: Thomas Hervé <[email protected]> Co-authored-by: api-clients-generation-pipeline[bot] <54105614+api-clients-generation-pipeline[bot]@users.noreply.github.com> Co-authored-by: ci.datadog-api-spec <[email protected]>
1 parent 12e0ab8 commit f8e63fc

File tree

5 files changed

+46
-65
lines changed

5 files changed

+46
-65
lines changed

.apigentools-info

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
"spec_versions": {
55
"v1": {
66
"apigentools_version": "1.5.1.dev2",
7-
"regenerated": "2021-12-23 17:02:34.770350",
8-
"spec_repo_commit": "7b8392b"
7+
"regenerated": "2022-01-03 13:20:02.788921",
8+
"spec_repo_commit": "63335df"
99
},
1010
"v2": {
1111
"apigentools_version": "1.5.1.dev2",
12-
"regenerated": "2021-12-23 17:02:35.234866",
13-
"spec_repo_commit": "7b8392b"
12+
"regenerated": "2022-01-03 13:20:03.271184",
13+
"spec_repo_commit": "63335df"
1414
}
1515
}
1616
}

.generator/templates/model_utils.mustache

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,9 +1408,6 @@ def validate_and_convert_types(input_value, required_types_mixed, path_to_item,
14081408
will be attempted.
14091409
configuration: (Configuration): the configuration class to use
14101410
when converting file_type items.
1411-
If passed, conversion will be attempted when possible
1412-
If not passed, no conversions will be attempted and
1413-
exceptions will be raised
14141411

14151412
Returns:
14161413
the correctly typed value
@@ -1424,30 +1421,25 @@ def validate_and_convert_types(input_value, required_types_mixed, path_to_item,
14241421
input_class_simple = get_simple_class(input_value)
14251422
valid_type = is_valid_type(input_class_simple, valid_classes)
14261423
if not valid_type:
1427-
if configuration:
1428-
# if input_value is not valid_type try to convert it
1429-
converted_instance = attempt_convert_item(
1430-
input_value,
1431-
valid_classes,
1432-
path_to_item,
1433-
configuration,
1434-
spec_property_naming,
1435-
key_type=False,
1436-
must_convert=True,
1437-
check_type=_check_type
1438-
)
1439-
return converted_instance
1440-
else:
1441-
raise get_type_error(input_value, path_to_item, valid_classes,
1442-
key_type=False)
1424+
# if input_value is not valid_type try to convert it
1425+
return attempt_convert_item(
1426+
input_value,
1427+
valid_classes,
1428+
path_to_item,
1429+
configuration,
1430+
spec_property_naming,
1431+
key_type=False,
1432+
must_convert=True,
1433+
check_type=_check_type
1434+
)
14431435

14441436
# input_value's type is in valid_classes
14451437
if len(valid_classes) > 1 and configuration:
14461438
# there are valid classes which are not the current class
14471439
valid_classes_coercible = remove_uncoercible(
14481440
valid_classes, input_value, spec_property_naming, must_convert=False)
14491441
if valid_classes_coercible:
1450-
converted_instance = attempt_convert_item(
1442+
return attempt_convert_item(
14511443
input_value,
14521444
valid_classes_coercible,
14531445
path_to_item,
@@ -1457,7 +1449,6 @@ def validate_and_convert_types(input_value, required_types_mixed, path_to_item,
14571449
must_convert=False,
14581450
check_type=_check_type
14591451
)
1460-
return converted_instance
14611452

14621453
if child_req_types_by_current_type == {}:
14631454
# all types are of the required types and there are no more inner

src/datadog_api_client/v1/model_utils.py

Lines changed: 12 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datadog_api_client/v2/model_utils.py

Lines changed: 12 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_serialization.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from datadog_api_client.v1.model.monitor_thresholds import MonitorThresholds
2+
3+
4+
def test_default_coerce():
5+
thresholds = MonitorThresholds(critical=5)
6+
assert thresholds["critical"] == 5.0

0 commit comments

Comments
 (0)