Skip to content

normalize_value_for_hashing methods are used from all registered operators #606

Description

@andrewVolodko

Describe the bug
normalize_value_for_hashing method is not custom operator scoped, so that all normalize_value_for_hashing methods from all registered operators are invoked during comparison, ignoring match method result.

To Reproduce

  1. Create custom operator.
  2. Implement match method so that it returns True on comparisons of str only.
  3. Implement normalize_value_for_hashing any way so that you can set a breakpoint inside. (or add print)
  4. Compare two integers with ignore_order = True in debug mode (so that match method returns False).
  5. Code stops in normalize_value_for_hashing method.

Expected behavior
Since the operator should not be used in case of comparison of integers, normalize_value_for_hashing method from the operator should not be invoked.

Additional info:
Because of this behavior, if registered comparator has normalize_value_for_hashing with pass inside, comparison of lists is fully incorrect. E.g. [1, 2], [2, 3] - these lists are assumed as identical by deepdiff, even though the comparator above shouldnt have been invoked.

OS, DeepDiff version and Python version (please complete the following information):

  • OS: MacOS 15.7.1
  • Python Version 3.13.2
  • DeepDiff Version 9.1.0

Example:

from typing import Any

from deepdiff import DeepDiff
from deepdiff.model import DiffLevel
from deepdiff.operator import BaseOperatorPlus


class StrCompareCustomOperator(BaseOperatorPlus):
    def match(self, level: DiffLevel) -> bool:
        return isinstance(level.t1, str) and isinstance(level.t2, str)

    def give_up_diffing(self, level: DiffLevel, diff_instance: DeepDiff) -> bool: 
        actual = level.t2
        expected = level.t1

        return actual == expected

    def normalize_value_for_hashing(self, parent: Any, obj: Any) -> Any:
        pass

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions