Skip to content
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

feat: add __eq__ for Key and KeySet #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

feat: add __eq__ for Key and KeySet #38

wants to merge 1 commit into from

Conversation

lepture
Copy link
Member

@lepture lepture commented Mar 26, 2025

Replacing: #37

@bjmc

Copy link

codecov bot commented Mar 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.84%. Comparing base (94a651d) to head (2ebe3cc).

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #38   +/-   ##
=======================================
  Coverage   99.84%   99.84%           
=======================================
  Files          44       44           
  Lines        2606     2613    +7     
  Branches      306      307    +1     
=======================================
+ Hits         2602     2609    +7     
  Misses          2        2           
  Partials        2        2           
Flag Coverage Δ
unittests 99.84% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

Please retry analysis of this Pull-Request directly on SonarQube Cloud

Copy link

Please retry analysis of this Pull-Request directly on SonarQube Cloud

@@ -120,6 +120,10 @@ def __iter__(self) -> t.Iterator[Key]:
def __bool__(self) -> bool:
return bool(self.keys)

def __eq__(self, other: t.Any) -> bool:
assert isinstance(other, KeySet)
Copy link

@bjmc bjmc Mar 26, 2025

Choose a reason for hiding this comment

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

This can raise an AssertionError not return a bool

I think it's usually preferred to avoid assert outside of tests because in certain circumstances it can be removed by optimization flags.

Copy link

Choose a reason for hiding this comment

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

Separately: isinstance() checks can sometimes conflict with ducktyping. This answer suggests returning NotImplemented to give the other object a chance to check its own notion of equality.

Copy link
Member Author

Choose a reason for hiding this comment

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

The assert is used to fix mypy issues

Copy link

Choose a reason for hiding this comment

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

What happens if you do KeySet([]) == object()? Does it throw an AssertionError or return False? I don't see any tests covering comparison against non-KeySet objects.

@@ -76,3 +76,18 @@ def test_key_set_iter(self):
key_set = KeySet.generate_key_set('RSA', 2048)
for k in key_set:
self.assertEqual(k.key_type, "RSA")

def test_key_eq_with_same_keys(self):
key_set1 = KeySet.generate_key_set('RSA', 2048)
Copy link

@bjmc bjmc Mar 26, 2025

Choose a reason for hiding this comment

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

Nitpick but generating all these key/sets repeatedly in every test is inefficient in terms of wall/CPU time and energy.

Consider using setUpClass() or module-scoped Pytest fixtures to make the tests run faster / be kinder to your (+ everyone else's) computer and the planet.

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