This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
improve typing annotations in CachedCall #10450
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Update type annotations to work with forthcoming Twisted 21.7.0 release. |
This file contains 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 |
---|---|---|
|
@@ -11,9 +11,10 @@ | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import enum | ||
from typing import Awaitable, Callable, Generic, Optional, TypeVar, Union | ||
|
||
from twisted.internet import defer | ||
from twisted.internet.defer import Deferred | ||
from twisted.python.failure import Failure | ||
|
||
|
@@ -22,6 +23,10 @@ | |
TV = TypeVar("TV") | ||
|
||
|
||
class _Sentinel(enum.Enum): | ||
sentinel = object() | ||
|
||
|
||
class CachedCall(Generic[TV]): | ||
"""A wrapper for asynchronous calls whose results should be shared | ||
|
||
|
@@ -65,7 +70,7 @@ def __init__(self, f: Callable[[], Awaitable[TV]]): | |
""" | ||
self._callable: Optional[Callable[[], Awaitable[TV]]] = f | ||
self._deferred: Optional[Deferred] = None | ||
self._result: Union[None, Failure, TV] = None | ||
self._result: Union[_Sentinel, TV, Failure] = _Sentinel.sentinel | ||
|
||
async def get(self) -> TV: | ||
"""Kick off the call if necessary, and return the result""" | ||
|
@@ -78,8 +83,9 @@ async def get(self) -> TV: | |
self._callable = None | ||
|
||
# once the deferred completes, store the result. We cannot simply leave the | ||
# result in the deferred, since if it's a Failure, GCing the deferred | ||
# would then log a critical error about unhandled Failures. | ||
# result in the deferred, since `awaiting` a deferred destroys its result. | ||
# (Also, if it's a Failure, GCing the deferred would log a critical error | ||
# about unhandled Failures) | ||
def got_result(r): | ||
self._result = r | ||
|
||
|
@@ -92,13 +98,19 @@ def got_result(r): | |
# and any eventual exception may not be reported. | ||
|
||
# we can now await the deferred, and once it completes, return the result. | ||
await make_deferred_yieldable(self._deferred) | ||
|
||
# I *think* this is the easiest way to correctly raise a Failure without having | ||
# to gut-wrench into the implementation of Deferred. | ||
d = Deferred() | ||
d.callback(self._result) | ||
return await d | ||
if isinstance(self._result, _Sentinel): | ||
await make_deferred_yieldable(self._deferred) | ||
assert not isinstance(self._result, _Sentinel) | ||
|
||
if isinstance(self._result, Failure): | ||
# I *think* awaiting a failed Deferred is the easiest way to correctly raise | ||
# the right exception. | ||
d = defer.fail(self._result) | ||
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. Can we not use 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. yes 🤦 |
||
await d | ||
# the `await` should always raise, so this should be unreachable. | ||
raise AssertionError("unexpected return from await on failure") | ||
|
||
return self._result | ||
|
||
|
||
class RetryOnExceptionCachedCall(Generic[TV]): | ||
|
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.
Looks like
_result
is anUnion[_Sentinel, TV, Failure]
but its value is anobject
? Or am I misunderstanding something here?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.
I think you're missing something. Or I don't understand the question.
_result
starts out as_Sentinel.sentinel
, which (via the magic of Enums) is an instance of_Sentinel
. It is later set to either aTV
or aFailure
.