compare_dicts#

compare_dicts(dict1, dict2, exclude_keys=None, include_only_keys=None)#

Recursively compare two dictionaries with optional key filters.

Comparison order:
  1. Effective key sets (after include/exclude),

  2. Values under those keys (recursing into nested dicts),

  3. Sets are compared via sorted order, other values via native ordering.

Parameters:
  • dict1 (dict[str, Any]) – First dictionary to compare.

  • dict2 (dict[str, Any]) – Second dictionary to compare.

  • exclude_keys (Iterable[str] | None) – Keys to ignore at the top level.

  • include_only_keys (Iterable[str] | None) – If provided, only these top-level keys are considered.

Returns:

1 if dict1 > dict2, -1 if dict1 < dict2, 0 if equal.

Raises:

TypeError – If corresponding values have mismatched types and cannot be compared.

Return type:

int