-
-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix IsNow
#48
Comments
Here's what's happening if exceptions are allowed to propagate: Traceback (most recent call last):
File "/home/alex/.config/JetBrains/PyCharm2023.2/scratches/scratch_1195.py", line 3, in <module>
assert '2023-09-15T10:56:38.311Z' == IsNow(delta=1000000, tz='utc', format_string='%Y-%m-%dT%H:%M:%S.%fZ', enforce_tz=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alex/work/dirty-equals/dirty_equals/_base.py", line 104, in __eq__
self._was_equal = self.equals(other)
^^^^^^^^^^^^^^^^^^
File "/home/alex/work/dirty-equals/dirty_equals/_numeric.py", line 118, in equals
return self.bounds_checks(other)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alex/work/dirty-equals/dirty_equals/_numeric.py", line 133, in bounds_checks
return self.approx_equals(other, delta)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alex/work/dirty-equals/dirty_equals/_datetime.py", line 111, in approx_equals
if not super().approx_equals(other, delta):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/alex/work/dirty-equals/dirty_equals/_numeric.py", line 146, in approx_equals
return abs(self.approx - other) <= delta
~~~~~~~~~~~~^~~~~~~
TypeError: can't subtract offset-naive and offset-aware datetimes This fixes it: @@ -103,8 +103,11 @@ class IsDatetime(IsNumeric[datetime]):
else:
raise ValueError(f'{type(other)} not valid as datetime')
- if self.approx is not None and not self.enforce_tz and self.approx.tzinfo is None and dt.tzinfo is not None:
- dt = dt.replace(tzinfo=None)
+ if self.approx is not None and not self.enforce_tz:
+ if self.approx.tzinfo is None and dt.tzinfo is not None:
+ dt = dt.replace(tzinfo=None)
+ elif self.approx.tzinfo is not None and dt.tzinfo is None:
+ dt = dt.replace(tzinfo=self.approx.tzinfo)
return dt but it causes failures in existing tests, e.g: pytest.param(
datetime(2020, 1, 1, 12, 13, 14),
IsDatetime(approx=datetime(2020, 1, 1, 12, 13, 14, tzinfo=timezone.utc), enforce_tz=False),
False,
id='tz-approx-tz',
), so I'm confused about the desired behaviour. |
This was referenced Sep 16, 2023
I was also expecting this to work: now_utc = datetime.now(timezone.utc)
date_format = "%Y-%m-%dT%H:%M:%S.%fZ"
> assert now_utc.strftime(date_format) == IsNow(format_string=date_format, tz=timezone.utc)
E AssertionError: assert '2024-05-22T08:12:30.899780Z' == IsNow(approx=datetime.datetime(2024, 5, 22, 8, 35, 9, 821721, tzinfo=datetime.timezone.utc), delta=datetime.timedelta(seconds=2), format_string='%Y-%m-%dT%H:%M:%S.%fZ', tz=datetime.timezone.utc)
E + where '2024-05-22T08:12:30.899780Z' = <built-in method strftime of datetime.datetime object at 0x10454b4b0>('%Y-%m-%dT%H:%M:%S.%fZ')
E + where <built-in method strftime of datetime.datetime object at 0x10454b4b0> = datetime.datetime(2024, 5, 22, 8, 12, 30, 899780, tzinfo=datetime.timezone.utc).strftime
E + and IsNow(approx=datetime.datetime(2024, 5, 22, 8, 35, 9, 821721, tzinfo=datetime.timezone.utc), delta=datetime.timedelta(seconds=2), format_string='%Y-%m-%dT%H:%M:%S.%fZ', tz=datetime.timezone.utc) = IsNow(format_string='%Y-%m-%dT%H:%M:%S.%fZ', tz=datetime.timezone.utc)
E + where datetime.timezone.utc = timezone.utc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following should pass
(ignoring that that's not "now" any longer obvisouly)
The text was updated successfully, but these errors were encountered: