Skip to content

fix(jwk): handle KeySet equality with unrelated types - #106

Open
janmrow wants to merge 1 commit into
authlib:mainfrom
janmrow:fix/keyset-equality
Open

fix(jwk): handle KeySet equality with unrelated types#106
janmrow wants to merge 1 commit into
authlib:mainfrom
janmrow:fix/keyset-equality

Conversation

@janmrow

@janmrow janmrow commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

KeySet.__eq__ currently raises an exception when a KeySet is compared with an unrelated type.

This PR returns NotImplemented for unsupported types. This lets Python handle the comparison, while keeping existing KeySet-to-KeySet comparisons unchanged.

Regression tests cover comparisons with unrelated types, also including reflected equality.

Tests on local:

  • pytest tests/ -q
  • mypy src/joserfc/_keys.py

Comment thread src/joserfc/_keys.py
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants