mirror of
https://github.com/sprockets/sprockets-statsd.git
synced 2024-11-22 03:00:20 +00:00
Add some missing typing.
This doesn't completely appease pyright & mypy since I insist on calling the sprockets.http.Application methods when guarded by isinstance() checks.
This commit is contained in:
parent
a47f121f6d
commit
9054c729f5
2 changed files with 15 additions and 11 deletions
|
@ -67,6 +67,9 @@ class Timer:
|
||||||
twice in a row will result in a :exc:`RuntimeError` as well.
|
twice in a row will result in a :exc:`RuntimeError` as well.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
_start_time: typing.Union[None, float]
|
||||||
|
_finish_time: typing.Union[None, float]
|
||||||
|
|
||||||
def __init__(self, connector: 'AbstractConnector', path: str):
|
def __init__(self, connector: 'AbstractConnector', path: str):
|
||||||
self._connector = connector
|
self._connector = connector
|
||||||
self._path = path
|
self._path = path
|
||||||
|
@ -94,7 +97,7 @@ class Timer:
|
||||||
self.send()
|
self.send()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def send(self):
|
def send(self) -> None:
|
||||||
"""Send the recorded timing to the connector.
|
"""Send the recorded timing to the connector.
|
||||||
|
|
||||||
This method will raise a :exc:`RuntimeError` if a timing has
|
This method will raise a :exc:`RuntimeError` if a timing has
|
||||||
|
@ -112,13 +115,14 @@ class Timer:
|
||||||
max(self._finish_time, self._start_time) - self._start_time)
|
max(self._finish_time, self._start_time) - self._start_time)
|
||||||
self._start_time, self._finish_time = None, None
|
self._start_time, self._finish_time = None, None
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self) -> 'Timer':
|
||||||
self.start()
|
self.start()
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
def __exit__(self, exc_type: typing.Union[typing.Type[Exception], None],
|
||||||
|
exc_val: typing.Union[Exception, None],
|
||||||
|
exc_tb: typing.Union[typing.Tuple, None]) -> None:
|
||||||
self.stop()
|
self.stop()
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
class AbstractConnector:
|
class AbstractConnector:
|
||||||
|
@ -187,7 +191,7 @@ class AbstractConnector:
|
||||||
seconds = seconds.total_seconds()
|
seconds = seconds.total_seconds()
|
||||||
self.inject_metric(f'timers.{path}', str(seconds * 1000.0), 'ms')
|
self.inject_metric(f'timers.{path}', str(seconds * 1000.0), 'ms')
|
||||||
|
|
||||||
def timer(self, path) -> Timer:
|
def timer(self, path: str) -> Timer:
|
||||||
"""Send a timer metric using a context manager.
|
"""Send a timer metric using a context manager.
|
||||||
|
|
||||||
:param path: timer to append the measured time to
|
:param path: timer to append the measured time to
|
||||||
|
@ -482,7 +486,7 @@ class Processor:
|
||||||
|
|
||||||
logger: logging.Logger
|
logger: logging.Logger
|
||||||
protocol: typing.Optional[StatsdProtocol]
|
protocol: typing.Optional[StatsdProtocol]
|
||||||
queue: asyncio.Queue
|
queue: asyncio.Queue[bytes]
|
||||||
_create_transport: typing.Callable[[], typing.Coroutine[
|
_create_transport: typing.Callable[[], typing.Coroutine[
|
||||||
typing.Any, typing.Any, typing.Tuple[asyncio.BaseTransport,
|
typing.Any, typing.Any, typing.Tuple[asyncio.BaseTransport,
|
||||||
StatsdProtocol]]]
|
StatsdProtocol]]]
|
||||||
|
|
|
@ -121,7 +121,7 @@ class Application(web.Application):
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def start_statsd(self, *_) -> None:
|
async def start_statsd(self, *_: typing.Any) -> None:
|
||||||
"""Start the connector during startup.
|
"""Start the connector during startup.
|
||||||
|
|
||||||
Call this method during application startup to enable the statsd
|
Call this method during application startup to enable the statsd
|
||||||
|
@ -157,7 +157,7 @@ class Application(web.Application):
|
||||||
|
|
||||||
await self.statsd_connector.start()
|
await self.statsd_connector.start()
|
||||||
|
|
||||||
async def stop_statsd(self, *_) -> None:
|
async def stop_statsd(self, *_: typing.Any) -> None:
|
||||||
"""Stop the connector during shutdown.
|
"""Stop the connector during shutdown.
|
||||||
|
|
||||||
If the connector was started, then this method will gracefully
|
If the connector was started, then this method will gracefully
|
||||||
|
@ -171,7 +171,7 @@ class Application(web.Application):
|
||||||
|
|
||||||
def __handle_fatal_error(self,
|
def __handle_fatal_error(self,
|
||||||
message: str,
|
message: str,
|
||||||
exc: typing.Optional[Exception] = None):
|
exc: typing.Optional[Exception] = None) -> None:
|
||||||
logger = self.__get_logger()
|
logger = self.__get_logger()
|
||||||
if exc is not None:
|
if exc is not None:
|
||||||
logger.exception('%s', message)
|
logger.exception('%s', message)
|
||||||
|
@ -184,7 +184,7 @@ class Application(web.Application):
|
||||||
|
|
||||||
def __get_logger(self) -> logging.Logger:
|
def __get_logger(self) -> logging.Logger:
|
||||||
try:
|
try:
|
||||||
return getattr(self, 'logger')
|
return typing.cast(logging.Logger, getattr(self, 'logger'))
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return logging.getLogger(__package__).getChild(
|
return logging.getLogger(__package__).getChild(
|
||||||
'tornado.Application')
|
'tornado.Application')
|
||||||
|
@ -192,7 +192,7 @@ class Application(web.Application):
|
||||||
|
|
||||||
class RequestHandler(web.RequestHandler):
|
class RequestHandler(web.RequestHandler):
|
||||||
"""Mix this into your handler to send metrics to a statsd server."""
|
"""Mix this into your handler to send metrics to a statsd server."""
|
||||||
statsd_connector: typing.Optional[statsd.Connector]
|
statsd_connector: typing.Optional[statsd.AbstractConnector]
|
||||||
|
|
||||||
def initialize(self, **kwargs: typing.Any) -> None:
|
def initialize(self, **kwargs: typing.Any) -> None:
|
||||||
super().initialize(**kwargs)
|
super().initialize(**kwargs)
|
||||||
|
|
Loading…
Reference in a new issue