Skip to content

Commit

Permalink
Annotate all attrs members (#5115)
Browse files Browse the repository at this point in the history
* Annotate all attrs members

In py.typed packages, public attributes must be explicitly typed.
This is not an issue for mypy because the attrs mypy plug-in is able
to infer the types of attrs dynamically.  However, in other
static type checkers, like Pyright and pytype, the type of
unannotated attrs is not known statically.

Since aiohttp no longer supports Python versions older than 3.6
I've taken the liberty of converting the annotations to the native
format.
  • Loading branch information
layday authored and asvetlov committed Oct 24, 2020
1 parent 8d135d8 commit 2dfdde4
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 100 deletions.
1 change: 1 addition & 0 deletions CHANGES/5115.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added annotations to all attrs members.
26 changes: 13 additions & 13 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,20 @@
SSLContext = object # type: ignore


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class ClientTimeout:
total = attr.ib(type=Optional[float], default=None)
connect = attr.ib(type=Optional[float], default=None)
sock_read = attr.ib(type=Optional[float], default=None)
sock_connect = attr.ib(type=Optional[float], default=None)

# pool_queue_timeout = attr.ib(type=float, default=None)
# dns_resolution_timeout = attr.ib(type=float, default=None)
# socket_connect_timeout = attr.ib(type=float, default=None)
# connection_acquiring_timeout = attr.ib(type=float, default=None)
# new_connection_timeout = attr.ib(type=float, default=None)
# http_header_timeout = attr.ib(type=float, default=None)
# response_body_timeout = attr.ib(type=float, default=None)
total: Optional[float] = None
connect: Optional[float] = None
sock_read: Optional[float] = None
sock_connect: Optional[float] = None

# pool_queue_timeout: Optional[float] = None
# dns_resolution_timeout: Optional[float] = None
# socket_connect_timeout: Optional[float] = None
# connection_acquiring_timeout: Optional[float] = None
# new_connection_timeout: Optional[float] = None
# http_header_timeout: Optional[float] = None
# response_body_timeout: Optional[float] = None

# to create a timeout specific for a single request, either
# - create a completely new one to overwrite the default
Expand Down
38 changes: 17 additions & 21 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,19 @@
json_re = re.compile(r"^application/(?:[\w.+-]+?\+)?json")


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class ContentDisposition:
type = attr.ib(type=str) # type: Optional[str]
parameters = attr.ib(
type=MappingProxyType
) # type: MappingProxyType[str, str] # noqa
filename = attr.ib(type=str) # type: Optional[str]
type: Optional[str]
parameters: "MappingProxyType[str, str]"
filename: Optional[str]


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class RequestInfo:
url = attr.ib(type=URL)
method = attr.ib(type=str)
headers = attr.ib(type=CIMultiDictProxy) # type: CIMultiDictProxy[str]
real_url = attr.ib(type=URL)
url: URL
method: str
headers: "CIMultiDictProxy[str]"
real_url: URL = attr.ib()

@real_url.default
def real_url_default(self) -> URL:
Expand Down Expand Up @@ -198,19 +196,17 @@ def _merge_ssl_params(
return ssl


@attr.s(slots=True, frozen=True)
@attr.s(auto_attribs=True, slots=True, frozen=True)
class ConnectionKey:
# the key should contain an information about used proxy / TLS
# to prevent reusing wrong connections from a pool
host = attr.ib(type=str)
port = attr.ib(type=int) # type: Optional[int]
is_ssl = attr.ib(type=bool)
ssl = attr.ib() # type: Union[SSLContext, None, bool, Fingerprint]
proxy = attr.ib() # type: Optional[URL]
proxy_auth = attr.ib() # type: Optional[BasicAuth]
proxy_headers_hash = attr.ib(
type=int
) # type: Optional[int] # noqa # hash(CIMultiDict)
host: str
port: Optional[int]
is_ssl: bool
ssl: Union[SSLContext, None, bool, Fingerprint]
proxy: Optional[URL]
proxy_auth: Optional[BasicAuth]
proxy_headers_hash: Optional[int] # hash(CIMultiDict)


def _is_expected_content_type(
Expand Down
16 changes: 8 additions & 8 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ def netrc_from_env() -> Optional[netrc.netrc]:
return None


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class ProxyInfo:
proxy = attr.ib(type=URL)
proxy_auth = attr.ib(type=Optional[BasicAuth])
proxy: URL
proxy_auth: Optional[BasicAuth]


def proxies_from_env() -> Dict[str, ProxyInfo]:
Expand Down Expand Up @@ -302,12 +302,12 @@ def isasyncgenfunction(obj: Any) -> bool:
return False


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class MimeType:
type = attr.ib(type=str)
subtype = attr.ib(type=str)
suffix = attr.ib(type=str)
parameters = attr.ib(type=MultiDictProxy) # type: MultiDictProxy[str]
type: str
subtype: str
suffix: str
parameters: "MultiDictProxy[str]"


@functools.lru_cache(maxsize=56)
Expand Down
80 changes: 40 additions & 40 deletions aiohttp/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,114 +206,114 @@ def on_dns_cache_miss(self) -> "Signal[_SignalCallback[TraceDnsCacheMissParams]]
return self._on_dns_cache_miss


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
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[str]")
method: str
url: URL
headers: "CIMultiDict[str]"


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class TraceRequestChunkSentParams:
""" Parameters sent by the `on_request_chunk_sent` signal"""

method = attr.ib(type=str)
url = attr.ib(type=URL)
chunk = attr.ib(type=bytes)
method: str
url: URL
chunk: bytes


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class TraceResponseChunkReceivedParams:
""" Parameters sent by the `on_response_chunk_received` signal"""

method = attr.ib(type=str)
url = attr.ib(type=URL)
chunk = attr.ib(type=bytes)
method: str
url: URL
chunk: bytes


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
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[str]")
response = attr.ib(type=ClientResponse)
method: str
url: URL
headers: "CIMultiDict[str]"
response: ClientResponse


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
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[str]")
exception = attr.ib(type=BaseException)
method: str
url: URL
headers: "CIMultiDict[str]"
exception: BaseException


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
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[str]")
response = attr.ib(type=ClientResponse)
method: str
url: URL
headers: "CIMultiDict[str]"
response: ClientResponse


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class TraceConnectionQueuedStartParams:
""" Parameters sent by the `on_connection_queued_start` signal"""


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class TraceConnectionQueuedEndParams:
""" Parameters sent by the `on_connection_queued_end` signal"""


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class TraceConnectionCreateStartParams:
""" Parameters sent by the `on_connection_create_start` signal"""


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class TraceConnectionCreateEndParams:
""" Parameters sent by the `on_connection_create_end` signal"""


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class TraceConnectionReuseconnParams:
""" Parameters sent by the `on_connection_reuseconn` signal"""


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class TraceDnsResolveHostStartParams:
""" Parameters sent by the `on_dns_resolvehost_start` signal"""

host = attr.ib(type=str)
host: str


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class TraceDnsResolveHostEndParams:
""" Parameters sent by the `on_dns_resolvehost_end` signal"""

host = attr.ib(type=str)
host: str


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class TraceDnsCacheHitParams:
""" Parameters sent by the `on_dns_cache_hit` signal"""

host = attr.ib(type=str)
host: str


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class TraceDnsCacheMissParams:
""" Parameters sent by the `on_dns_cache_miss` signal"""

host = attr.ib(type=str)
host: str


class Trace:
Expand Down
12 changes: 6 additions & 6 deletions aiohttp/web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
from .web_urldispatcher import UrlMappingMatchInfo # noqa


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class FileField:
name = attr.ib(type=str)
filename = attr.ib(type=str)
file = attr.ib(type=io.BufferedReader)
content_type = attr.ib(type=str)
headers = attr.ib(type=CIMultiDictProxy) # type: CIMultiDictProxy[str]
name: str
filename: str
file: io.BufferedReader
content_type: str
headers: "CIMultiDictProxy[str]"


_TCHAR = string.digits + string.ascii_letters + r"!#$%&'*+.^_`|~-"
Expand Down
18 changes: 9 additions & 9 deletions aiohttp/web_routedef.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ def register(self, router: UrlDispatcher) -> List[AbstractRoute]:
_HandlerType = Union[Type[AbstractView], _SimpleHandler]


@attr.s(frozen=True, repr=False, slots=True)
@attr.s(auto_attribs=True, frozen=True, repr=False, slots=True)
class RouteDef(AbstractRouteDef):
method = attr.ib(type=str)
path = attr.ib(type=str)
handler = attr.ib() # type: _HandlerType
kwargs = attr.ib(type=Dict[str, Any])
method: str
path: str
handler: _HandlerType
kwargs: Dict[str, Any]

def __repr__(self) -> str:
info = []
Expand All @@ -82,11 +82,11 @@ def register(self, router: UrlDispatcher) -> List[AbstractRoute]:
]


@attr.s(frozen=True, repr=False, slots=True)
@attr.s(auto_attribs=True, frozen=True, repr=False, slots=True)
class StaticDef(AbstractRouteDef):
prefix = attr.ib(type=str)
path = attr.ib() # type: PathLike
kwargs = attr.ib(type=Dict[str, Any])
prefix: str
path: PathLike
kwargs: Dict[str, Any]

def __repr__(self) -> str:
info = []
Expand Down
6 changes: 3 additions & 3 deletions aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
THRESHOLD_CONNLOST_ACCESS = 5


@attr.s(frozen=True, slots=True)
@attr.s(auto_attribs=True, frozen=True, slots=True)
class WebSocketReady:
ok = attr.ib(type=bool)
protocol = attr.ib(type=Optional[str])
ok: bool
protocol: Optional[str]

def __bool__(self) -> bool:
return self.ok
Expand Down

0 comments on commit 2dfdde4

Please sign in to comment.