-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add type annotations to synapse.metrics
#10847
Changes from 5 commits
66d63c0
934fe41
f058e4f
dbe4345
949439c
d55eb93
ab66469
d1ee5da
6924ce8
7ab1153
77120c2
dba3f61
b483e31
dd276d1
055dbde
b7d099b
6c2f682
53ff8f5
550d1bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -203,6 +203,9 @@ def run_as_background_process( # type: ignore[misc] | |
... | ||
# The `type: ignore[misc]` above suppresses | ||
# "error: Overloaded function signatures 1 and 2 overlap with incompatible return types [misc]" | ||
# | ||
# Overloads are used instead of a `Union` here because mypy fails to infer the type of `R` | ||
# and complains about almost every call to this function when `func` is a `Union`. | ||
|
||
|
||
@overload | ||
|
@@ -287,19 +290,19 @@ async def run() -> Optional[R]: | |
F = TypeVar("F", bound=Callable[..., Any]) | ||
|
||
|
||
# NB: Return type is incorrect and should be a callable returning an F with a | ||
# Deferred[Optional[R]] return, which we can't express correctly until Python 3.10. | ||
def wrap_as_background_process(desc: str) -> Callable[[F], F]: | ||
"""Decorator that wraps a function that gets called as a background | ||
process. | ||
|
||
Equivalent to calling the function with `run_as_background_process`. | ||
|
||
Note that `run_as_background_process` changes the return type into | ||
Note that the annotated return type of this function is incorrect. | ||
The return type of the function, once wrapped, is actually a | ||
`Deferred[Optional[T]]`. | ||
""" | ||
|
||
# NB: Return type is incorrect and should be F with a Deferred[Optional[R]] return | ||
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. I think this should work, but maybe not? This makes me a bit nervous that we're not mentioning this returns a 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. Not sure what to do about this. I updated the docstring above to call the change of return type out. We can either annotate the return type correctly and lose the types of the arguments for the decorated function, 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. I feel kind of bad about lying but appreciate the dilemma. :/ I'm not really sure what's best, though. Is lying going to cause type errors for users of the function? I would probably make the docstring louder about the return type being a lie, though. 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. I tried to improve the docstring further 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. I think it's fine as - is. question though, what happens if you bound
(or something similar?) 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.
to be clear: I'm saying I'm happy with the PR as it stands. 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.
Ooh. mypy throws up 3 errors which all look trivial to fix:
So it sounds like the cleanest thing to do is to restrict 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. I've gotten rid of the |
||
# We can't expression the return types correctly until Python 3.10. | ||
DMRobertson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def wrap_as_background_process_inner(func: F) -> F: | ||
@wraps(func) | ||
def wrap_as_background_process_inner_2( | ||
|
Uh oh!
There was an error while loading. Please reload this page.