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
Prefer make_awaitable
over defer.succeed
in tests
#12505
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2ff6425
Support `DoneAwaitable` and `make_awaitable` in `run_in_background`
d56d118
Prefer `make_awaitable` over `defer.succeed` in tests
301bfe3
Prefer `make_awaitable` over `defer.succeed` in `tests/rest/client/te…
9718ba7
Add newsfile
180ab40
Appease mypy
629ce1b
Reword comments and reduce indent
ba28da5
run linter
daa0e9a
refactor run_in_background
8ffb491
Move `awaiter` function to top-level
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 @@ | ||
Use `make_awaitable` instead of `defer.succeed` for return values of mocks in tests. |
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 |
---|---|---|
|
@@ -808,11 +808,22 @@ def run_in_background( # type: ignore[misc] | |
# At this point we should have a Deferred, if not then f was a synchronous | ||
# function, wrap it in a Deferred for consistency. | ||
if not isinstance(res, defer.Deferred): | ||
# `res` is not a `Deferred` and not a `Coroutine`. | ||
# There are no other types of `Awaitable`s we expect to encounter in Synapse. | ||
assert not isinstance(res, Awaitable) | ||
|
||
return defer.succeed(res) | ||
if isinstance(res, Awaitable): | ||
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 flip this condition to reduce nesting? if not isinstance(res, Awaitable):
# `f` returned a plain value.
return defer.succeed(res)
# now handle the completed awaitable case 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. Done |
||
# `f` returned some kind of awaitable that is not a coroutine or `Deferred`. | ||
# We assume that it is a completed awaitable, such as a `DoneAwaitable` or | ||
# `Future` from `make_awaitable`, and await it manually. | ||
iterator = res.__await__() # `__await__` returns an iterator... | ||
try: | ||
next(iterator) | ||
raise ValueError( | ||
f"Function {f} returned an unresolved awaitable: {res}" | ||
) | ||
except StopIteration as e: | ||
# ...which raises a `StopIteration` once the awaitable is complete. | ||
return defer.succeed(e.value) | ||
else: | ||
# `f` returned a plain value. | ||
return defer.succeed(res) | ||
|
||
if res.called and not res.paused: | ||
# The function should have maintained the logcontext, so we can | ||
|
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
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
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
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
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
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
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
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
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
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.
this comment (apart from being horrifying grammar) seems to be a bit outdated?
(I also wonder if we really need to support synchronous functions here these days. if not we can simplify all this with an
assert isinstance(res, Awaitable)
)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'll come up with some new words here.
Ditching support for synchronous functions is a good idea, but:
run_in_background
is part of the module API so it's a little naughty to change it. However, I unknowingly did something similar tomake_deferred_yieldable
in Add missing type hints tosynapse.logging.context
#11556 by a while back and nobody's complained to my knowledge. So maybe that's fine?run_in_background
is called bypreserve_fn
, which also accepts synchronous functions.preserve_fn
's used by@cached
and@cachedList
to wrap methods, some of which are still synchronous. I think restricting those decorators to async functions is going to turn out to be a rabbit hole.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.
ok, fair. In general,
@cached
and co seem to be overkill for a synchronous function and we should just use@lru_cache
or something instead - but agreed this is a rabbithole we shouldn't get into right now.