Skip to content

Commit 2dfdde4

Browse files
laydayasvetlov
authored andcommitted
Annotate all attrs members (#5115)
* 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.
1 parent 8d135d8 commit 2dfdde4

8 files changed

+97
-100
lines changed

CHANGES/5115.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added annotations to all attrs members.

aiohttp/client.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,20 @@
138138
SSLContext = object # type: ignore
139139

140140

141-
@attr.s(frozen=True, slots=True)
141+
@attr.s(auto_attribs=True, frozen=True, slots=True)
142142
class ClientTimeout:
143-
total = attr.ib(type=Optional[float], default=None)
144-
connect = attr.ib(type=Optional[float], default=None)
145-
sock_read = attr.ib(type=Optional[float], default=None)
146-
sock_connect = attr.ib(type=Optional[float], default=None)
147-
148-
# pool_queue_timeout = attr.ib(type=float, default=None)
149-
# dns_resolution_timeout = attr.ib(type=float, default=None)
150-
# socket_connect_timeout = attr.ib(type=float, default=None)
151-
# connection_acquiring_timeout = attr.ib(type=float, default=None)
152-
# new_connection_timeout = attr.ib(type=float, default=None)
153-
# http_header_timeout = attr.ib(type=float, default=None)
154-
# response_body_timeout = attr.ib(type=float, default=None)
143+
total: Optional[float] = None
144+
connect: Optional[float] = None
145+
sock_read: Optional[float] = None
146+
sock_connect: Optional[float] = None
147+
148+
# pool_queue_timeout: Optional[float] = None
149+
# dns_resolution_timeout: Optional[float] = None
150+
# socket_connect_timeout: Optional[float] = None
151+
# connection_acquiring_timeout: Optional[float] = None
152+
# new_connection_timeout: Optional[float] = None
153+
# http_header_timeout: Optional[float] = None
154+
# response_body_timeout: Optional[float] = None
155155

156156
# to create a timeout specific for a single request, either
157157
# - create a completely new one to overwrite the default

aiohttp/client_reqrep.py

+17-21
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,19 @@
8484
json_re = re.compile(r"^application/(?:[\w.+-]+?\+)?json")
8585

8686

87-
@attr.s(frozen=True, slots=True)
87+
@attr.s(auto_attribs=True, frozen=True, slots=True)
8888
class ContentDisposition:
89-
type = attr.ib(type=str) # type: Optional[str]
90-
parameters = attr.ib(
91-
type=MappingProxyType
92-
) # type: MappingProxyType[str, str] # noqa
93-
filename = attr.ib(type=str) # type: Optional[str]
89+
type: Optional[str]
90+
parameters: "MappingProxyType[str, str]"
91+
filename: Optional[str]
9492

9593

96-
@attr.s(frozen=True, slots=True)
94+
@attr.s(auto_attribs=True, frozen=True, slots=True)
9795
class RequestInfo:
98-
url = attr.ib(type=URL)
99-
method = attr.ib(type=str)
100-
headers = attr.ib(type=CIMultiDictProxy) # type: CIMultiDictProxy[str]
101-
real_url = attr.ib(type=URL)
96+
url: URL
97+
method: str
98+
headers: "CIMultiDictProxy[str]"
99+
real_url: URL = attr.ib()
102100

103101
@real_url.default
104102
def real_url_default(self) -> URL:
@@ -198,19 +196,17 @@ def _merge_ssl_params(
198196
return ssl
199197

200198

201-
@attr.s(slots=True, frozen=True)
199+
@attr.s(auto_attribs=True, slots=True, frozen=True)
202200
class ConnectionKey:
203201
# the key should contain an information about used proxy / TLS
204202
# to prevent reusing wrong connections from a pool
205-
host = attr.ib(type=str)
206-
port = attr.ib(type=int) # type: Optional[int]
207-
is_ssl = attr.ib(type=bool)
208-
ssl = attr.ib() # type: Union[SSLContext, None, bool, Fingerprint]
209-
proxy = attr.ib() # type: Optional[URL]
210-
proxy_auth = attr.ib() # type: Optional[BasicAuth]
211-
proxy_headers_hash = attr.ib(
212-
type=int
213-
) # type: Optional[int] # noqa # hash(CIMultiDict)
203+
host: str
204+
port: Optional[int]
205+
is_ssl: bool
206+
ssl: Union[SSLContext, None, bool, Fingerprint]
207+
proxy: Optional[URL]
208+
proxy_auth: Optional[BasicAuth]
209+
proxy_headers_hash: Optional[int] # hash(CIMultiDict)
214210

215211

216212
def _is_expected_content_type(

aiohttp/helpers.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ def netrc_from_env() -> Optional[netrc.netrc]:
236236
return None
237237

238238

239-
@attr.s(frozen=True, slots=True)
239+
@attr.s(auto_attribs=True, frozen=True, slots=True)
240240
class ProxyInfo:
241-
proxy = attr.ib(type=URL)
242-
proxy_auth = attr.ib(type=Optional[BasicAuth])
241+
proxy: URL
242+
proxy_auth: Optional[BasicAuth]
243243

244244

245245
def proxies_from_env() -> Dict[str, ProxyInfo]:
@@ -302,12 +302,12 @@ def isasyncgenfunction(obj: Any) -> bool:
302302
return False
303303

304304

305-
@attr.s(frozen=True, slots=True)
305+
@attr.s(auto_attribs=True, frozen=True, slots=True)
306306
class MimeType:
307-
type = attr.ib(type=str)
308-
subtype = attr.ib(type=str)
309-
suffix = attr.ib(type=str)
310-
parameters = attr.ib(type=MultiDictProxy) # type: MultiDictProxy[str]
307+
type: str
308+
subtype: str
309+
suffix: str
310+
parameters: "MultiDictProxy[str]"
311311

312312

313313
@functools.lru_cache(maxsize=56)

aiohttp/tracing.py

+40-40
Original file line numberDiff line numberDiff line change
@@ -206,114 +206,114 @@ def on_dns_cache_miss(self) -> "Signal[_SignalCallback[TraceDnsCacheMissParams]]
206206
return self._on_dns_cache_miss
207207

208208

209-
@attr.s(frozen=True, slots=True)
209+
@attr.s(auto_attribs=True, frozen=True, slots=True)
210210
class TraceRequestStartParams:
211211
""" Parameters sent by the `on_request_start` signal"""
212212

213-
method = attr.ib(type=str)
214-
url = attr.ib(type=URL)
215-
headers = attr.ib(type="CIMultiDict[str]")
213+
method: str
214+
url: URL
215+
headers: "CIMultiDict[str]"
216216

217217

218-
@attr.s(frozen=True, slots=True)
218+
@attr.s(auto_attribs=True, frozen=True, slots=True)
219219
class TraceRequestChunkSentParams:
220220
""" Parameters sent by the `on_request_chunk_sent` signal"""
221221

222-
method = attr.ib(type=str)
223-
url = attr.ib(type=URL)
224-
chunk = attr.ib(type=bytes)
222+
method: str
223+
url: URL
224+
chunk: bytes
225225

226226

227-
@attr.s(frozen=True, slots=True)
227+
@attr.s(auto_attribs=True, frozen=True, slots=True)
228228
class TraceResponseChunkReceivedParams:
229229
""" Parameters sent by the `on_response_chunk_received` signal"""
230230

231-
method = attr.ib(type=str)
232-
url = attr.ib(type=URL)
233-
chunk = attr.ib(type=bytes)
231+
method: str
232+
url: URL
233+
chunk: bytes
234234

235235

236-
@attr.s(frozen=True, slots=True)
236+
@attr.s(auto_attribs=True, frozen=True, slots=True)
237237
class TraceRequestEndParams:
238238
""" Parameters sent by the `on_request_end` signal"""
239239

240-
method = attr.ib(type=str)
241-
url = attr.ib(type=URL)
242-
headers = attr.ib(type="CIMultiDict[str]")
243-
response = attr.ib(type=ClientResponse)
240+
method: str
241+
url: URL
242+
headers: "CIMultiDict[str]"
243+
response: ClientResponse
244244

245245

246-
@attr.s(frozen=True, slots=True)
246+
@attr.s(auto_attribs=True, frozen=True, slots=True)
247247
class TraceRequestExceptionParams:
248248
""" Parameters sent by the `on_request_exception` signal"""
249249

250-
method = attr.ib(type=str)
251-
url = attr.ib(type=URL)
252-
headers = attr.ib(type="CIMultiDict[str]")
253-
exception = attr.ib(type=BaseException)
250+
method: str
251+
url: URL
252+
headers: "CIMultiDict[str]"
253+
exception: BaseException
254254

255255

256-
@attr.s(frozen=True, slots=True)
256+
@attr.s(auto_attribs=True, frozen=True, slots=True)
257257
class TraceRequestRedirectParams:
258258
""" Parameters sent by the `on_request_redirect` signal"""
259259

260-
method = attr.ib(type=str)
261-
url = attr.ib(type=URL)
262-
headers = attr.ib(type="CIMultiDict[str]")
263-
response = attr.ib(type=ClientResponse)
260+
method: str
261+
url: URL
262+
headers: "CIMultiDict[str]"
263+
response: ClientResponse
264264

265265

266-
@attr.s(frozen=True, slots=True)
266+
@attr.s(auto_attribs=True, frozen=True, slots=True)
267267
class TraceConnectionQueuedStartParams:
268268
""" Parameters sent by the `on_connection_queued_start` signal"""
269269

270270

271-
@attr.s(frozen=True, slots=True)
271+
@attr.s(auto_attribs=True, frozen=True, slots=True)
272272
class TraceConnectionQueuedEndParams:
273273
""" Parameters sent by the `on_connection_queued_end` signal"""
274274

275275

276-
@attr.s(frozen=True, slots=True)
276+
@attr.s(auto_attribs=True, frozen=True, slots=True)
277277
class TraceConnectionCreateStartParams:
278278
""" Parameters sent by the `on_connection_create_start` signal"""
279279

280280

281-
@attr.s(frozen=True, slots=True)
281+
@attr.s(auto_attribs=True, frozen=True, slots=True)
282282
class TraceConnectionCreateEndParams:
283283
""" Parameters sent by the `on_connection_create_end` signal"""
284284

285285

286-
@attr.s(frozen=True, slots=True)
286+
@attr.s(auto_attribs=True, frozen=True, slots=True)
287287
class TraceConnectionReuseconnParams:
288288
""" Parameters sent by the `on_connection_reuseconn` signal"""
289289

290290

291-
@attr.s(frozen=True, slots=True)
291+
@attr.s(auto_attribs=True, frozen=True, slots=True)
292292
class TraceDnsResolveHostStartParams:
293293
""" Parameters sent by the `on_dns_resolvehost_start` signal"""
294294

295-
host = attr.ib(type=str)
295+
host: str
296296

297297

298-
@attr.s(frozen=True, slots=True)
298+
@attr.s(auto_attribs=True, frozen=True, slots=True)
299299
class TraceDnsResolveHostEndParams:
300300
""" Parameters sent by the `on_dns_resolvehost_end` signal"""
301301

302-
host = attr.ib(type=str)
302+
host: str
303303

304304

305-
@attr.s(frozen=True, slots=True)
305+
@attr.s(auto_attribs=True, frozen=True, slots=True)
306306
class TraceDnsCacheHitParams:
307307
""" Parameters sent by the `on_dns_cache_hit` signal"""
308308

309-
host = attr.ib(type=str)
309+
host: str
310310

311311

312-
@attr.s(frozen=True, slots=True)
312+
@attr.s(auto_attribs=True, frozen=True, slots=True)
313313
class TraceDnsCacheMissParams:
314314
""" Parameters sent by the `on_dns_cache_miss` signal"""
315315

316-
host = attr.ib(type=str)
316+
host: str
317317

318318

319319
class Trace:

aiohttp/web_request.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
from .web_urldispatcher import UrlMappingMatchInfo # noqa
5555

5656

57-
@attr.s(frozen=True, slots=True)
57+
@attr.s(auto_attribs=True, frozen=True, slots=True)
5858
class FileField:
59-
name = attr.ib(type=str)
60-
filename = attr.ib(type=str)
61-
file = attr.ib(type=io.BufferedReader)
62-
content_type = attr.ib(type=str)
63-
headers = attr.ib(type=CIMultiDictProxy) # type: CIMultiDictProxy[str]
59+
name: str
60+
filename: str
61+
file: io.BufferedReader
62+
content_type: str
63+
headers: "CIMultiDictProxy[str]"
6464

6565

6666
_TCHAR = string.digits + string.ascii_letters + r"!#$%&'*+.^_`|~-"

aiohttp/web_routedef.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ def register(self, router: UrlDispatcher) -> List[AbstractRoute]:
5757
_HandlerType = Union[Type[AbstractView], _SimpleHandler]
5858

5959

60-
@attr.s(frozen=True, repr=False, slots=True)
60+
@attr.s(auto_attribs=True, frozen=True, repr=False, slots=True)
6161
class RouteDef(AbstractRouteDef):
62-
method = attr.ib(type=str)
63-
path = attr.ib(type=str)
64-
handler = attr.ib() # type: _HandlerType
65-
kwargs = attr.ib(type=Dict[str, Any])
62+
method: str
63+
path: str
64+
handler: _HandlerType
65+
kwargs: Dict[str, Any]
6666

6767
def __repr__(self) -> str:
6868
info = []
@@ -82,11 +82,11 @@ def register(self, router: UrlDispatcher) -> List[AbstractRoute]:
8282
]
8383

8484

85-
@attr.s(frozen=True, repr=False, slots=True)
85+
@attr.s(auto_attribs=True, frozen=True, repr=False, slots=True)
8686
class StaticDef(AbstractRouteDef):
87-
prefix = attr.ib(type=str)
88-
path = attr.ib() # type: PathLike
89-
kwargs = attr.ib(type=Dict[str, Any])
87+
prefix: str
88+
path: PathLike
89+
kwargs: Dict[str, Any]
9090

9191
def __repr__(self) -> str:
9292
info = []

aiohttp/web_ws.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
THRESHOLD_CONNLOST_ACCESS = 5
4141

4242

43-
@attr.s(frozen=True, slots=True)
43+
@attr.s(auto_attribs=True, frozen=True, slots=True)
4444
class WebSocketReady:
45-
ok = attr.ib(type=bool)
46-
protocol = attr.ib(type=Optional[str])
45+
ok: bool
46+
protocol: Optional[str]
4747

4848
def __bool__(self) -> bool:
4949
return self.ok

0 commit comments

Comments
 (0)