Skip to content

Are there plans to support providing functions to compare only the fields that exist in both objects? #562

Open

Description

@anfreshman

Is your feature request related to a problem? Please describe.
I've used deepdiff to compare some JSON objects, but now I have a new scenario for a new project. I need to compare two special dicts.

Describe the solution you'd like
Generally speaking, the two dicts have the same structure, both converted from the JSON format response of the HTTP request, but one dict has more fields and the other has fewer, only containing key fields. I hope there is a function that can automatically ignore the fields that exist in dict A but not in dict B, and only compare the differences in the fields that exist in both objects.

I read the API documentation and found that currently it seems that only the ignored fields can be manually specified, which is almost unusable for this scenario because there will be a lot of different fields. So is there a plan to support a function that automatically ignores the non-existent fields between the two dicts and only compares the fields that exist?

Activity

  1. chen56 commented on Jan 7, 2026

    @chen56

    @anfreshman

    I couldn't find any easy way to handle subset inclusion either. It seems that only include and exclude are applicable.

    like this code:

    from deepdiff import DeepDiff
    from deepdiff.helper import COLORED_VIEW
    
    
    def get_all_paths(d, current_path="root"):
        """get all paths from a nested dict or list"""
        paths = []
        if isinstance(d, dict):
            for k, v in d.items():
                new_path = f"{current_path}['{k}']"
                paths.extend(get_all_paths(v, new_path))
        elif isinstance(d, list):
            for i, v in enumerate(d):
                new_path = f"{current_path}[{i}]"
                paths.extend(get_all_paths(v, new_path))
        else:
            # leaf node(int, str, float...)
            paths.append(current_path)
        return paths
    
    
    subset = {"params": {"gpu": "A100", "memory": {"size": "64G"}}}
    full_set = {"params": {"gpu": "A100", "memory": {"size": "64G", "type": "HBM2"}}, "other": 1}
    
    print(f"get all leaf paths: {get_all_paths(subset)}")
    
    
    # get all leaf paths: ["root['params']['gpu']", "root['params']['memory']['size']"]
    
    def only_diff_subset(full_set: dict, subset_: dict):
        return DeepDiff(full_set, subset_, include_paths=get_all_paths(subset_), view=COLORED_VIEW)
    
    
    diff = only_diff_subset(full_set, subset)
    assert {} == diff, diff
    # ok
    
    diff = only_diff_subset(full_set, {"params": {"gpu": "XXX", "memory": {"size": "64G"}}})
    assert {} == diff, diff
    # Traceback (most recent call last):
    #   File "/Users/chen/git/chen56/applab_research/features/validation/diff/test_pycheck.py", line 39, in <module>
    #     assert {} == diff, diff
    #            ^^^^^^^^^^
    # AssertionError: {
    #   "params": {
    #     "gpu": "A100" -> "XXX",
    #     "memory": {
    #       "size": "64G"
    #     }
    #   }
    # }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions