Skip to content

Do not run custom operators on excluded paths - #676

Open
tony1233200 wants to merge 1 commit into
qlustered:devfrom
tony1233200:fix/633-custom-operator-excluded-path
Open

tony1233200 wants to merge 1 commit into
qlustered:devfrom
tony1233200:fix/633-custom-operator-excluded-path

Conversation

@tony1233200

Copy link
Copy Markdown

Fixes #633

Custom operators were invoked on paths excluded via exclude_paths, even though the comparison result was then discarded. For example, with exclude_paths=["root['b']"], give_up_diffing was still called for root['b'], so operators paid the cost of the comparison (and any of its side effects) for a path the user asked to skip:

DeepDiff(t1, t2,
         custom_operators=[ListMatchOperator(types=[CustomClass])],
         exclude_paths=["root['b']"])
# before: prints "Comparing root['a']" and "Comparing root['b']"
# after:  prints "Comparing root['a']" only

Changes

  • deepdiff/diff.py (DeepDiff._diff): when custom operators are set, the exclusion check (_skip_this, which covers exclude_paths / exclude_regex_paths / include_paths / exclude_types / callbacks) now runs before _use_custom_operator, so operators never see an excluded level. When no custom operators are set, the check stays in its original position, so behaviour for plain diffs is unchanged.
  • tests/test_operators.py: new test_custom_operator_not_applied_to_excluded_path reproducing the issue (records the paths the operator is invoked on; asserts only root['a'] is visited and the result is {}).

Tests

  • New regression test fails on unmodified dev and passes with this change.
  • Full test suite: 1345 passed, 45 skipped (baseline on dev is 1344 passed, 45 skipped; the +1 is the new test).
  • flake8 on the touched files: no new violations (the remaining findings in tests/test_operators.py are pre-existing on dev).

Notes

  • While verifying this I noticed that DeepDiff({b'k': 1}, {b'k': 2}) already crashes on unmodified dev (TypeError in stringify_element when computing the path of a bytes-keyed level). That is a pre-existing issue in the same area, already covered by open PR Fix paths for bytes dictionary keys #640, so I deliberately did not touch path.py here. This PR's reordering is guarded so the no-operator path is byte-for-byte the old order, keeping that pre-existing behaviour unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant