From a9bf387e5ecaf8877c9c8a4d28bd9c62254492eb Mon Sep 17 00:00:00 2001
From: Lincoln Stein <lstein@gmail.com>
Date: Mon, 7 Aug 2023 14:04:53 -0400
Subject: [PATCH] turned on Pydantic `validate_assignment`

---
 invokeai/app/services/config.py                | 13 ++++++++++---
 invokeai/backend/install/invokeai_configure.py |  6 +++++-
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/invokeai/app/services/config.py b/invokeai/app/services/config.py
index 7b5015a670..bc615be7b2 100644
--- a/invokeai/app/services/config.py
+++ b/invokeai/app/services/config.py
@@ -28,7 +28,6 @@ InvokeAI:
     always_use_cpu: false
     free_gpu_mem: false
   Features:
-    restore: true
     esrgan: true
     patchmatch: true
     internet_available: true
@@ -165,7 +164,7 @@ import pydoc
 import os
 import sys
 from argparse import ArgumentParser
-from omegaconf import OmegaConf, DictConfig
+from omegaconf import OmegaConf, DictConfig, ListConfig
 from pathlib import Path
 from pydantic import BaseSettings, Field, parse_obj_as
 from typing import ClassVar, Dict, List, Set, Literal, Union, get_origin, get_type_hints, get_args
@@ -189,7 +188,12 @@ class InvokeAISettings(BaseSettings):
         opt = parser.parse_args(argv)
         for name in self.__fields__:
             if name not in self._excluded():
-                setattr(self, name, getattr(opt, name))
+                value = getattr(opt, name)
+                if isinstance(value, ListConfig):
+                    value = list(value)
+                elif isinstance(value, DictConfig):
+                    value = dict(value)
+                setattr(self, name, value)
 
     def to_yaml(self) -> str:
         """
@@ -426,6 +430,9 @@ class InvokeAIAppConfig(InvokeAISettings):
     version             : bool = Field(default=False, description="Show InvokeAI version and exit", category="Other")
     # fmt: on
 
+    class Config:
+        validate_assignment = True
+
     def parse_args(self, argv: List[str] = None, conf: DictConfig = None, clobber=False):
         """
         Update settings with contents of init file, environment, and
diff --git a/invokeai/backend/install/invokeai_configure.py b/invokeai/backend/install/invokeai_configure.py
index a846c50dc3..979c5d3337 100755
--- a/invokeai/backend/install/invokeai_configure.py
+++ b/invokeai/backend/install/invokeai_configure.py
@@ -63,6 +63,7 @@ from invokeai.backend.install.model_install_backend import (
     ModelInstall,
 )
 from invokeai.backend.model_management.model_probe import ModelType, BaseModelType
+from pydantic.error_wrappers import ValidationError
 
 warnings.filterwarnings("ignore")
 transformers.logging.set_verbosity_error()
@@ -659,7 +660,10 @@ def migrate_init_file(legacy_format: Path):
     fields = [x for x, y in InvokeAIAppConfig.__fields__.items() if y.field_info.extra.get("category") != "DEPRECATED"]
     for attr in fields:
         if hasattr(old, attr):
-            setattr(new, attr, getattr(old, attr))
+            try:
+                setattr(new, attr, getattr(old, attr))
+            except ValidationError as e:
+                print(f'* Ignoring incompatible value for field {attr}:\n  {str(e)}')
 
     # a few places where the field names have changed and we have to
     # manually add in the new names/values