From c8717e602ddc30519205e832cd1366502ab9efbf Mon Sep 17 00:00:00 2001 From: "Lex Darlog (DRL)" <3897975+Lex-DRL@users.noreply.github.com> Date: Fri, 6 Mar 2026 01:49:43 -0300 Subject: [PATCH] support for immutable dicts --- src/basic_data_handling/dict_nodes.py | 36 ++++++++++++++++----------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/basic_data_handling/dict_nodes.py b/src/basic_data_handling/dict_nodes.py index 2393634..deb71c2 100644 --- a/src/basic_data_handling/dict_nodes.py +++ b/src/basic_data_handling/dict_nodes.py @@ -1,5 +1,6 @@ from typing import Any from inspect import cleandoc +from itertools import chain try: from comfy.comfy_types.node_typing import IO, ComfyNodeABC @@ -624,17 +625,18 @@ class DictMerge(ComfyNodeABC): FUNCTION = "merge" def merge(self, dict1: dict, dict2=None, dict3=None, dict4=None) -> tuple[dict]: - result = dict1.copy() - - if dict2 is not None: - result.update(dict2) - - if dict3 is not None: - result.update(dict3) - - if dict4 is not None: - result.update(dict4) + extra_dicts = [x for x in (dict2, dict3, dict4) if x is not None] + if not extra_dicts: + return (dict1,) + # dict1 might be something like frozendict or other immutable mapping class. + # Thus, we shouldn't just copy-and-update, + # but instead we should build a new instance of the same type: + dict1_type = type(dict1) + result = dict1_type(chain( + dict1.items(), + *(x.items() for x in extra_dicts), + )) return (result,) @@ -806,8 +808,11 @@ class DictSet(ComfyNodeABC): FUNCTION = "set" def set(self, input_dict: dict, key: str, value: Any) -> tuple[dict]: - result = input_dict.copy() - result[key] = value + # input_dict might be something like frozendict or other immutable mapping class. + # Thus, we shouldn't just copy-and-update, + # but instead we should build a new instance of the same type: + dict_type = type(input_dict) + result = dict_type(chain(input_dict.items(), (key, value))) return (result,) @@ -864,8 +869,11 @@ class DictUpdate(ComfyNodeABC): FUNCTION = "update" def update(self, dict1: dict, dict2: dict) -> tuple[dict]: - result = dict1.copy() - result.update(dict2) + # dict1 might be something like frozendict or other immutable mapping class. + # Thus, we shouldn't just copy-and-update, + # but instead we should build a new instance of the same type: + dict1_type = type(dict1) + result = dict1_type(chain(dict1.items(), dict2.items())) return (result,)