Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add missing type hints to synapse.logging.context #11556

Merged
merged 17 commits into from
Dec 14, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion synapse/logging/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,10 @@ def run_in_background(f, *args, **kwargs) -> defer.Deferred:
return res


def make_deferred_yieldable(deferred):
T = TypeVar("T")


def make_deferred_yieldable(deferred: "defer.Deferred[T]") -> "defer.Deferred[T]":
"""Given a deferred (or coroutine), make it follow the Synapse logcontext
rules:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Judging by the doctstring I'd've thought the annotation ought to be Union["defer.Deferred[T]", Awaitable[T]].

This might have cropped up in #10980 --- I remember something like this being painful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I'd entirely forgotten that there was a previous PR that tried to type hint these. I didn't mean to step on your toes!

Yes, the docstring contradicts the type hint. I wasn't sure what to do here. I can't find anywhere where we feed a coroutine to this function and I would remove the coroutine support, but it feels risky?

I could change it to accept an Awaitable, then I'd need another assert in the implementation, since we can't really handle non-coroutine, non-deferred awaitables.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to worry---I had let that one rot a little and I don't think I had as robust an understanding of what's going on then as you do now!

Maybe we can remove (or coroutine) from the docstring, if we're convinced that this always is given a Deferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've elected to remove the coroutine support, since mypy thinks it is unused, and I can't find any usage with coroutines manually.


Expand Down