Skip to content

Commit

Permalink
Fix bug where class is initialized with new parameters despite incons…
Browse files Browse the repository at this point in the history
…istency.
  • Loading branch information
BirchKwok committed Apr 15, 2024
1 parent c9d480a commit 9765ca9
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions min_vec/configs/parameters_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ def check_configs(self, configs: dict, update_configs_dict: dict):
self.logger.warning(f"The database configuration {key} has been set to {value}, "
f"the new value {update_configs_dict[key]} will be ignored.")

for key, value in update_configs_dict.items():
if key not in configs:
self.logger.warning(f"The database configuration {key} is not set, "
f"and it will be set to {value}.")
configs[key] = value

return configs

def load_configs(self, configs_json: Path):
"""Load the configurations."""
try:
Expand All @@ -39,13 +47,18 @@ def save_configs(self, configs_json: Path, configs: dict):
try:
with open(configs_json, 'w') as f:
json.dump(configs, f)

return configs
except (PermissionError, OSError) as e:
self.logger.error(f"Failed to save the MinVectorDB configurations.")
raise e

def __call__(self, func):
@wraps(func)
def wrapper(*args, **kwargs):
# first parameter is self
self_instance = args[0]

kwargs = generate_function_kwargs(func, *args, **kwargs)

dir_path = Path(kwargs.get("database_path"))
Expand All @@ -66,11 +79,11 @@ def wrapper(*args, **kwargs):
configs_json = self.database_path_parent / Path('configs.json')

if first_create or not configs_json.exists():
self.save_configs(configs_json, update_configs_dict)
final_configs = self.save_configs(configs_json, update_configs_dict)
else:
existing_configs = self.load_configs(configs_json)
self.check_configs(existing_configs, update_configs_dict)
final_configs = self.check_configs(existing_configs, update_configs_dict)

return func(*args, **kwargs)
return func(self_instance, **final_configs)

return wrapper

0 comments on commit 9765ca9

Please sign in to comment.