-
Notifications
You must be signed in to change notification settings - Fork 180
Type annotations #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Type annotations #151
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8be14b8
annotate params with defaults
orsinium 9e3e4a5
annotate return types
orsinium c7232dc
autopep8
orsinium 0e65b32
manually annotate everything
orsinium afb7cb0
add py.typed
orsinium 3ece35a
wrap long lines
orsinium ba59343
annotate delta
orsinium 6adab9e
add mypy and flake8 into tox
orsinium 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 hidden or 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
include AUTHORS CHANGES LICENSE MANIFEST.in README.rst | ||
include setup.py | ||
include statsd/py.typed | ||
recursive-include docs * |
This file contains hidden or 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mock==1.0.1 | ||
nose==1.2.1 | ||
flake8==1.7.0 | ||
flake8 | ||
mypy==0.910 | ||
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,4 +1,11 @@ | ||
from __future__ import absolute_import, division, unicode_literals | ||
|
||
from .stream import TCPStatsClient, UnixSocketStatsClient # noqa | ||
from .udp import StatsClient # noqa | ||
from .stream import TCPStatsClient, UnixSocketStatsClient | ||
from .udp import StatsClient | ||
|
||
|
||
__all__ = [ | ||
'TCPStatsClient', | ||
'UnixSocketStatsClient', | ||
'StatsClient', | ||
] |
This file contains hidden or 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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -3,27 +3,37 @@ | |||||||
import random | ||||||||
from collections import deque | ||||||||
from datetime import timedelta | ||||||||
from typing import Deque, Optional, TypeVar, Union | ||||||||
|
||||||||
from .timer import Timer | ||||||||
|
||||||||
|
||||||||
P = TypeVar('P', bound='PipelineBase') | ||||||||
|
||||||||
|
||||||||
class StatsClientBase(object): | ||||||||
"""A Base class for various statsd clients.""" | ||||||||
_prefix: Optional[str] | ||||||||
|
||||||||
def close(self): | ||||||||
def close(self) -> None: | ||||||||
"""Used to close and clean up any underlying resources.""" | ||||||||
raise NotImplementedError() | ||||||||
|
||||||||
def _send(self): | ||||||||
def _send(self) -> None: | ||||||||
raise NotImplementedError() | ||||||||
|
||||||||
def pipeline(self): | ||||||||
def pipeline(self) -> 'PipelineBase': | ||||||||
raise NotImplementedError() | ||||||||
|
||||||||
def timer(self, stat, rate=1): | ||||||||
def timer(self, stat: str, rate: float = 1) -> Timer: | ||||||||
return Timer(self, stat, rate) | ||||||||
|
||||||||
def timing(self, stat, delta, rate=1): | ||||||||
def timing( | ||||||||
self, | ||||||||
stat: str, | ||||||||
delta: Union[timedelta, float], | ||||||||
rate: float = 1, | ||||||||
) -> None: | ||||||||
""" | ||||||||
Send new timing information. | ||||||||
|
||||||||
|
@@ -34,15 +44,21 @@ def timing(self, stat, delta, rate=1): | |||||||
delta = delta.total_seconds() * 1000. | ||||||||
self._send_stat(stat, '%0.6f|ms' % delta, rate) | ||||||||
|
||||||||
def incr(self, stat, count=1, rate=1): | ||||||||
def incr(self, stat: str, count: int = 1, rate: float = 1) -> None: | ||||||||
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 might accept non-integer values, at least, there's a test for that case Line 152 in aed9504
Suggested change
|
||||||||
"""Increment a stat by `count`.""" | ||||||||
self._send_stat(stat, '%s|c' % count, rate) | ||||||||
|
||||||||
def decr(self, stat, count=1, rate=1): | ||||||||
def decr(self, stat: str, count: int = 1, rate: float = 1) -> None: | ||||||||
"""Decrement a stat by `count`.""" | ||||||||
self.incr(stat, -count, rate) | ||||||||
|
||||||||
def gauge(self, stat, value, rate=1, delta=False): | ||||||||
def gauge( | ||||||||
self, | ||||||||
stat: str, | ||||||||
value: float, | ||||||||
rate: float = 1, | ||||||||
delta: bool = False, | ||||||||
) -> None: | ||||||||
"""Set a gauge value.""" | ||||||||
if value < 0 and not delta: | ||||||||
if rate < 1: | ||||||||
|
@@ -55,53 +71,56 @@ def gauge(self, stat, value, rate=1, delta=False): | |||||||
prefix = '+' if delta and value >= 0 else '' | ||||||||
self._send_stat(stat, '%s%s|g' % (prefix, value), rate) | ||||||||
|
||||||||
def set(self, stat, value, rate=1): | ||||||||
def set(self, stat: str, value: str, rate: float = 1) -> None: | ||||||||
"""Set a set value.""" | ||||||||
self._send_stat(stat, '%s|s' % value, rate) | ||||||||
|
||||||||
def _send_stat(self, stat, value, rate): | ||||||||
def _send_stat(self, stat: str, value: str, rate: float) -> None: | ||||||||
self._after(self._prepare(stat, value, rate)) | ||||||||
|
||||||||
def _prepare(self, stat, value, rate): | ||||||||
def _prepare(self, stat: str, value: str, rate: float) -> Optional[str]: | ||||||||
if rate < 1: | ||||||||
if random.random() > rate: | ||||||||
return | ||||||||
return None | ||||||||
value = '%s|@%s' % (value, rate) | ||||||||
|
||||||||
if self._prefix: | ||||||||
stat = '%s.%s' % (self._prefix, stat) | ||||||||
|
||||||||
return '%s:%s' % (stat, value) | ||||||||
|
||||||||
def _after(self, data): | ||||||||
def _after(self, data: Optional[str]) -> None: | ||||||||
if data: | ||||||||
self._send(data) | ||||||||
|
||||||||
|
||||||||
class PipelineBase(StatsClientBase): | ||||||||
_prefix: Optional[str] | ||||||||
_stats: Deque[str] | ||||||||
_client: StatsClientBase | ||||||||
|
||||||||
def __init__(self, client): | ||||||||
def __init__(self, client: StatsClientBase) -> None: | ||||||||
self._client = client | ||||||||
self._prefix = client._prefix | ||||||||
self._stats = deque() | ||||||||
|
||||||||
def _send(self): | ||||||||
def _send(self) -> None: | ||||||||
raise NotImplementedError() | ||||||||
|
||||||||
def _after(self, data): | ||||||||
def _after(self, data: Optional[str]) -> None: | ||||||||
if data is not None: | ||||||||
self._stats.append(data) | ||||||||
|
||||||||
def __enter__(self): | ||||||||
def __enter__(self: P) -> P: | ||||||||
return self | ||||||||
|
||||||||
def __exit__(self, typ, value, tb): | ||||||||
def __exit__(self, *exc_info) -> None: | ||||||||
self.send() | ||||||||
|
||||||||
def send(self): | ||||||||
def send(self) -> None: | ||||||||
if not self._stats: | ||||||||
return | ||||||||
self._send() | ||||||||
|
||||||||
def pipeline(self): | ||||||||
def pipeline(self: P) -> P: | ||||||||
return self.__class__(self) |
This file contains hidden or 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 hidden or 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.
Upgrade?