From 302018d3cf6ad7cee409fc070c5316ab47a6f7e0 Mon Sep 17 00:00:00 2001 From: Asankhaya Sharma Date: Sat, 21 Jun 2025 07:13:46 +0800 Subject: [PATCH 1/2] Filter unsupported fields in Program.from_dict Updated Program.from_dict to ignore dictionary keys that do not correspond to dataclass fields, preventing errors from unexpected data. Added debug logging to indicate when unsupported fields are filtered out. --- openevolve/database.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/openevolve/database.py b/openevolve/database.py index 0f8c73742..2ae1aa19c 100644 --- a/openevolve/database.py +++ b/openevolve/database.py @@ -8,7 +8,7 @@ import os import random import time -from dataclasses import asdict, dataclass, field +from dataclasses import asdict, dataclass, field, fields from pathlib import Path from typing import Any, Dict, List, Optional, Set, Tuple, Union @@ -73,7 +73,18 @@ def to_dict(self) -> Dict[str, Any]: @classmethod def from_dict(cls, data: Dict[str, Any]) -> "Program": """Create from dictionary representation""" - return cls(**data) + # Get the valid field names for the Program dataclass + valid_fields = {f.name for f in fields(cls)} + + # Filter the data to only include valid fields + filtered_data = {k: v for k, v in data.items() if k in valid_fields} + + # Log if we're filtering out any fields + if len(filtered_data) != len(data): + filtered_out = set(data.keys()) - set(filtered_data.keys()) + logger.debug(f"Filtered out unsupported fields when loading Program: {filtered_out}") + + return cls(**filtered_data) class ProgramDatabase: From 0fe8bf5bbab0ded4d0a000a0c0214920c59e73b1 Mon Sep 17 00:00:00 2001 From: Asankhaya Sharma Date: Sat, 21 Jun 2025 07:17:34 +0800 Subject: [PATCH 2/2] Refactor whitespace in Program.from_dict method Cleaned up whitespace in the Program.from_dict method for improved code readability. No functional changes were made. --- openevolve/database.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/openevolve/database.py b/openevolve/database.py index 2ae1aa19c..b23b660dd 100644 --- a/openevolve/database.py +++ b/openevolve/database.py @@ -75,15 +75,15 @@ def from_dict(cls, data: Dict[str, Any]) -> "Program": """Create from dictionary representation""" # Get the valid field names for the Program dataclass valid_fields = {f.name for f in fields(cls)} - + # Filter the data to only include valid fields filtered_data = {k: v for k, v in data.items() if k in valid_fields} - + # Log if we're filtering out any fields if len(filtered_data) != len(data): filtered_out = set(data.keys()) - set(filtered_data.keys()) logger.debug(f"Filtered out unsupported fields when loading Program: {filtered_out}") - + return cls(**filtered_data)