Skip to content

Commit 89aaa0f

Browse files
committed
Stop using typing's Dict, List, Tuple for type annotations
We can now use plain `dict`, `list` and `tuple`.
1 parent cbde38d commit 89aaa0f

25 files changed

+213
-226
lines changed

examples/doq_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import asyncio
33
import logging
44
import struct
5-
from typing import Dict, Optional
5+
from typing import Optional
66

77
from aioquic.asyncio import QuicConnectionProtocol, serve
88
from aioquic.quic.configuration import QuicConfiguration
@@ -33,7 +33,7 @@ class SessionTicketStore:
3333
"""
3434

3535
def __init__(self) -> None:
36-
self.tickets: Dict[bytes, SessionTicket] = {}
36+
self.tickets: dict[bytes, SessionTicket] = {}
3737

3838
def add(self, ticket: SessionTicket) -> None:
3939
self.tickets[ticket.ticket] = ticket

examples/http3_client.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import ssl
77
import time
88
from collections import deque
9-
from typing import BinaryIO, Callable, Deque, Dict, List, Optional, Union, cast
9+
from typing import BinaryIO, Callable, Deque, Optional, Union, cast
1010
from urllib.parse import urlparse
1111

1212
import aioquic
@@ -57,7 +57,7 @@ def __init__(
5757
method: str,
5858
url: URL,
5959
content: bytes = b"",
60-
headers: Optional[Dict] = None,
60+
headers: Optional[dict] = None,
6161
) -> None:
6262
if headers is None:
6363
headers = {}
@@ -125,18 +125,18 @@ class HttpClient(QuicConnectionProtocol):
125125
def __init__(self, *args, **kwargs) -> None:
126126
super().__init__(*args, **kwargs)
127127

128-
self.pushes: Dict[int, Deque[H3Event]] = {}
128+
self.pushes: dict[int, Deque[H3Event]] = {}
129129
self._http: Optional[HttpConnection] = None
130-
self._request_events: Dict[int, Deque[H3Event]] = {}
131-
self._request_waiter: Dict[int, asyncio.Future[Deque[H3Event]]] = {}
132-
self._websockets: Dict[int, WebSocket] = {}
130+
self._request_events: dict[int, Deque[H3Event]] = {}
131+
self._request_waiter: dict[int, asyncio.Future[Deque[H3Event]]] = {}
132+
self._websockets: dict[int, WebSocket] = {}
133133

134134
if self._quic.configuration.alpn_protocols[0].startswith("hq-"):
135135
self._http = H0Connection(self._quic)
136136
else:
137137
self._http = H3Connection(self._quic)
138138

139-
async def get(self, url: str, headers: Optional[Dict] = None) -> Deque[H3Event]:
139+
async def get(self, url: str, headers: Optional[dict] = None) -> Deque[H3Event]:
140140
"""
141141
Perform a GET request.
142142
"""
@@ -145,7 +145,7 @@ async def get(self, url: str, headers: Optional[Dict] = None) -> Deque[H3Event]:
145145
)
146146

147147
async def post(
148-
self, url: str, data: bytes, headers: Optional[Dict] = None
148+
self, url: str, data: bytes, headers: Optional[dict] = None
149149
) -> Deque[H3Event]:
150150
"""
151151
Perform a POST request.
@@ -155,7 +155,7 @@ async def post(
155155
)
156156

157157
async def websocket(
158-
self, url: str, subprotocols: Optional[List[str]] = None
158+
self, url: str, subprotocols: Optional[list[str]] = None
159159
) -> WebSocket:
160160
"""
161161
Open a WebSocket.
@@ -347,7 +347,7 @@ def save_session_ticket(ticket: SessionTicket) -> None:
347347

348348
async def main(
349349
configuration: QuicConfiguration,
350-
urls: List[str],
350+
urls: list[str],
351351
data: Optional[str],
352352
include: bool,
353353
output_dir: Optional[str],

examples/http3_server.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66
from collections import deque
77
from email.utils import formatdate
8-
from typing import Callable, Deque, Dict, List, Optional, Union, cast
8+
from typing import Callable, Deque, Optional, Union, cast
99

1010
import aioquic
1111
import wsproto
@@ -44,15 +44,15 @@ def __init__(
4444
authority: bytes,
4545
connection: HttpConnection,
4646
protocol: QuicConnectionProtocol,
47-
scope: Dict,
47+
scope: dict,
4848
stream_ended: bool,
4949
stream_id: int,
5050
transmit: Callable[[], None],
5151
) -> None:
5252
self.authority = authority
5353
self.connection = connection
5454
self.protocol = protocol
55-
self.queue: asyncio.Queue[Dict] = asyncio.Queue()
55+
self.queue: asyncio.Queue[dict] = asyncio.Queue()
5656
self.scope = scope
5757
self.stream_id = stream_id
5858
self.transmit = transmit
@@ -77,10 +77,10 @@ def http_event_received(self, event: H3Event) -> None:
7777
async def run_asgi(self, app: AsgiApplication) -> None:
7878
await app(self.scope, self.receive, self.send)
7979

80-
async def receive(self) -> Dict:
80+
async def receive(self) -> dict:
8181
return await self.queue.get()
8282

83-
async def send(self, message: Dict) -> None:
83+
async def send(self, message: dict) -> None:
8484
if message["type"] == "http.response.start":
8585
self.connection.send_headers(
8686
stream_id=self.stream_id,
@@ -129,14 +129,14 @@ def __init__(
129129
self,
130130
*,
131131
connection: HttpConnection,
132-
scope: Dict,
132+
scope: dict,
133133
stream_id: int,
134134
transmit: Callable[[], None],
135135
) -> None:
136136
self.closed = False
137137
self.connection = connection
138138
self.http_event_queue: Deque[DataReceived] = deque()
139-
self.queue: asyncio.Queue[Dict] = asyncio.Queue()
139+
self.queue: asyncio.Queue[dict] = asyncio.Queue()
140140
self.scope = scope
141141
self.stream_id = stream_id
142142
self.transmit = transmit
@@ -171,10 +171,10 @@ async def run_asgi(self, app: AsgiApplication) -> None:
171171
if not self.closed:
172172
await self.send({"type": "websocket.close", "code": 1000})
173173

174-
async def receive(self) -> Dict:
174+
async def receive(self) -> dict:
175175
return await self.queue.get()
176176

177-
async def send(self, message: Dict) -> None:
177+
async def send(self, message: dict) -> None:
178178
data = b""
179179
end_stream = False
180180
if message["type"] == "websocket.accept":
@@ -229,15 +229,15 @@ def __init__(
229229
self,
230230
*,
231231
connection: H3Connection,
232-
scope: Dict,
232+
scope: dict,
233233
stream_id: int,
234234
transmit: Callable[[], None],
235235
) -> None:
236236
self.accepted = False
237237
self.closed = False
238238
self.connection = connection
239239
self.http_event_queue: Deque[H3Event] = deque()
240-
self.queue: asyncio.Queue[Dict] = asyncio.Queue()
240+
self.queue: asyncio.Queue[dict] = asyncio.Queue()
241241
self.scope = scope
242242
self.stream_id = stream_id
243243
self.transmit = transmit
@@ -274,10 +274,10 @@ async def run_asgi(self, app: AsgiApplication) -> None:
274274
if not self.closed:
275275
await self.send({"type": "webtransport.close"})
276276

277-
async def receive(self) -> Dict:
277+
async def receive(self) -> dict:
278278
return await self.queue.get()
279279

280-
async def send(self, message: Dict) -> None:
280+
async def send(self, message: dict) -> None:
281281
data = b""
282282
end_stream = False
283283

@@ -325,7 +325,7 @@ async def send(self, message: Dict) -> None:
325325
class HttpServerProtocol(QuicConnectionProtocol):
326326
def __init__(self, *args, **kwargs) -> None:
327327
super().__init__(*args, **kwargs)
328-
self._handlers: Dict[int, Handler] = {}
328+
self._handlers: dict[int, Handler] = {}
329329
self._http: Optional[HttpConnection] = None
330330

331331
def http_event_received(self, event: H3Event) -> None:
@@ -361,9 +361,9 @@ def http_event_received(self, event: H3Event) -> None:
361361
client = (client_addr[0], client_addr[1])
362362

363363
handler: Handler
364-
scope: Dict
364+
scope: dict
365365
if method == "CONNECT" and protocol == "websocket":
366-
subprotocols: List[str] = []
366+
subprotocols: list[str] = []
367367
for header, value in event.headers:
368368
if header == b"sec-websocket-protocol":
369369
subprotocols = [x.strip() for x in value.decode().split(",")]
@@ -409,7 +409,7 @@ def http_event_received(self, event: H3Event) -> None:
409409
transmit=self.transmit,
410410
)
411411
else:
412-
extensions: Dict[str, Dict] = {}
412+
extensions: dict[str, dict] = {}
413413
if isinstance(self._http, H3Connection):
414414
extensions["http.response.push"] = {}
415415
scope = {
@@ -471,7 +471,7 @@ class SessionTicketStore:
471471
"""
472472

473473
def __init__(self) -> None:
474-
self.tickets: Dict[bytes, SessionTicket] = {}
474+
self.tickets: dict[bytes, SessionTicket] = {}
475475

476476
def add(self, ticket: SessionTicket) -> None:
477477
self.tickets[ticket.ticket] = ticket

examples/httpx_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import ssl
77
import time
88
from collections import deque
9-
from typing import AsyncIterator, Deque, Dict, Optional, Tuple, cast
9+
from typing import AsyncIterator, Deque, Optional, cast
1010
from urllib.parse import urlparse
1111

1212
import httpx
@@ -35,8 +35,8 @@ def __init__(self, *args, **kwargs) -> None:
3535
super().__init__(*args, **kwargs)
3636

3737
self._http = H3Connection(self._quic)
38-
self._read_queue: Dict[int, Deque[H3Event]] = {}
39-
self._read_ready: Dict[int, asyncio.Event] = {}
38+
self._read_queue: dict[int, Deque[H3Event]] = {}
39+
self._read_ready: dict[int, asyncio.Event] = {}
4040

4141
async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
4242
assert isinstance(request.stream, httpx.AsyncByteStream)
@@ -94,7 +94,7 @@ def quic_event_received(self, event: QuicEvent):
9494
for http_event in self._http.handle_event(event):
9595
self.http_event_received(http_event)
9696

97-
async def _receive_response(self, stream_id: int) -> Tuple[int, Headers, bool]:
97+
async def _receive_response(self, stream_id: int) -> tuple[int, Headers, bool]:
9898
"""
9999
Read the response status and headers.
100100
"""

src/aioquic/_crypto.pyi

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Tuple
2-
31
class AEAD:
42
def __init__(self, cipher_name: bytes, key: bytes, iv: bytes): ...
53
def decrypt(
@@ -14,4 +12,4 @@ class CryptoError(ValueError): ...
1412
class HeaderProtection:
1513
def __init__(self, cipher_name: bytes, key: bytes): ...
1614
def apply(self, plain_header: bytes, protected_payload: bytes) -> bytes: ...
17-
def remove(self, packet: bytes, encrypted_offset: int) -> Tuple[bytes, int]: ...
15+
def remove(self, packet: bytes, encrypted_offset: int) -> tuple[bytes, int]: ...

src/aioquic/asyncio/protocol.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import asyncio
2-
from typing import Any, Callable, Dict, Optional, Text, Tuple, Union, cast
2+
from typing import Any, Callable, Optional, Text, Union, cast
33

44
from ..quic import events
55
from ..quic.connection import NetworkAddress, QuicConnection
@@ -19,9 +19,9 @@ def __init__(
1919
self._connected = False
2020
self._connected_waiter: Optional[asyncio.Future[None]] = None
2121
self._loop = loop
22-
self._ping_waiters: Dict[int, asyncio.Future[None]] = {}
22+
self._ping_waiters: dict[int, asyncio.Future[None]] = {}
2323
self._quic = quic
24-
self._stream_readers: Dict[int, asyncio.StreamReader] = {}
24+
self._stream_readers: dict[int, asyncio.StreamReader] = {}
2525
self._timer: Optional[asyncio.TimerHandle] = None
2626
self._timer_at: Optional[float] = None
2727
self._transmit_task: Optional[asyncio.Handle] = None
@@ -76,7 +76,7 @@ def connect(self, addr: NetworkAddress, transmit=True) -> None:
7676

7777
async def create_stream(
7878
self, is_unidirectional: bool = False
79-
) -> Tuple[asyncio.StreamReader, asyncio.StreamWriter]:
79+
) -> tuple[asyncio.StreamReader, asyncio.StreamWriter]:
8080
"""
8181
Create a QUIC stream and return a pair of (reader, writer) objects.
8282
@@ -182,7 +182,7 @@ def quic_event_received(self, event: events.QuicEvent) -> None:
182182

183183
def _create_stream(
184184
self, stream_id: int
185-
) -> Tuple[asyncio.StreamReader, asyncio.StreamWriter]:
185+
) -> tuple[asyncio.StreamReader, asyncio.StreamWriter]:
186186
adapter = QuicStreamAdapter(self, stream_id)
187187
reader = asyncio.StreamReader()
188188
protocol = asyncio.streams.StreamReaderProtocol(reader)

src/aioquic/asyncio/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import asyncio
22
import os
33
from functools import partial
4-
from typing import Callable, Dict, Optional, Text, Union, cast
4+
from typing import Callable, Optional, Text, Union, cast
55

66
from ..buffer import Buffer
77
from ..quic.configuration import SMALLEST_MAX_DATAGRAM_SIZE, QuicConfiguration
@@ -33,7 +33,7 @@ def __init__(
3333
self._configuration = configuration
3434
self._create_protocol = create_protocol
3535
self._loop = asyncio.get_running_loop()
36-
self._protocols: Dict[bytes, QuicConnectionProtocol] = {}
36+
self._protocols: dict[bytes, QuicConnectionProtocol] = {}
3737
self._session_ticket_fetcher = session_ticket_fetcher
3838
self._session_ticket_handler = session_ticket_handler
3939
self._transport: Optional[asyncio.DatagramTransport] = None

src/aioquic/h0/connection.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Dict, List
2-
31
from aioquic.h3.events import DataReceived, H3Event, Headers, HeadersReceived
42
from aioquic.quic.connection import QuicConnection
53
from aioquic.quic.events import QuicEvent, StreamDataReceived
@@ -13,13 +11,13 @@ class H0Connection:
1311
"""
1412

1513
def __init__(self, quic: QuicConnection):
16-
self._buffer: Dict[int, bytes] = {}
17-
self._headers_received: Dict[int, bool] = {}
14+
self._buffer: dict[int, bytes] = {}
15+
self._headers_received: dict[int, bool] = {}
1816
self._is_client = quic.configuration.is_client
1917
self._quic = quic
2018

21-
def handle_event(self, event: QuicEvent) -> List[H3Event]:
22-
http_events: List[H3Event] = []
19+
def handle_event(self, event: QuicEvent) -> list[H3Event]:
20+
http_events: list[H3Event] = []
2321

2422
if isinstance(event, StreamDataReceived) and (event.stream_id % 4) == 0:
2523
data = self._buffer.pop(event.stream_id, b"") + event.data

0 commit comments

Comments
 (0)