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:
Dave Shawley 2021-07-20 17:32:51 -04:00
parent a47f121f6d
commit 9054c729f5
No known key found for this signature in database
GPG key ID: F41A8A99298F8EED
2 changed files with 15 additions and 11 deletions

View file

@ -67,6 +67,9 @@ class Timer:
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):
self._connector = connector
self._path = path
@ -94,7 +97,7 @@ class Timer:
self.send()
return self
def send(self):
def send(self) -> None:
"""Send the recorded timing to the connector.
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)
self._start_time, self._finish_time = None, None
def __enter__(self):
def __enter__(self) -> 'Timer':
self.start()
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()
return False
class AbstractConnector:
@ -187,7 +191,7 @@ class AbstractConnector:
seconds = seconds.total_seconds()
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.
:param path: timer to append the measured time to
@ -482,7 +486,7 @@ class Processor:
logger: logging.Logger
protocol: typing.Optional[StatsdProtocol]
queue: asyncio.Queue
queue: asyncio.Queue[bytes]
_create_transport: typing.Callable[[], typing.Coroutine[
typing.Any, typing.Any, typing.Tuple[asyncio.BaseTransport,
StatsdProtocol]]]

View file

@ -121,7 +121,7 @@ class Application(web.Application):
except AttributeError:
pass
async def start_statsd(self, *_) -> None:
async def start_statsd(self, *_: typing.Any) -> None:
"""Start the connector during startup.
Call this method during application startup to enable the statsd
@ -157,7 +157,7 @@ class Application(web.Application):
await self.statsd_connector.start()
async def stop_statsd(self, *_) -> None:
async def stop_statsd(self, *_: typing.Any) -> None:
"""Stop the connector during shutdown.
If the connector was started, then this method will gracefully
@ -171,7 +171,7 @@ class Application(web.Application):
def __handle_fatal_error(self,
message: str,
exc: typing.Optional[Exception] = None):
exc: typing.Optional[Exception] = None) -> None:
logger = self.__get_logger()
if exc is not None:
logger.exception('%s', message)
@ -184,7 +184,7 @@ class Application(web.Application):
def __get_logger(self) -> logging.Logger:
try:
return getattr(self, 'logger')
return typing.cast(logging.Logger, getattr(self, 'logger'))
except AttributeError:
return logging.getLogger(__package__).getChild(
'tornado.Application')
@ -192,7 +192,7 @@ class Application(web.Application):
class RequestHandler(web.RequestHandler):
"""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:
super().initialize(**kwargs)