From 1165b9cdda532e2b84582b99bff2aa07935133f8 Mon Sep 17 00:00:00 2001 From: "elijah.benizzy" Date: Sun, 6 Feb 2022 19:49:43 -0800 Subject: [PATCH] Utilizes chainmap instead of {**..., **...} for merging dicts This is more generic, and allows us to support lazily evaluated configurations if needed. --- hamilton/graph.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/hamilton/graph.py b/hamilton/graph.py index e7abfa42..2fddcbee 100644 --- a/hamilton/graph.py +++ b/hamilton/graph.py @@ -5,11 +5,12 @@ Note: one should largely consider the code in this module to be "private". """ +import collections import inspect import logging import typing from types import ModuleType -from typing import Type, Dict, Any, Callable, Tuple, Set, Collection, List +from typing import Type, Dict, Any, Callable, Tuple, Set, Collection, List, Mapping import hamilton.function_modifiers_base from hamilton import node @@ -310,10 +311,10 @@ def dfs_traverse(node: node.Node): @staticmethod def execute_static(nodes: Collection[node.Node], - inputs: Dict[str, Any], + inputs: Mapping[str, Any], adapter: base.HamiltonGraphAdapter, - computed: Dict[str, Any] = None, - overrides: Dict[str, Any] = None): + computed: Mapping[str, Any] = None, + overrides: Mapping[str, Any] = None): """Executes computation on the given graph, inputs, and memoized computation. Effectively this is a "private" function and should be viewed as such. @@ -369,7 +370,7 @@ def dfs_traverse(node: node.Node, dependency_type: DependencyType = DependencyTy return computed @staticmethod - def combine_config_and_inputs(config: Dict[str, Any], inputs: Dict[str, Any]) -> Dict[str, Any]: + def combine_config_and_inputs(config: Mapping[str, Any], inputs: Mapping[str, Any]) -> typing.Mapping[str, Any]: """Validates and combines config and inputs, ensuring that they're mutually disjoint. :param config: Config to construct, run the DAG with. :param inputs: Inputs to run the DAG on at runtime @@ -379,7 +380,7 @@ def combine_config_and_inputs(config: Dict[str, Any], inputs: Dict[str, Any]) -> duplicated_inputs = [key for key in inputs if key in config] if len(duplicated_inputs) > 0: raise ValueError(f'The following inputs are present in both config and inputs. They must be mutually disjoint. {duplicated_inputs}') - return {**config, **inputs} + return collections.ChainMap(config, inputs) def execute(self, nodes: Collection[node.Node] = None,