-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
chore(utils): allow duplicate values in registry by making reverse lookup optional #82114
Conversation
Co-authored-by: Josh Callender <[email protected]>
@@ -15,9 +15,10 @@ class NoRegistrationExistsError(ValueError): | |||
|
|||
|
|||
class Registry(Generic[T]): | |||
def __init__(self): | |||
def __init__(self, enable_reverse_lookup=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth a docstring explaining that it's useful to disable reverse lookup when you have duplicate values
self.registrations: dict[str, T] = {} | ||
self.reverse_lookup: dict[T, str] = {} | ||
self.enable_reverse_lookup = enable_reverse_lookup | ||
|
||
def register(self, key: str): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Separately, it could also be worth changing the way we register so that we support both a decorator and a function. Not a huge priority though.
Codecov ReportAll modified and coverable lines are covered by tests ✅ ✅ All tests successful. No failed tests found. Additional details and impacted files@@ Coverage Diff @@
## master #82114 +/- ##
==========================================
- Coverage 80.37% 80.36% -0.01%
==========================================
Files 7277 7277
Lines 321488 321491 +3
Branches 20966 20966
==========================================
- Hits 258391 258382 -9
- Misses 62689 62701 +12
Partials 408 408 |
Make reverse lookup optional in
Registry
. Reverse lookup via a dictionary means keys AND values must be unique (because we also have regular lookup via a dictionary), but sometimes different keys have the same value and we want to allow registration if that's the case.