Strategies

Authoring your own Strategy

Your function should take the arguments of (merger, path, base_value, value_to_merge_in).

Strategies are passed as a list, and the merge runs through each strategy in the order passed into the merger, stopping at the first one to return a value that is not the sentinel value deepmerge.STRATEGY_END.

For example, this function would not be considered valid for any base value besides the string “foo”:

from deepmerge import STRATEGY_END

def return_true_if_foo(config, path, base, nxt):
  if base == "foo":
      return True
  return STRATEGY_END

Note that the merger does not copy values before passing them into mergers for performance reasons.

Builtin Strategies

These are the built in strategies provided by deepmerge.

class deepmerge.strategy.type_conflict.TypeConflictStrategies(strategy_list)

contains the strategies provided for type conflicts.

NAME = 'type conflict'
static strategy_override(config, path, base, nxt)

overrides the new object over the old object

static strategy_override_if_not_empty(config, path, base, nxt)

overrides the new object over the old object only if the new object is not empty or null

static strategy_use_existing(config, path, base, nxt)

uses the old object instead of the new object

class deepmerge.strategy.fallback.FallbackStrategies(strategy_list)

The StrategyList containing fallback strategies.

NAME = 'fallback'
static strategy_override(config, path, base, nxt)

use nxt, and ignore base.

static strategy_use_existing(config, path, base, nxt)

use base, and ignore next.

class deepmerge.strategy.dict.DictStrategies(strategy_list)

Contains the strategies provided for dictionaries.

NAME = 'dict'
static strategy_merge(config, path, base, nxt)

for keys that do not exists, use them directly. if the key exists in both dictionaries, attempt a value merge.

static strategy_override(config, path, base, nxt)

move all keys in nxt into base, overriding conflicts.

class deepmerge.strategy.list.ListStrategies(strategy_list)

Contains the strategies provided for lists.

NAME = 'list'
static strategy_append(config, path, base, nxt)

append nxt to base.

static strategy_override(config, path, base, nxt)

use the list nxt.

static strategy_prepend(config, path, base, nxt)

prepend nxt to base.