Repository navigation
Add docstrings to FirstMatchRegistry #832
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
Open
JazzyJJ0601
wants to merge
1
commit into
getsentry:master
Choose a base branch
from
JazzyJJ0601:docs/registry-docstrings
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+41
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,19 +12,42 @@ | |
|
|
||
|
|
||
| class FirstMatchRegistry: | ||
| """Default registry: the first registered `Response` that matches the request is used. | ||
|
|
||
| Responses are checked in the order they were added. If more than one response | ||
| matches, the earliest match is used once and then removed, so later calls get | ||
| the next match; the last remaining match is reused for every further call. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| self._responses: List["BaseResponse"] = [] | ||
|
|
||
| @property | ||
| def registered(self) -> List["BaseResponse"]: | ||
| """List of the registered `Response` objects, in the order they were added.""" | ||
| return self._responses | ||
|
|
||
| def reset(self) -> None: | ||
| """Remove all registered responses.""" | ||
| self._responses = [] | ||
|
|
||
| def find( | ||
| self, request: "PreparedRequest" | ||
| ) -> Tuple[Optional["BaseResponse"], List[str]]: | ||
| """Find the first registered `Response` that matches the request. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| request : PreparedRequest | ||
| Request that was caught by the custom adapter. | ||
|
|
||
| Returns | ||
| ------- | ||
| Tuple[Optional["BaseResponse"], List[str]] | ||
| Matched `Response` object (or None) and the reasons why the other | ||
| registered responses did not match. | ||
|
|
||
| """ | ||
| found = None | ||
| found_match = None | ||
| match_failed_reasons = [] | ||
|
|
@@ -47,6 +70,12 @@ def find( | |
| return found_match, match_failed_reasons | ||
|
|
||
| def add(self, response: "BaseResponse") -> "BaseResponse": | ||
| """Register a response and return it. | ||
|
|
||
| If the same `Response` instance is already registered, a deep copy is | ||
| registered and returned instead, so the same object is never | ||
| registered twice. | ||
| """ | ||
| if any(response is resp for resp in self.registered): | ||
| # if user adds multiple responses that reference the same instance. | ||
| # do a comparison by memory allocation address. | ||
|
|
@@ -57,13 +86,25 @@ def add(self, response: "BaseResponse") -> "BaseResponse": | |
| return response | ||
|
|
||
| def remove(self, response: "BaseResponse") -> List["BaseResponse"]: | ||
| """Remove every registered response with the same method and URL as ``response``. | ||
|
|
||
| Returns the removed responses. | ||
| """ | ||
| removed_responses = [] | ||
| while response in self.registered: | ||
| self.registered.remove(response) | ||
| removed_responses.append(response) | ||
| return removed_responses | ||
|
|
||
| def replace(self, response: "BaseResponse") -> "BaseResponse": | ||
| """Replace the registered response with the same method and URL as ``response``. | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
| Raises | ||
| ------ | ||
| ValueError | ||
| If no response with that method and URL is registered. | ||
|
|
||
| """ | ||
| try: | ||
| index = self.registered.index(response) | ||
| except ValueError: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
python docstrings are RST shaped.