diff --git a/instruct/templates/macros.jinja b/instruct/templates/macros.jinja index eabe8e0..30ce0cf 100644 --- a/instruct/templates/macros.jinja +++ b/instruct/templates/macros.jinja @@ -24,22 +24,34 @@ def make_setter(type_def, fast, derived, type_restriction, coerce_types=(), coer assert type_restriction is not None if fast: - def _set_{{field_name}}(self, val: type_def) -> NoneType: + def _set_{{field_name}}(self, val: type_def) -> None: {%- if has_coercion %} if isinstance(val, coerce_types): val = coerce_func(val) {%- endif %} + assert isinstance(val, type_restriction) {%- if on_sets %} _old_value = self.{{field_name}} {%- endif %} - {{setter_variable_template|format(key=field_name)|indent(12)}} + {{ setter_variable_template|format(key=field_name)|indent(12) }} {%- if on_sets %} {%- for on_func_name in on_sets %} self.{{on_func_name}}(_old_value, val) {%- endfor %} {%- endif %} + {%- if on_sets_1 %} + {%- for on_func_name in on_sets_1 %} + self.{{on_func_name}}(val) + {%- endfor %} + {%- endif %} + {%- if on_sets_3 %} + {%- for on_func_name in on_sets_3 %} + self.{{on_func_name}}("{{field_name}}", _old_value, val) + {%- endfor %} + {%- endif %} + else: - def _set_{{field_name}}(self, val: type_def) -> NoneType: + def _set_{{field_name}}(self, val: type_def) -> None: try: wrong_type = isinstance(val, type_restriction) except TypeError: