Created
August 27, 2024 20:54
-
-
Save myles/1eb4ff00cebf4c932b6e431caf35a81b to your computer and use it in GitHub Desktop.
Function to merges two dataclass instances
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import asdict, is_dataclass | |
def merge_dataclasses(dataclass_a, dataclass_b): | |
""" | |
Merge two dataclasses into a new dataclass. | |
""" | |
if ( | |
is_dataclass(dataclass_a) is False | |
and is_dataclass(dataclass_b) is False | |
): | |
raise ValueError("Both arguments must be dataclasses.") | |
if dataclass_a.__class__ != dataclass_b.__class__: | |
raise ValueError("Both dataclasses must be of the same type.") | |
data_a = asdict(dataclass_a) | |
data_b = asdict(dataclass_b) | |
data = {} | |
for key in data_a.keys(): | |
data[key] = data_a[key] if data_a[key] is not None else data_b[key] | |
return dataclass_a.__class__(**data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment