Skip to content

Commit 92d7470

Browse files
api-clients-generation-pipeline[bot]therveci.datadog-api-spec
authored
Refactor python model (#541)
* Refactor models This refactors models to reduce duplication. * Some more cleanups * Regenerate client from commit 7c3089e 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 3d232fc commit 92d7470

File tree

955 files changed

+9344
-129292
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

955 files changed

+9344
-129292
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.4.1.dev11",
7-
"regenerated": "2021-08-06 11:31:17.817652",
8-
"spec_repo_commit": "692cbbb"
7+
"regenerated": "2021-08-09 09:22:24.096659",
8+
"spec_repo_commit": "7c3089e"
99
},
1010
"v2": {
1111
"apigentools_version": "1.4.1.dev11",
12-
"regenerated": "2021-08-06 11:31:51.168390",
13-
"spec_repo_commit": "692cbbb"
12+
"regenerated": "2021-08-09 09:22:50.599634",
13+
"spec_repo_commit": "7c3089e"
1414
}
1515
}
1616
}

.generator/templates/model.mustache

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
{{> partial_header }}
22

3-
import re # noqa: F401
4-
import sys # noqa: F401
5-
63
from {{packageName}}.model_utils import ( # noqa: F401
74
ApiTypeError,
85
ModelComposed,
@@ -15,10 +12,7 @@ from {{packageName}}.model_utils import ( # noqa: F401
1512
datetime,
1613
file_type,
1714
none_type,
18-
validate_get_composed_info,
1915
)
20-
from ..model_utils import OpenApiModel
21-
from {{packageName}}.exceptions import ApiAttributeError
2216

2317
{{#models}}
2418
{{#model}}

.generator/templates/model_templates/classvars.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@
120120
}
121121
{{/oneOf}}
122122

123-
@cached_property
124-
def discriminator():
125123
{{^discriminator}}
126-
return None
124+
discriminator = None
127125
{{/discriminator}}
128126
{{#discriminator}}
127+
@cached_property
128+
def discriminator():
129129
{{#mappedModels}}
130130
{{#-first}}
131131
{{#imports}}
@@ -142,4 +142,4 @@
142142
}
143143
if not val:
144144
return None
145-
return {'{{{discriminatorName}}}': val}{{/discriminator}}
145+
return {'{{{discriminatorName}}}': val}{{/discriminator}}
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
if args:
2-
raise ApiTypeError(
3-
"Invalid positional arguments=%s passed to %s. Remove those invalid positional arguments." % (
4-
args,
5-
self.__class__.__name__,
6-
),
7-
path_to_item=_path_to_item,
8-
valid_classes=(self.__class__,),
9-
)
1+
self._check_pos_args(args)
Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,16 @@
11
@classmethod
22
@convert_js_args_to_python_args
33
def _from_openapi_data(cls, *args, **kwargs): # noqa: E501
4-
"""{{classname}} - a model defined in OpenAPI
5-
6-
Keyword Args:
7-
{{#requiredVars}}
8-
{{#defaultValue}}
9-
{{name}} ({{{dataType}}}):{{#description}} {{{description}}}.{{/description}} defaults to {{{defaultValue}}}{{#allowableValues}}, must be one of [{{#enumVars}}{{{value}}}, {{/enumVars}}]{{/allowableValues}} # noqa: E501
10-
{{/defaultValue}}
11-
{{^defaultValue}}
12-
{{name}} ({{{dataType}}}):{{#description}} {{{description}}}{{/description}}
13-
{{/defaultValue}}
14-
{{/requiredVars}}
15-
{{> model_templates/docstring_init_required_kwargs }}
16-
{{#optionalVars}}
17-
{{name}} ({{{dataType}}}):{{#description}} {{{description}}}.{{/description}} [optional]{{#defaultValue}} if omitted the server will use the default value of {{{defaultValue}}}{{/defaultValue}} # noqa: E501
18-
{{/optionalVars}}
19-
"""
4+
"""Helper creating a new instance from a response."""
205

216
{{#requiredVars}}
227
{{#defaultValue}}
238
{{name}} = kwargs.get('{{name}}', {{{defaultValue}}})
249
{{/defaultValue}}
2510
{{/requiredVars}}
26-
_check_type = kwargs.pop('_check_type', True)
27-
_spec_property_naming = kwargs.pop('_spec_property_naming', False)
28-
_path_to_item = kwargs.pop('_path_to_item', ())
29-
_configuration = kwargs.pop('_configuration', None)
30-
_visited_composed_classes = kwargs.pop('_visited_composed_classes', ())
3111

32-
self = super(OpenApiModel, cls).__new__(cls)
12+
self = super({{classname}}, cls)._from_openapi_data(kwargs)
3313

3414
{{> model_templates/invalid_pos_args }}
3515

36-
self._data_store = {}
37-
self._check_type = _check_type
38-
self._spec_property_naming = _spec_property_naming
39-
self._path_to_item = _path_to_item
40-
self._configuration = _configuration
41-
self._visited_composed_classes = _visited_composed_classes + (self.__class__,)
42-
43-
constant_args = {
44-
'_check_type': _check_type,
45-
'_path_to_item': _path_to_item,
46-
'_spec_property_naming': _spec_property_naming,
47-
'_configuration': _configuration,
48-
'_visited_composed_classes': self._visited_composed_classes,
49-
}
50-
composed_info = validate_get_composed_info(
51-
constant_args, kwargs, self)
52-
self._composed_instances = composed_info[0]
53-
self._var_name_to_model_instances = composed_info[1]
54-
self._additional_properties_model_instances = composed_info[2]
55-
discarded_args = composed_info[3]
56-
57-
return self
16+
return self

.generator/templates/model_templates/method_from_openapi_data_normal.mustache

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,4 @@
66
{{#requiredVars}}
77
self.{{name}} = {{name}}
88
{{/requiredVars}}
9-
for var_name, var_value in kwargs.items():
10-
if var_name not in self.attribute_map and \
11-
self._configuration is not None and \
12-
self._configuration.discard_unknown_keys and \
13-
self.additional_properties_type is None:
14-
# discard variable.
15-
continue
16-
setattr(self, var_name, var_value)
17-
return self
9+
return self
Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,13 @@
11
@classmethod
22
@convert_js_args_to_python_args
33
def _from_openapi_data(cls{{#requiredVars}}{{^defaultValue}}, {{name}}{{/defaultValue}}{{/requiredVars}}, *args, **kwargs): # noqa: E501
4-
"""{{classname}} - a model defined in OpenAPI
5-
6-
{{#requiredVars}}
7-
{{#-first}}
8-
Args:
9-
{{/-first}}
10-
{{^defaultValue}}
11-
{{name}} ({{{dataType}}}):{{#description}} {{{description}}}{{/description}}
12-
{{/defaultValue}}
13-
{{#-last}}
14-
15-
{{/-last}}
16-
{{/requiredVars}}
17-
Keyword Args:
18-
{{#requiredVars}}
19-
{{#defaultValue}}
20-
{{name}} ({{{dataType}}}):{{#description}} {{{description}}}.{{/description}} defaults to {{{defaultValue}}}{{#allowableValues}}, must be one of [{{#enumVars}}{{{value}}}, {{/enumVars}}]{{/allowableValues}} # noqa: E501
21-
{{/defaultValue}}
22-
{{/requiredVars}}
23-
{{> model_templates/docstring_init_required_kwargs }}
24-
{{#optionalVars}}
25-
{{name}} ({{{dataType}}}):{{#description}} {{{description}}}.{{/description}} [optional]{{#defaultValue}} if omitted the server will use the default value of {{{defaultValue}}}{{/defaultValue}} # noqa: E501
26-
{{/optionalVars}}
27-
"""
28-
4+
"""Helper creating a new instance from a response."""
295
{{#requiredVars}}
306
{{#defaultValue}}
317
{{name}} = kwargs.get('{{name}}', {{{defaultValue}}})
328
{{/defaultValue}}
339
{{/requiredVars}}
34-
_check_type = kwargs.pop('_check_type', True)
35-
_spec_property_naming = kwargs.pop('_spec_property_naming', False)
36-
_path_to_item = kwargs.pop('_path_to_item', ())
37-
_configuration = kwargs.pop('_configuration', None)
38-
_visited_composed_classes = kwargs.pop('_visited_composed_classes', ())
3910

40-
self = super(OpenApiModel, cls).__new__(cls)
11+
self = super({{classname}}, cls)._from_openapi_data(kwargs)
4112

4213
{{> model_templates/invalid_pos_args }}
43-
44-
self._data_store = {}
45-
self._check_type = _check_type
46-
self._spec_property_naming = _spec_property_naming
47-
self._path_to_item = _path_to_item
48-
self._configuration = _configuration
49-
self._visited_composed_classes = _visited_composed_classes + (self.__class__,)
Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,5 @@
11
@classmethod
22
@convert_js_args_to_python_args
33
def _from_openapi_data(cls, *args, **kwargs):
4-
"""{{classname}} - a model defined in OpenAPI
5-
6-
Note that value can be passed either in args or in kwargs, but not in both.
7-
8-
Args:
9-
args[0] ({{{dataType}}}):{{#description}} {{{description}}}.{{/description}}{{#defaultValue}} if omitted defaults to {{{defaultValue}}}{{/defaultValue}}{{#allowableValues}}, must be one of [{{#enumVars}}{{{value}}}, {{/enumVars}}]{{/allowableValues}} # noqa: E501
10-
11-
Keyword Args:
12-
value ({{{dataType}}}):{{#description}} {{{description}}}.{{/description}}{{#defaultValue}} if omitted defaults to {{{defaultValue}}}{{/defaultValue}}{{#allowableValues}}, must be one of [{{#enumVars}}{{{value}}}, {{/enumVars}}]{{/allowableValues}} # noqa: E501
13-
{{> model_templates/docstring_init_required_kwargs }}
14-
"""
15-
# required up here when default value is not given
16-
_path_to_item = kwargs.pop('_path_to_item', ())
17-
18-
self = super(OpenApiModel, cls).__new__(cls)
19-
20-
if 'value' in kwargs:
21-
value = kwargs.pop('value')
22-
elif args:
23-
args = list(args)
24-
value = args.pop(0)
25-
{{#defaultValue}}
26-
else:
27-
value = {{{defaultValue}}}
28-
{{/defaultValue}}
29-
{{^defaultValue}}
30-
else:
31-
raise ApiTypeError(
32-
"value is required, but not passed in args or kwargs and doesn't have default",
33-
path_to_item=_path_to_item,
34-
valid_classes=(self.__class__,),
35-
)
36-
{{/defaultValue}}
37-
38-
_check_type = kwargs.pop('_check_type', True)
39-
_spec_property_naming = kwargs.pop('_spec_property_naming', False)
40-
_configuration = kwargs.pop('_configuration', None)
41-
_visited_composed_classes = kwargs.pop('_visited_composed_classes', ())
42-
43-
{{> model_templates/invalid_pos_args }}
44-
45-
self._data_store = {}
46-
self._check_type = _check_type
47-
self._spec_property_naming = _spec_property_naming
48-
self._path_to_item = _path_to_item
49-
self._configuration = _configuration
50-
self._visited_composed_classes = _visited_composed_classes + (self.__class__,)
51-
self.value = value
52-
if kwargs:
53-
raise ApiTypeError(
54-
"Invalid named arguments=%s passed to %s. Remove those invalid named arguments." % (
55-
kwargs,
56-
self.__class__.__name__,
57-
),
58-
path_to_item=_path_to_item,
59-
valid_classes=(self.__class__,),
60-
)
61-
62-
return self
4+
"""Helper creating a new instance from a response."""
5+
return cls(*args, **kwargs)
Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
required_properties = set([
2-
'_data_store',
3-
'_check_type',
4-
'_spec_property_naming',
5-
'_path_to_item',
6-
'_configuration',
7-
'_visited_composed_classes',
8-
'_composed_instances',
9-
'_var_name_to_model_instances',
10-
'_additional_properties_model_instances',
11-
])
12-
131
@convert_js_args_to_python_args
142
def __init__(self, *args, **kwargs): # noqa: E501
153
"""{{classname}} - a model defined in OpenAPI
@@ -30,6 +18,7 @@
3018
{{name}} ({{{dataType}}}):{{#description}} {{{description}}}.{{/description}} [optional]{{#defaultValue}} if omitted the server will use the default value of {{{defaultValue}}}{{/defaultValue}} # noqa: E501
3119
{{/optionalVars}}
3220
"""
21+
super().__init__(kwargs)
3322

3423
{{#requiredVars}}
3524
{{^isReadOnly}}
@@ -38,31 +27,5 @@
3827
{{/defaultValue}}
3928
{{/isReadOnly}}
4029
{{/requiredVars}}
41-
_check_type = kwargs.pop('_check_type', True)
42-
_spec_property_naming = kwargs.pop('_spec_property_naming', False)
43-
_path_to_item = kwargs.pop('_path_to_item', ())
44-
_configuration = kwargs.pop('_configuration', None)
45-
_visited_composed_classes = kwargs.pop('_visited_composed_classes', ())
4630

4731
{{> model_templates/invalid_pos_args }}
48-
49-
self._data_store = {}
50-
self._check_type = _check_type
51-
self._spec_property_naming = _spec_property_naming
52-
self._path_to_item = _path_to_item
53-
self._configuration = _configuration
54-
self._visited_composed_classes = _visited_composed_classes + (self.__class__,)
55-
56-
constant_args = {
57-
'_check_type': _check_type,
58-
'_path_to_item': _path_to_item,
59-
'_spec_property_naming': _spec_property_naming,
60-
'_configuration': _configuration,
61-
'_visited_composed_classes': self._visited_composed_classes,
62-
}
63-
composed_info = validate_get_composed_info(
64-
constant_args, kwargs, self)
65-
self._composed_instances = composed_info[0]
66-
self._var_name_to_model_instances = composed_info[1]
67-
self._additional_properties_model_instances = composed_info[2]
68-
discarded_args = composed_info[3]
Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
required_properties = set([
2-
'_data_store',
3-
'_check_type',
4-
'_spec_property_naming',
5-
'_path_to_item',
6-
'_configuration',
7-
'_visited_composed_classes',
8-
])
9-
101
{{> model_templates/method_init_shared }}
112

123
{{#isEnum}}
@@ -17,14 +8,3 @@
178
self.{{name}} = {{name}}
189
{{/isReadOnly}}
1910
{{/requiredVars}}
20-
for var_name, var_value in kwargs.items():
21-
if var_name not in self.attribute_map and \
22-
self._configuration is not None and \
23-
self._configuration.discard_unknown_keys and \
24-
self.additional_properties_type is None:
25-
# discard variable.
26-
continue
27-
setattr(self, var_name, var_value)
28-
if var_name in self.read_only_vars:
29-
raise ApiAttributeError(f"`{var_name}` is a read-only attribute. Use `from_openapi_data` to instantiate "
30-
f"class with read only attributes.")

0 commit comments

Comments
 (0)