Skip to content

Commit 701ea77

Browse files
authoredJan 12, 2021
feat(models): Change model from_dict from staticmethod to classmethod (#292)
1 parent f49038d commit 701ea77

31 files changed

+188
-131
lines changed
 

‎end_to_end_tests/golden-record-custom/custom_e2e/models/a_model.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import datetime
2-
from typing import Any, Dict, List, Optional, Union, cast
2+
from typing import Any, Dict, List, Optional, Type, TypeVar, Union, cast
33

44
import attr
55
from dateutil.parser import isoparse
@@ -8,6 +8,8 @@
88
from ..models.different_enum import DifferentEnum
99
from ..types import UNSET, Unset
1010

11+
T = TypeVar("T", bound="AModel")
12+
1113

1214
@attr.s(auto_attribs=True)
1315
class AModel:
@@ -75,8 +77,8 @@ def to_dict(self) -> Dict[str, Any]:
7577

7678
return field_dict
7779

78-
@staticmethod
79-
def from_dict(src_dict: Dict[str, Any]) -> "AModel":
80+
@classmethod
81+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
8082
d = src_dict.copy()
8183
an_enum_value = AnEnum(d.pop("an_enum_value"))
8284

@@ -124,7 +126,7 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat
124126

125127
not_required_not_nullable = d.pop("not_required_not_nullable", UNSET)
126128

127-
a_model = AModel(
129+
a_model = cls(
128130
an_enum_value=an_enum_value,
129131
a_camel_date_time=a_camel_date_time,
130132
a_date=a_date,

‎end_to_end_tests/golden-record-custom/custom_e2e/models/body_upload_file_tests_upload_post.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from io import BytesIO
2-
from typing import Any, Dict
2+
from typing import Any, Dict, Type, TypeVar
33

44
import attr
55

66
from ..types import File
77

8+
T = TypeVar("T", bound="BodyUploadFileTestsUploadPost")
9+
810

911
@attr.s(auto_attribs=True)
1012
class BodyUploadFileTestsUploadPost:
@@ -24,12 +26,12 @@ def to_dict(self) -> Dict[str, Any]:
2426

2527
return field_dict
2628

27-
@staticmethod
28-
def from_dict(src_dict: Dict[str, Any]) -> "BodyUploadFileTestsUploadPost":
29+
@classmethod
30+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
2931
d = src_dict.copy()
3032
some_file = File(payload=BytesIO(d.pop("some_file")))
3133

32-
body_upload_file_tests_upload_post = BodyUploadFileTestsUploadPost(
34+
body_upload_file_tests_upload_post = cls(
3335
some_file=some_file,
3436
)
3537

‎end_to_end_tests/golden-record-custom/custom_e2e/models/free_form_model.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from typing import Any, Dict, List
1+
from typing import Any, Dict, List, Type, TypeVar
22

33
import attr
44

5+
T = TypeVar("T", bound="FreeFormModel")
6+
57

68
@attr.s(auto_attribs=True)
79
class FreeFormModel:
@@ -17,10 +19,10 @@ def to_dict(self) -> Dict[str, Any]:
1719

1820
return field_dict
1921

20-
@staticmethod
21-
def from_dict(src_dict: Dict[str, Any]) -> "FreeFormModel":
22+
@classmethod
23+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
2224
d = src_dict.copy()
23-
free_form_model = FreeFormModel()
25+
free_form_model = cls()
2426

2527
free_form_model.additional_properties = d
2628
return free_form_model

‎end_to_end_tests/golden-record-custom/custom_e2e/models/http_validation_error.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
from typing import Any, Dict, List, Union
1+
from typing import Any, Dict, List, Type, TypeVar, Union
22

33
import attr
44

55
from ..models.validation_error import ValidationError
66
from ..types import UNSET, Unset
77

8+
T = TypeVar("T", bound="HTTPValidationError")
9+
810

911
@attr.s(auto_attribs=True)
1012
class HTTPValidationError:
@@ -28,8 +30,8 @@ def to_dict(self) -> Dict[str, Any]:
2830

2931
return field_dict
3032

31-
@staticmethod
32-
def from_dict(src_dict: Dict[str, Any]) -> "HTTPValidationError":
33+
@classmethod
34+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
3335
d = src_dict.copy()
3436
detail = []
3537
_detail = d.pop("detail", UNSET)
@@ -38,7 +40,7 @@ def from_dict(src_dict: Dict[str, Any]) -> "HTTPValidationError":
3840

3941
detail.append(detail_item)
4042

41-
http_validation_error = HTTPValidationError(
43+
http_validation_error = cls(
4244
detail=detail,
4345
)
4446

‎end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_additional_properties_inlined.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, List, Union
1+
from typing import Any, Dict, List, Type, TypeVar, Union
22

33
import attr
44

@@ -7,6 +7,8 @@
77
)
88
from ..types import UNSET, Unset
99

10+
T = TypeVar("T", bound="ModelWithAdditionalPropertiesInlined")
11+
1012

1113
@attr.s(auto_attribs=True)
1214
class ModelWithAdditionalPropertiesInlined:
@@ -30,12 +32,12 @@ def to_dict(self) -> Dict[str, Any]:
3032

3133
return field_dict
3234

33-
@staticmethod
34-
def from_dict(src_dict: Dict[str, Any]) -> "ModelWithAdditionalPropertiesInlined":
35+
@classmethod
36+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
3537
d = src_dict.copy()
3638
a_number = d.pop("a_number", UNSET)
3739

38-
model_with_additional_properties_inlined = ModelWithAdditionalPropertiesInlined(
40+
model_with_additional_properties_inlined = cls(
3941
a_number=a_number,
4042
)
4143

‎end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_additional_properties_inlined_additional_property.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from typing import Any, Dict, List, Union
1+
from typing import Any, Dict, List, Type, TypeVar, Union
22

33
import attr
44

55
from ..types import UNSET, Unset
66

7+
T = TypeVar("T", bound="ModelWithAdditionalPropertiesInlinedAdditionalProperty")
8+
79

810
@attr.s(auto_attribs=True)
911
class ModelWithAdditionalPropertiesInlinedAdditionalProperty:
@@ -23,15 +25,13 @@ def to_dict(self) -> Dict[str, Any]:
2325

2426
return field_dict
2527

26-
@staticmethod
27-
def from_dict(src_dict: Dict[str, Any]) -> "ModelWithAdditionalPropertiesInlinedAdditionalProperty":
28+
@classmethod
29+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
2830
d = src_dict.copy()
2931
extra_props_prop = d.pop("extra_props_prop", UNSET)
3032

31-
model_with_additional_properties_inlined_additional_property = (
32-
ModelWithAdditionalPropertiesInlinedAdditionalProperty(
33-
extra_props_prop=extra_props_prop,
34-
)
33+
model_with_additional_properties_inlined_additional_property = cls(
34+
extra_props_prop=extra_props_prop,
3535
)
3636

3737
model_with_additional_properties_inlined_additional_property.additional_properties = d

‎end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_additional_properties_refed.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from typing import Any, Dict, List
1+
from typing import Any, Dict, List, Type, TypeVar
22

33
import attr
44

55
from ..models.an_enum import AnEnum
66

7+
T = TypeVar("T", bound="ModelWithAdditionalPropertiesRefed")
8+
79

810
@attr.s(auto_attribs=True)
911
class ModelWithAdditionalPropertiesRefed:
@@ -21,10 +23,10 @@ def to_dict(self) -> Dict[str, Any]:
2123

2224
return field_dict
2325

24-
@staticmethod
25-
def from_dict(src_dict: Dict[str, Any]) -> "ModelWithAdditionalPropertiesRefed":
26+
@classmethod
27+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
2628
d = src_dict.copy()
27-
model_with_additional_properties_refed = ModelWithAdditionalPropertiesRefed()
29+
model_with_additional_properties_refed = cls()
2830

2931
additional_properties = {}
3032
for prop_name, prop_dict in d.items():

‎end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_any_json_properties.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
from typing import Any, Dict, List, Union, cast
1+
from typing import Any, Dict, List, Type, TypeVar, Union, cast
22

33
import attr
44

55
from ..models.model_with_any_json_properties_additional_property import ModelWithAnyJsonPropertiesAdditionalProperty
66
from ..types import Unset
77

8+
T = TypeVar("T", bound="ModelWithAnyJsonProperties")
9+
810

911
@attr.s(auto_attribs=True)
1012
class ModelWithAnyJsonProperties:
@@ -31,10 +33,10 @@ def to_dict(self) -> Dict[str, Any]:
3133

3234
return field_dict
3335

34-
@staticmethod
35-
def from_dict(src_dict: Dict[str, Any]) -> "ModelWithAnyJsonProperties":
36+
@classmethod
37+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
3638
d = src_dict.copy()
37-
model_with_any_json_properties = ModelWithAnyJsonProperties()
39+
model_with_any_json_properties = cls()
3840

3941
additional_properties = {}
4042
for prop_name, prop_dict in d.items():

‎end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_any_json_properties_additional_property.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from typing import Any, Dict, List
1+
from typing import Any, Dict, List, Type, TypeVar
22

33
import attr
44

5+
T = TypeVar("T", bound="ModelWithAnyJsonPropertiesAdditionalProperty")
6+
57

68
@attr.s(auto_attribs=True)
79
class ModelWithAnyJsonPropertiesAdditionalProperty:
@@ -17,10 +19,10 @@ def to_dict(self) -> Dict[str, Any]:
1719

1820
return field_dict
1921

20-
@staticmethod
21-
def from_dict(src_dict: Dict[str, Any]) -> "ModelWithAnyJsonPropertiesAdditionalProperty":
22+
@classmethod
23+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
2224
d = src_dict.copy()
23-
model_with_any_json_properties_additional_property = ModelWithAnyJsonPropertiesAdditionalProperty()
25+
model_with_any_json_properties_additional_property = cls()
2426

2527
model_with_any_json_properties_additional_property.additional_properties = d
2628
return model_with_any_json_properties_additional_property

‎end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, List, Union, cast
1+
from typing import Any, Dict, List, Type, TypeVar, Union, cast
22

33
import attr
44

@@ -7,6 +7,8 @@
77
)
88
from ..types import UNSET, Unset
99

10+
T = TypeVar("T", bound="ModelWithPrimitiveAdditionalProperties")
11+
1012

1113
@attr.s(auto_attribs=True)
1214
class ModelWithPrimitiveAdditionalProperties:
@@ -28,8 +30,8 @@ def to_dict(self) -> Dict[str, Any]:
2830

2931
return field_dict
3032

31-
@staticmethod
32-
def from_dict(src_dict: Dict[str, Any]) -> "ModelWithPrimitiveAdditionalProperties":
33+
@classmethod
34+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
3335
d = src_dict.copy()
3436
a_date_holder: Union[ModelWithPrimitiveAdditionalPropertiesADateHolder, Unset] = UNSET
3537
_a_date_holder = d.pop("a_date_holder", UNSET)
@@ -38,7 +40,7 @@ def from_dict(src_dict: Dict[str, Any]) -> "ModelWithPrimitiveAdditionalProperti
3840
cast(Dict[str, Any], _a_date_holder)
3941
)
4042

41-
model_with_primitive_additional_properties = ModelWithPrimitiveAdditionalProperties(
43+
model_with_primitive_additional_properties = cls(
4244
a_date_holder=a_date_holder,
4345
)
4446

‎end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_primitive_additional_properties_a_date_holder.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import datetime
2-
from typing import Any, Dict, List
2+
from typing import Any, Dict, List, Type, TypeVar
33

44
import attr
55
from dateutil.parser import isoparse
66

7+
T = TypeVar("T", bound="ModelWithPrimitiveAdditionalPropertiesADateHolder")
8+
79

810
@attr.s(auto_attribs=True)
911
class ModelWithPrimitiveAdditionalPropertiesADateHolder:
@@ -21,10 +23,10 @@ def to_dict(self) -> Dict[str, Any]:
2123

2224
return field_dict
2325

24-
@staticmethod
25-
def from_dict(src_dict: Dict[str, Any]) -> "ModelWithPrimitiveAdditionalPropertiesADateHolder":
26+
@classmethod
27+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
2628
d = src_dict.copy()
27-
model_with_primitive_additional_properties_a_date_holder = ModelWithPrimitiveAdditionalPropertiesADateHolder()
29+
model_with_primitive_additional_properties_a_date_holder = cls()
2830

2931
additional_properties = {}
3032
for prop_name, prop_dict in d.items():

‎end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
from typing import Any, Dict, Union
1+
from typing import Any, Dict, Type, TypeVar, Union
22

33
import attr
44

55
from ..models.an_enum import AnEnum
66
from ..models.an_int_enum import AnIntEnum
77
from ..types import UNSET, Unset
88

9+
T = TypeVar("T", bound="ModelWithUnionProperty")
10+
911

1012
@attr.s(auto_attribs=True)
1113
class ModelWithUnionProperty:
@@ -34,8 +36,8 @@ def to_dict(self) -> Dict[str, Any]:
3436

3537
return field_dict
3638

37-
@staticmethod
38-
def from_dict(src_dict: Dict[str, Any]) -> "ModelWithUnionProperty":
39+
@classmethod
40+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
3941
d = src_dict.copy()
4042

4143
def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
@@ -59,7 +61,7 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]:
5961

6062
a_property = _parse_a_property(d.pop("a_property", UNSET))
6163

62-
model_with_union_property = ModelWithUnionProperty(
64+
model_with_union_property = cls(
6365
a_property=a_property,
6466
)
6567

‎end_to_end_tests/golden-record-custom/custom_e2e/models/test_inline_objects_json_body.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from typing import Any, Dict, Union
1+
from typing import Any, Dict, Type, TypeVar, Union
22

33
import attr
44

55
from ..types import UNSET, Unset
66

7+
T = TypeVar("T", bound="TestInlineObjectsJsonBody")
8+
79

810
@attr.s(auto_attribs=True)
911
class TestInlineObjectsJsonBody:
@@ -21,12 +23,12 @@ def to_dict(self) -> Dict[str, Any]:
2123

2224
return field_dict
2325

24-
@staticmethod
25-
def from_dict(src_dict: Dict[str, Any]) -> "TestInlineObjectsJsonBody":
26+
@classmethod
27+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
2628
d = src_dict.copy()
2729
a_property = d.pop("a_property", UNSET)
2830

29-
test_inline_objects_json_body = TestInlineObjectsJsonBody(
31+
test_inline_objects_json_body = cls(
3032
a_property=a_property,
3133
)
3234

0 commit comments

Comments
 (0)
Please sign in to comment.