Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/joserfc/_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def __bool__(self) -> bool:
return bool(self.keys)

def __eq__(self, other: t.Any) -> bool:
assert isinstance(other, KeySet)
if not isinstance(other, KeySet):
return NotImplemented

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just return False

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered False, but switched to NotImplemented, because that follows Python's equality protocol and allows the other operand's __eq__ to handle the comparison if appropriate.

But for KeySet that extra behavior probably isn't needed.

Looks likeFalse is simpler here and matches BaseKey.__eq__, so I suppose that fits better to the project. Happy to change it.

return self.keys == other.keys

def as_dict(self, private: bool = False, **params: t.Any) -> KeySetSerialization:
Expand Down
13 changes: 13 additions & 0 deletions tests/jwk/test_jwk_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,16 @@ def test_key_eq_with_new_keys(self):
key_set2 = KeySet([RSAKey.import_key(k.as_dict(private=True)) for k in key_set1])
self.assertIsNot(key_set1, key_set2)
self.assertEqual(key_set1, key_set2)

def test_key_set_eq_with_unrelated_type(self):
key_set = KeySet.generate_key_set("oct", 8, count=1)
self.assertFalse(key_set == "foo")
self.assertNotEqual(key_set, "foo")

def test_key_set_eq_uses_reflected_comparison(self):
class EqualToKeySet:
def __eq__(self, other):
return isinstance(other, KeySet)

key_set = KeySet.generate_key_set("oct", 8, count=1)
self.assertTrue(key_set == EqualToKeySet())