Skip to content

Commit 4e274da

Browse files
authored
Return NotImplemented from comparisons with non-Ksuid objects (#62)
1 parent ec8e1c9 commit 4e274da

2 files changed

Lines changed: 54 additions & 7 deletions

File tree

‎ksuid/ksuid.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,13 @@ def __bytes__(self) -> bytes:
8181
return self._uid
8282

8383
def __eq__(self, other: object) -> bool:
84-
assert isinstance(other, self.__class__)
84+
if not isinstance(other, Ksuid) or self.TIMESTAMP_LENGTH_IN_BYTES != other.TIMESTAMP_LENGTH_IN_BYTES:
85+
return NotImplemented
8586
return self._uid == other._uid
8687

87-
def __lt__(self: SelfT, other: SelfT) -> bool:
88+
def __lt__(self, other: object) -> bool:
89+
if not isinstance(other, Ksuid):
90+
return NotImplemented
8891
return self._uid < other._uid
8992

9093
def __hash__(self) -> int:

‎tests/test_ksuid.py‎

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
2-
import os
32
import math
4-
import typing as t
3+
import os
54
from datetime import datetime, timedelta, timezone
65

76
import pytest
@@ -73,6 +72,51 @@ def test_to_from_base62():
7372
assert ksuid == ksuid_from_base62
7473

7574

75+
def test_eq_with_non_ksuid_does_not_raise():
76+
ksuid = Ksuid.from_bytes(bytes(Ksuid.BYTES_LENGTH))
77+
78+
# Comparing against a foreign type must not raise; equality is False
79+
assert (ksuid == None) is False # noqa: E711
80+
assert (ksuid != None) is True # noqa: E711
81+
assert (ksuid == "not a ksuid") is False
82+
assert (ksuid == 42) is False
83+
assert None not in [ksuid]
84+
85+
86+
def test_eq_by_raw_bytes():
87+
raw = bytes(range(Ksuid.BYTES_LENGTH))
88+
assert Ksuid.from_bytes(raw) == Ksuid.from_bytes(raw)
89+
assert Ksuid.from_bytes(raw) != Ksuid.from_bytes(bytes(Ksuid.BYTES_LENGTH))
90+
# equal values must hash equally
91+
assert hash(Ksuid.from_bytes(raw)) == hash(Ksuid.from_bytes(raw))
92+
93+
94+
def test_eq_across_ksuid_subclasses_is_false():
95+
# Ksuid and KsuidMs are both 20 bytes, so the same raw bytes can be parsed
96+
# as either, but they split those bytes into different timestamp/payload
97+
# layouts (4- vs 5-byte timestamp). A Ksuid must never compare equal to a
98+
# KsuidMs that merely shares its byte representation.
99+
assert Ksuid.BYTES_LENGTH == KsuidMs.BYTES_LENGTH
100+
raw = bytes(range(Ksuid.BYTES_LENGTH))
101+
ksuid = Ksuid.from_bytes(raw)
102+
ksuid_ms = KsuidMs.from_bytes(raw)
103+
assert bytes(ksuid) == bytes(ksuid_ms) # identical raw bytes...
104+
assert ksuid != ksuid_ms # ...but not equal
105+
assert ksuid_ms != ksuid
106+
assert not (ksuid == ksuid_ms)
107+
# same-class equality is unaffected
108+
assert ksuid == Ksuid.from_bytes(raw)
109+
assert ksuid_ms == KsuidMs.from_bytes(raw)
110+
111+
112+
def test_ordering_with_non_ksuid_raises_type_error():
113+
ksuid = Ksuid.from_bytes(bytes(Ksuid.BYTES_LENGTH))
114+
with pytest.raises(TypeError):
115+
_ = ksuid < None
116+
with pytest.raises(TypeError):
117+
_ = ksuid < "not a ksuid"
118+
119+
76120
def test_to_from_bytes():
77121
# Arrange
78122
ksuid = Ksuid()
@@ -122,7 +166,7 @@ def test_payload_uniqueness():
122166
# Arrange
123167
now = datetime.now()
124168
timestamp = now.replace(microsecond=0).timestamp()
125-
ksuids_set: t.Set[Ksuid] = set()
169+
ksuids_set: set[Ksuid] = set()
126170
for i in range(TEST_ITEMS_COUNT):
127171
ksuids_set.add(Ksuid(datetime=now))
128172

@@ -135,7 +179,7 @@ def test_payload_uniqueness():
135179
def test_timestamp_uniqueness():
136180
# Arrange
137181
time = datetime.now()
138-
ksuids_set: t.Set[Ksuid] = set()
182+
ksuids_set: set[Ksuid] = set()
139183
for i in range(TEST_ITEMS_COUNT):
140184
ksuids_set.add(Ksuid(datetime=time, payload=EMPTY_KSUID_PAYLOAD))
141185
time += timedelta(seconds=1)
@@ -183,7 +227,7 @@ def test_ms_mode_datetime():
183227
def pytest_generate_tests(metafunc):
184228
if "test_data" in metafunc.fixturenames:
185229
data = []
186-
with open(TF_PATH, "r") as test_kuids:
230+
with open(TF_PATH) as test_kuids:
187231
for ksuid_json in test_kuids:
188232
data.append(json.loads(ksuid_json))
189233
metafunc.parametrize("test_data", data)

0 commit comments

Comments
 (0)