This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
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.
Add type annotations to
synapse.metrics
#10847Add type annotations to
synapse.metrics
#10847Changes from 8 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
There are no files selected for viewing
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 would think this needs to be a
Generator
, but maybe not?This is true for a variety of spots we use
yield
.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.
(Or maybe I can significantly simplify type hints in other spots!)
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 opted for using the most basic type that callers want, to minimize assumptions, rather than documenting the concrete return type.
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.
All Generators are Iterators (and all Iterators are Iterable), and in this case this seems cleaner than saying a
Generator[Metric, None, None]
.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.
is this common practice in our code?
Is it possible/useful to create a Protocol for it (if we have no other choice)?
Right now,
Any
doesn't tell mypy (or me, or my IDE) a whole lot, though I do appreciate that you've taken the time to common it out into a common label :).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.
ah I see the fields are created dynamically. Makes sense, even if a bit sad.
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'm taking a look at adding more typing to
synapse.utils
and got stuck on annotatingmetrics
here. I think it should be one of these dynamically-created types but can't annotate it meaningfully.So +1 to any other smart approaches!
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 typings are really frustrating. The equivalent in typescript would be something like: this
Perhaps we can add a type parameter to
InFlightGauge
and make the calling code provide an appropriateProtocol
?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.
InFlightGauge
now takes a type parameter, so now those callbacks can have proper type annotations.mypy forced me to annotate
in_flight
in synapse/util/metrics.py (sorry!).Two caveats:
Protocol
InFlightGauge
constructor match the attributes in the protocolThere 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.
nice
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.
could we come up with something a bit stronger than
Any
here?Union[float, str]
(if that's what is needed)?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.
It's
typeof(Sample.value)
and I had a really hard time figuring out what that was, sinceprometheus_client
doesn't have typings.Though it seems that
prometheus_client
pinky promises that it's anint
orfloat
:https://github.com/prometheus/client_python/blob/master/prometheus_client/samples.py#L35-L39
I shall update the function!