deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

compare datetime.date as numbers

Open mrxra opened this issue 3 years ago • 1 comments

Please checkout the F.A.Q page before creating a bug ticket to make sure it is not already addressed.

Describe the bug comparing datastructures that contain numbers and datetime.date objects raise a TypeError when certain flags related to number comparisons are set (e.g. math_epsilon). it appears some values of the datetime module are treated as plain numbers https://github.com/seperman/deepdiff/blob/81341e2827d083429bcdfb4617cd40e1188bbdb7/deepdiff/helper.py#L109 I assume this causes datetime.date to be compared by DeepDiff._diff_numbers rather than DeepDiff._diff_datetimes (as I would expect) https://github.com/seperman/deepdiff/blob/81341e2827d083429bcdfb4617cd40e1188bbdb7/deepdiff/diff.py#L1330

To Reproduce

import datetime
from deepdiff import DeepDiff
# passes
DeepDiff(datetime.datetime(2022, 1, 1), datetime.datetime(2022, 1, 1), math_epsilon=0.001)
# raises TypeError: must be real number, not datetime.date
DeepDiff(datetime.date(2022, 1, 1), datetime.date(2022, 1, 1), math_epsilon=0.001)

Expected behavior No exception should be raised, the values should be detected as equal/unchanged. flags that impact number comparisons (math_epsion, precision flags,...) should probably not impact datetime comparisons

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

  • DeepDiff 5.8.1
  • Python 3.8
  • OS: Ubuntu (running in WSL2)
  • Version 18.04.5 LTS

Additional context Add any other context about the problem here.

mrxra avatar Jul 30 '22 13:07 mrxra

this workaround kind of fixes the issue for now, but I'm not sure about any other side effects I might run into due to this change....not sure what the rationale is/was behind defining the types 'numbers' and 'times' the way they have been defined...

import datetime
import deepdiff
deepdiff.diff.times = deepdiff.helper.datetimes

# passes as expected
deepdiff.DeepDiff(datetime.date(2022, 1, 1), datetime.date(2022, 1, 1), math_epsilon=0.001)
# passes
deepdiff.DeepDiff(datetime.datetime(2022, 1, 1), datetime.datetime(2022, 1, 1), math_epsilon=0.001)

mrxra avatar Jul 30 '22 14:07 mrxra

This was fixed a while back. Closing the ticket!

seperman avatar Nov 20 '23 01:11 seperman