From aa61325bb64b1e2f70dca4dd88b5c1ac453d515e Mon Sep 17 00:00:00 2001 From: Miguel Angel Garcia Date: Mon, 27 May 2024 03:47:06 +0200 Subject: [PATCH] Fix timestamp comparison Signed-off-by: Miguel Angel Garcia --- prometheus_client/samples.py | 4 ++-- tests/test_samples.py | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/prometheus_client/samples.py b/prometheus_client/samples.py index 8735fed9..ea2b1aaf 100644 --- a/prometheus_client/samples.py +++ b/prometheus_client/samples.py @@ -28,10 +28,10 @@ def __ne__(self, other: object) -> bool: return not self == other def __gt__(self, other: "Timestamp") -> bool: - return self.sec > other.sec or self.nsec > other.nsec + return (self.sec, self.nsec) > (other.sec, other.nsec) def __lt__(self, other: "Timestamp") -> bool: - return self.sec < other.sec or self.nsec < other.nsec + return (self.sec, self.nsec) < (other.sec, other.nsec) # Timestamp and exemplar are optional. diff --git a/tests/test_samples.py b/tests/test_samples.py index 796afe7e..7b59218b 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -12,6 +12,8 @@ def test_gt(self): self.assertEqual(samples.Timestamp(1, 2) > samples.Timestamp(1, 1), True) self.assertEqual(samples.Timestamp(2, 1) > samples.Timestamp(1, 1), True) self.assertEqual(samples.Timestamp(2, 2) > samples.Timestamp(1, 1), True) + self.assertEqual(samples.Timestamp(0, 2) > samples.Timestamp(1, 1), False) + self.assertEqual(samples.Timestamp(2, 0) > samples.Timestamp(1, 1), True) def test_lt(self): self.assertEqual(samples.Timestamp(1, 1) < samples.Timestamp(1, 1), False) @@ -21,6 +23,8 @@ def test_lt(self): self.assertEqual(samples.Timestamp(1, 2) < samples.Timestamp(1, 1), False) self.assertEqual(samples.Timestamp(2, 1) < samples.Timestamp(1, 1), False) self.assertEqual(samples.Timestamp(2, 2) < samples.Timestamp(1, 1), False) + self.assertEqual(samples.Timestamp(0, 2) < samples.Timestamp(1, 1), True) + self.assertEqual(samples.Timestamp(2, 0) < samples.Timestamp(1, 1), False) if __name__ == '__main__':