github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/python/python-experimental/model_templates/methods_setattr_getattr_composed.mustache (about) 1 def __setattr__(self, name, value): 2 """this allows us to set a value with instance.field_name = val""" 3 if name in self.required_properties: 4 self.__dict__[name] = value 5 return 6 7 # set the attribute on the correct instance 8 model_instances = self._var_name_to_model_instances.get( 9 name, self._additional_properties_model_instances) 10 if model_instances: 11 for model_instance in model_instances: 12 if model_instance == self: 13 self.set_attribute(name, value) 14 else: 15 setattr(model_instance, name, value) 16 if name not in self._var_name_to_model_instances: 17 # we assigned an additional property 18 self.__dict__['_var_name_to_model_instances'][name] = ( 19 model_instance 20 ) 21 return None 22 23 path_to_item = [] 24 if self._path_to_item: 25 path_to_item.extend(self._path_to_item) 26 path_to_item.append(name) 27 raise ApiKeyError( 28 "{0} has no key '{1}'".format(type(self).__name__, name), 29 path_to_item 30 ) 31 32 def __getattr__(self, name): 33 """this allows us to get a value with val = instance.field_name""" 34 if name in self.required_properties: 35 return self.__dict__[name] 36 37 # get the attribute from the correct instance 38 model_instances = self._var_name_to_model_instances.get( 39 name, self._additional_properties_model_instances) 40 path_to_item = [] 41 if self._path_to_item: 42 path_to_item.extend(self._path_to_item) 43 path_to_item.append(name) 44 values = [] 45 # A composed model stores child (oneof/anyOf/allOf) models under 46 # self._var_name_to_model_instances. A named property can exist in 47 # multiple child models. If the property is present in more than one 48 # child model, the value must be the same across all the child models. 49 if model_instances: 50 for model_instance in model_instances: 51 if name in model_instance._data_store: 52 v = model_instance._data_store[name] 53 if v not in values: 54 values.append(v) 55 len_values = len(values) 56 if len_values == 0: 57 raise ApiKeyError( 58 "{0} has no key '{1}'".format(type(self).__name__, name), 59 path_to_item 60 ) 61 elif len_values == 1: 62 return values[0] 63 elif len_values > 1: 64 raise ApiValueError( 65 "Values stored for property {0} in {1} differ when looking " 66 "at self and self's composed instances. All values must be " 67 "the same".format(name, type(self).__name__), 68 path_to_item 69 )