Skip to content

Commit 4bc4393

Browse files
committed
Take advantage of typing.Self.
This is a small correctness improvement for Connection classes, which are subclassed as ClientConnection and ServerConnection.
1 parent 731eddf commit 4bc4393

8 files changed

Lines changed: 16 additions & 16 deletions

File tree

‎src/websockets/asyncio/connection.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import uuid
1111
from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Iterable, Mapping
1212
from types import TracebackType
13-
from typing import Any, Literal, cast, overload
13+
from typing import Any, Literal, Self, cast, overload
1414

1515
from ..exceptions import (
1616
ConcurrencyError,
@@ -205,7 +205,7 @@ def close_reason(self) -> str | None:
205205

206206
# Public methods
207207

208-
async def __aenter__(self) -> Connection:
208+
async def __aenter__(self) -> Self:
209209
return self
210210

211211
async def __aexit__(

‎src/websockets/asyncio/server.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import socket
99
from collections.abc import Awaitable, Generator, Iterable, Sequence
1010
from types import TracebackType
11-
from typing import Any, Callable, Mapping, cast
11+
from typing import Any, Callable, Mapping, Self, cast
1212

1313
from ..exceptions import InvalidHeader
1414
from ..extensions.base import ServerExtensionFactory
@@ -543,7 +543,7 @@ def sockets(self) -> tuple[socket.socket, ...]:
543543
"""
544544
return self.server.sockets
545545

546-
async def __aenter__(self) -> Server: # pragma: no cover
546+
async def __aenter__(self) -> Self: # pragma: no cover
547547
return self
548548

549549
async def __aexit__(

‎src/websockets/frames.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import secrets
88
import struct
99
from collections.abc import Generator, Sequence
10-
from typing import Callable
10+
from typing import Callable, Self
1111

1212
from .exceptions import PayloadTooBig, ProtocolError
1313
from .typing import BytesLike
@@ -431,7 +431,7 @@ def __str__(self) -> str:
431431
return result
432432

433433
@classmethod
434-
def parse(cls, data: BytesLike) -> Close:
434+
def parse(cls, data: BytesLike) -> Self:
435435
"""
436436
Parse the payload of a close frame.
437437

‎src/websockets/legacy/server.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import warnings
1111
from collections.abc import Awaitable, Generator, Iterable, Sequence
1212
from types import TracebackType
13-
from typing import Any, Callable, cast
13+
from typing import Any, Callable, Self, cast
1414

1515
from ..datastructures import Headers, HeadersLike, MultipleValuesError
1616
from ..exceptions import (
@@ -861,7 +861,7 @@ def sockets(self) -> Iterable[socket.socket]:
861861
"""
862862
return self.server.sockets
863863

864-
async def __aenter__(self) -> WebSocketServer: # pragma: no cover
864+
async def __aenter__(self) -> Self: # pragma: no cover
865865
return self
866866

867867
async def __aexit__(

‎src/websockets/sync/connection.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import uuid
1313
from collections.abc import Iterable, Iterator, Mapping
1414
from types import TracebackType
15-
from typing import Any, Literal, overload
15+
from typing import Any, Literal, Self, overload
1616

1717
from ..exceptions import (
1818
ConcurrencyError,
@@ -216,7 +216,7 @@ def close_reason(self) -> str | None:
216216

217217
# Public methods
218218

219-
def __enter__(self) -> Connection:
219+
def __enter__(self) -> Self:
220220
return self
221221

222222
def __exit__(

‎src/websockets/sync/server.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import warnings
1515
from collections.abc import Iterable, Sequence
1616
from types import TracebackType
17-
from typing import Any, Callable, Mapping, cast
17+
from typing import Any, Callable, Mapping, Self, cast
1818

1919
from ..exceptions import InvalidHeader
2020
from ..extensions.base import ServerExtensionFactory
@@ -439,7 +439,7 @@ def fileno(self) -> int:
439439
"""
440440
return self.socket.fileno()
441441

442-
def __enter__(self) -> Server:
442+
def __enter__(self) -> Self:
443443
return self
444444

445445
def __exit__(

‎src/websockets/trio/connection.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import uuid
99
from collections.abc import AsyncIterable, AsyncIterator, Iterable, Mapping
1010
from types import TracebackType
11-
from typing import Any, Literal, overload
11+
from typing import Any, Literal, Self, overload
1212

1313
import trio
1414
import trio.abc
@@ -221,7 +221,7 @@ def close_reason(self) -> str | None:
221221

222222
# Public methods
223223

224-
async def __aenter__(self) -> Connection:
224+
async def __aenter__(self) -> Self:
225225
return self
226226

227227
async def __aexit__(

‎src/websockets/trio/server.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import ssl as ssl_module
88
from collections.abc import Awaitable, Mapping, Sequence
99
from types import TracebackType
10-
from typing import Any, Callable
10+
from typing import Any, Callable, Self
1111

1212
import trio
1313
import trio.abc
@@ -338,7 +338,7 @@ async def aclose(
338338

339339
self.logger.info("server closed")
340340

341-
async def __aenter__(self) -> Server:
341+
async def __aenter__(self) -> Self:
342342
return self
343343

344344
async def __aexit__(

0 commit comments

Comments
 (0)