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

chore(utils): allow duplicate values in registry by making reverse lookup optional #82114

Merged
merged 3 commits into from
Dec 14, 2024

Conversation

cathteng
Copy link
Member

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.

@cathteng cathteng changed the title chore(utils): allow duplicate values in registry by disabling reverse lookup chore(utils): allow duplicate values in registry by making reverse lookup optional Dec 14, 2024
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Dec 14, 2024
@cathteng cathteng requested a review from a team December 14, 2024 00:08
@@ -15,9 +15,10 @@ class NoRegistrationExistsError(ValueError):


class Registry(Generic[T]):
def __init__(self):
def __init__(self, enable_reverse_lookup=True):
Copy link
Member

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):
Copy link
Member

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.

@cathteng cathteng merged commit 92e31b8 into master Dec 14, 2024
47 checks passed
@cathteng cathteng deleted the cathy/utils/duplicate-value-registry branch December 14, 2024 01:15
Copy link

codecov bot commented Dec 14, 2024

Codecov Report

All 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              

evanh pushed a commit that referenced this pull request Dec 17, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Dec 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants