You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Delegate attribute access to the root model if the attribute
29
+
doesn't exist on the main class.
30
+
"""
45
31
46
-
def __init__(self, *args, **kwargs) -> None:
47
-
if args:
48
-
if len(args) > 1:
49
-
raise ValueError("If a position argument is used, only 1 is allowed to set `actual_instance`")
50
-
if kwargs:
51
-
raise ValueError("If a position argument is used, keyword arguments cannot be used.")
52
-
super().__init__(actual_instance=args[0])
53
-
else:
54
-
super().__init__(**kwargs)
32
+
if name in self.__dict__:
33
+
return super().__getattribute__(name)
55
34
56
-
@field_validator('actual_instance')
57
-
def actual_instance_must_validate_anyof(cls, v):
58
-
{{#isNullable}}
59
-
if v is None:
60
-
return v
35
+
root = self.__dict__.get('root')
36
+
if root is not None:
37
+
return getattr(root, name)
61
38
62
-
{{/isNullable}}
63
-
instance = {{{classname}}}.model_construct()
64
-
error_messages = []
65
-
{{#composedSchemas.anyOf}}
66
-
# validate data type: {{{dataType}}}
67
-
{{#isContainer}}
68
-
try:
69
-
instance.{{vendorExtensions.x-py-name}} = v
70
-
return v
71
-
except (ValidationError, ValueError) as e:
72
-
error_messages.append(str(e))
73
-
{{/isContainer}}
74
-
{{^isContainer}}
75
-
{{#isPrimitiveType}}
76
-
try:
77
-
instance.{{vendorExtensions.x-py-name}} = v
78
-
return v
79
-
except (ValidationError, ValueError) as e:
80
-
error_messages.append(str(e))
81
-
{{/isPrimitiveType}}
82
-
{{^isPrimitiveType}}
83
-
if not isinstance(v, {{{dataType}}}):
84
-
error_messages.append(f"Error! Input type `{type(v)}` is not `{{{dataType}}}`")
85
-
else:
86
-
return v
87
-
88
-
{{/isPrimitiveType}}
89
-
{{/isContainer}}
90
-
{{/composedSchemas.anyOf}}
91
-
if error_messages:
92
-
# no match
93
-
raise ValueError("No match found when setting the actual_instance in {{{classname}}} with anyOf schemas: {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. Details: " + ", ".join(error_messages))
94
-
else:
95
-
return v
39
+
raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")
96
40
97
41
@classmethod
98
42
def from_dict(cls, obj: Dict[str, Any]) -> Self:
99
-
return cls.from_json(json.dumps(obj))
43
+
"""Returns the object represented by the Dict"""
44
+
return cls.model_validate(obj, strict=True)
100
45
101
46
@classmethod
102
47
def from_json(cls, json_str: str) -> Self:
103
48
"""Returns the object represented by the json string"""
raise ValueError("No match found when deserializing the JSON string into {{{classname}}} with anyOf schemas: {{#anyOf}}{{{.}}}{{^-last}}, {{/-last}}{{/anyOf}}. Details: " + ", ".join(error_messages))
149
-
else:
150
-
return instance
49
+
return cls.model_validate_json(json_str)
151
50
152
51
def to_json(self) -> str:
153
52
"""Returns the JSON representation of the actual instance"""
154
-
if self.actual_instance is None:
155
-
return "null"
156
-
157
-
if hasattr(self.actual_instance, "to_json") and callable(self.actual_instance.to_json):
0 commit comments