Skip to content
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

Add typings for Tracing #3343

Merged
merged 4 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/1749.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Add type hints to Application and Response
Add type hints to Exceptions
Upgrade mypy to 0.630
Add type hints to payload.py
Add type hints to tracing.py
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Dmitry Doroshev
Dmitry Lukashin
Dmitry Shamov
Dmitry Trofimov
Dmytro Bohomiakov
Dmytro Kuznetsov
Dustin J. Mitchell
Eduard Iskandarov
Expand Down
142 changes: 86 additions & 56 deletions aiohttp/tracing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from types import SimpleNamespace
from typing import TYPE_CHECKING, Awaitable, Callable, Type

import attr
from multidict import CIMultiDict
Expand All @@ -8,6 +9,11 @@
from .signals import Signal


if TYPE_CHECKING:
from .client import ClientSession
from .web_app import Application # noqa


__all__ = (
'TraceConfig', 'TraceRequestStartParams', 'TraceRequestEndParams',
'TraceRequestExceptionParams', 'TraceConnectionQueuedStartParams',
Expand All @@ -24,31 +30,37 @@ class TraceConfig:
"""First-class used to trace requests launched via ClientSession
objects."""

def __init__(self, trace_config_ctx_factory=SimpleNamespace):
self._on_request_start = Signal(self)
self._on_request_chunk_sent = Signal(self)
self._on_response_chunk_received = Signal(self)
self._on_request_end = Signal(self)
self._on_request_exception = Signal(self)
self._on_request_redirect = Signal(self)
self._on_connection_queued_start = Signal(self)
self._on_connection_queued_end = Signal(self)
self._on_connection_create_start = Signal(self)
self._on_connection_create_end = Signal(self)
self._on_connection_reuseconn = Signal(self)
self._on_dns_resolvehost_start = Signal(self)
self._on_dns_resolvehost_end = Signal(self)
self._on_dns_cache_hit = Signal(self)
self._on_dns_cache_miss = Signal(self)

self._trace_config_ctx_factory = trace_config_ctx_factory

def trace_config_ctx(self, trace_request_ctx=None):
def __init__(
self,
trace_config_ctx_factory: Type[SimpleNamespace]=SimpleNamespace
) -> None:
self._on_request_start = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe define Signal[Callable[['Application'], Awaitable[None]]] only once at top level and reuse it everywhere? You wouldn't need typing comments and qa disable in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored

self._on_request_chunk_sent = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_response_chunk_received = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_request_end = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_request_exception = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_request_redirect = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_connection_queued_start = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_connection_queued_end = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_connection_create_start = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_connection_create_end = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_connection_reuseconn = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_dns_resolvehost_start = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_dns_resolvehost_end = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_dns_cache_hit = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa
self._on_dns_cache_miss = Signal(self) # type: Signal[Callable[['Application'], Awaitable[None]]] # noqa

self._trace_config_ctx_factory = trace_config_ctx_factory # type: Type[SimpleNamespace] # noqa

def trace_config_ctx(
self,
trace_request_ctx: SimpleNamespace=None
) -> SimpleNamespace: # noqa
""" Return a new trace_config_ctx instance """
return self._trace_config_ctx_factory(
trace_request_ctx=trace_request_ctx)

def freeze(self):
def freeze(self) -> None:
self._on_request_start.freeze()
self._on_request_chunk_sent.freeze()
self._on_response_chunk_received.freeze()
Expand All @@ -66,63 +78,63 @@ def freeze(self):
self._on_dns_cache_miss.freeze()

@property
def on_request_start(self):
def on_request_start(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_request_start

@property
def on_request_chunk_sent(self):
def on_request_chunk_sent(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_request_chunk_sent

@property
def on_response_chunk_received(self):
def on_response_chunk_received(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_response_chunk_received

@property
def on_request_end(self):
def on_request_end(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_request_end

@property
def on_request_exception(self):
def on_request_exception(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_request_exception

@property
def on_request_redirect(self):
def on_request_redirect(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_request_redirect

@property
def on_connection_queued_start(self):
def on_connection_queued_start(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_connection_queued_start

@property
def on_connection_queued_end(self):
def on_connection_queued_end(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_connection_queued_end

@property
def on_connection_create_start(self):
def on_connection_create_start(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_connection_create_start

@property
def on_connection_create_end(self):
def on_connection_create_end(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_connection_create_end

@property
def on_connection_reuseconn(self):
def on_connection_reuseconn(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_connection_reuseconn

@property
def on_dns_resolvehost_start(self):
def on_dns_resolvehost_start(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_dns_resolvehost_start

@property
def on_dns_resolvehost_end(self):
def on_dns_resolvehost_end(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_dns_resolvehost_end

@property
def on_dns_cache_hit(self):
def on_dns_cache_hit(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_dns_cache_hit

@property
def on_dns_cache_miss(self):
def on_dns_cache_miss(self) -> 'Signal[Callable[["Application"], Awaitable[None]]]': # noqa
return self._on_dns_cache_miss


Expand All @@ -131,7 +143,7 @@ class TraceRequestStartParams:
""" Parameters sent by the `on_request_start` signal"""
method = attr.ib(type=str)
url = attr.ib(type=URL)
headers = attr.ib(type=CIMultiDict)
headers = attr.ib(type='CIMultiDict[str]')


@attr.s(frozen=True, slots=True)
Expand All @@ -151,7 +163,7 @@ class TraceRequestEndParams:
""" Parameters sent by the `on_request_end` signal"""
method = attr.ib(type=str)
url = attr.ib(type=URL)
headers = attr.ib(type=CIMultiDict)
headers = attr.ib(type='CIMultiDict[str]')
response = attr.ib(type=ClientResponse)


Expand All @@ -160,7 +172,7 @@ class TraceRequestExceptionParams:
""" Parameters sent by the `on_request_exception` signal"""
method = attr.ib(type=str)
url = attr.ib(type=URL)
headers = attr.ib(type=CIMultiDict)
headers = attr.ib(type='CIMultiDict[str]')
exception = attr.ib(type=Exception)


Expand All @@ -169,7 +181,7 @@ class TraceRequestRedirectParams:
""" Parameters sent by the `on_request_redirect` signal"""
method = attr.ib(type=str)
url = attr.ib(type=URL)
headers = attr.ib(type=CIMultiDict)
headers = attr.ib(type='CIMultiDict[str]')
response = attr.ib(type=ClientResponse)


Expand Down Expand Up @@ -226,110 +238,128 @@ class Trace:
""" Internal class used to keep together the main dependencies used
at the moment of send a signal."""

def __init__(self, session, trace_config, trace_config_ctx):
def __init__(self,
session: 'ClientSession',
trace_config: TraceConfig,
trace_config_ctx: SimpleNamespace) -> None:
self._trace_config = trace_config
self._trace_config_ctx = trace_config_ctx
self._session = session

async def send_request_start(self, method, url, headers):
async def send_request_start(self,
method: str,
url: URL,
headers: 'CIMultiDict[str]') -> None:
return await self._trace_config.on_request_start.send(
self._session,
self._trace_config_ctx,
TraceRequestStartParams(method, url, headers)
)

async def send_request_chunk_sent(self, chunk):
async def send_request_chunk_sent(self, chunk: bytes) -> None:
return await self._trace_config.on_request_chunk_sent.send(
self._session,
self._trace_config_ctx,
TraceRequestChunkSentParams(chunk)
)

async def send_response_chunk_received(self, chunk):
async def send_response_chunk_received(self, chunk: bytes) -> None:
return await self._trace_config.on_response_chunk_received.send(
self._session,
self._trace_config_ctx,
TraceResponseChunkReceivedParams(chunk)
)

async def send_request_end(self, method, url, headers, response):
async def send_request_end(self,
method: str,
url: URL,
headers: 'CIMultiDict[str]',
response: ClientResponse) -> None:
return await self._trace_config.on_request_end.send(
self._session,
self._trace_config_ctx,
TraceRequestEndParams(method, url, headers, response)
)

async def send_request_exception(self, method, url, headers, exception):
async def send_request_exception(self,
method: str,
url: URL,
headers: 'CIMultiDict[str]',
exception: Exception) -> None:
return await self._trace_config.on_request_exception.send(
self._session,
self._trace_config_ctx,
TraceRequestExceptionParams(method, url, headers, exception)
)

async def send_request_redirect(self, method, url, headers, response):
async def send_request_redirect(self,
method: str,
url: URL,
headers: 'CIMultiDict[str]',
response: ClientResponse) -> None:
return await self._trace_config._on_request_redirect.send(
self._session,
self._trace_config_ctx,
TraceRequestRedirectParams(method, url, headers, response)
)

async def send_connection_queued_start(self):
async def send_connection_queued_start(self) -> None:
return await self._trace_config.on_connection_queued_start.send(
self._session,
self._trace_config_ctx,
TraceConnectionQueuedStartParams()
)

async def send_connection_queued_end(self):
async def send_connection_queued_end(self) -> None:
return await self._trace_config.on_connection_queued_end.send(
self._session,
self._trace_config_ctx,
TraceConnectionQueuedEndParams()
)

async def send_connection_create_start(self):
async def send_connection_create_start(self) -> None:
return await self._trace_config.on_connection_create_start.send(
self._session,
self._trace_config_ctx,
TraceConnectionCreateStartParams()
)

async def send_connection_create_end(self):
async def send_connection_create_end(self) -> None:
return await self._trace_config.on_connection_create_end.send(
self._session,
self._trace_config_ctx,
TraceConnectionCreateEndParams()
)

async def send_connection_reuseconn(self):
async def send_connection_reuseconn(self) -> None:
return await self._trace_config.on_connection_reuseconn.send(
self._session,
self._trace_config_ctx,
TraceConnectionReuseconnParams()
)

async def send_dns_resolvehost_start(self, host):
async def send_dns_resolvehost_start(self, host: str) -> None:
return await self._trace_config.on_dns_resolvehost_start.send(
self._session,
self._trace_config_ctx,
TraceDnsResolveHostStartParams(host)
)

async def send_dns_resolvehost_end(self, host):
async def send_dns_resolvehost_end(self, host: str) -> None:
return await self._trace_config.on_dns_resolvehost_end.send(
self._session,
self._trace_config_ctx,
TraceDnsResolveHostEndParams(host)
)

async def send_dns_cache_hit(self, host):
async def send_dns_cache_hit(self, host: str) -> None:
return await self._trace_config.on_dns_cache_hit.send(
self._session,
self._trace_config_ctx,
TraceDnsCacheHitParams(host)
)

async def send_dns_cache_miss(self, host):
async def send_dns_cache_miss(self, host: str) -> None:
return await self._trace_config.on_dns_cache_miss.send(
self._session,
self._trace_config_ctx,
Expand Down