-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathmodel_anyof.mustache
More file actions
66 lines (53 loc) · 2.57 KB
/
model_anyof.mustache
File metadata and controls
66 lines (53 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from __future__ import annotations
{{#vendorExtensions.x-py-other-imports}}
{{{.}}}
{{/vendorExtensions.x-py-other-imports}}
{{#vendorExtensions.x-py-model-imports}}
{{{.}}}
{{/vendorExtensions.x-py-model-imports}}
from pydantic import Field, RootModel
from typing import Union, Any, List, Set, TYPE_CHECKING, Optional, Dict
from typing_extensions import Literal, Self
{{#lambda.uppercase}}{{{classname}}}{{/lambda.uppercase}}_ANY_OF_SCHEMAS = [{{#anyOf}}"{{.}}"{{^-last}}, {{/-last}}{{/anyOf}}]
class {{classname}}(RootModel[Union[{{#anyOf}}{{.}}{{^-last}}, {{/-last}}{{/anyOf}}]]):
"""
{{{description}}}{{^description}}{{{classname}}}{{/description}}
"""
root: Union[{{#anyOf}}{{.}}{{^-last}}, {{/-last}}{{/anyOf}}{{#isNullable}}, None{{/isNullable}}] = Field(
{{#isNullable}}None{{/isNullable}}{{^isNullable}}...{{/isNullable}}{{#discriminator}}{{/discriminator}}
)
def __getattr__(self, name):
"""
Delegate attribute access to the root model if the attribute
doesn't exist on the main class.
"""
if name in self.__dict__:
return super().__getattribute__(name)
root = self.__dict__.get('root')
if root is not None:
return getattr(root, name)
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")
@classmethod
def from_dict(cls, obj: Dict[str, Any]) -> Self:
"""Returns the object represented by the Dict"""
return cls.model_validate(obj, strict=True)
@classmethod
def from_json(cls, json_str: str) -> Self:
"""Returns the object represented by the json string"""
return cls.model_validate_json(json_str)
def to_dict(self, exclude_unset: bool = True) -> Dict[str, Any]:
"""Returns the dict representation of the actual instance"""
return self.model_dump(by_alias=True, exclude_unset=exclude_unset)
def to_json(self, exclude_unset: bool = True) -> str:
"""Returns the JSON representation of the actual instance"""
return json.dumps(self.model_dump(by_alias=True, exclude_unset=exclude_unset, mode="json"))
def to_str(self) -> str:
"""Returns the string representation of the model using alias"""
return pprint.pformat(self.to_dict(exclude_unset=False))
{{#vendorExtensions.x-py-postponed-model-imports.size}}
{{#vendorExtensions.x-py-postponed-model-imports}}
{{{.}}}
{{/vendorExtensions.x-py-postponed-model-imports}}
# TODO: Rewrite to not use raise_errors
{{classname}}.model_rebuild(raise_errors=False)
{{/vendorExtensions.x-py-postponed-model-imports.size}}