Skip to content

Commit 2abf77f

Browse files
committed
Improve consistency of convenience imports.
APIs available as convenience imports should have their types also available as convenienc imports. Fix #1560.
1 parent 197b3ec commit 2abf77f

File tree

6 files changed

+77
-29
lines changed

6 files changed

+77
-29
lines changed

docs/howto/upgrade.rst

+6-6
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ Client APIs
9393
| ``websockets.client.unix_connect()`` |br| | :func:`websockets.asyncio.client.unix_connect` |
9494
| :func:`websockets.legacy.client.unix_connect` | |
9595
+-------------------------------------------------------------------+-----------------------------------------------------+
96-
| ``websockets.WebSocketClientProtocol`` |br| | :class:`websockets.asyncio.client.ClientConnection` |
97-
| ``websockets.client.WebSocketClientProtocol`` |br| | |
96+
| ``websockets.WebSocketClientProtocol`` |br| | ``websockets.ClientConnection`` *(since 14.2)* |br| |
97+
| ``websockets.client.WebSocketClientProtocol`` |br| | :class:`websockets.asyncio.client.ClientConnection` |
9898
| :class:`websockets.legacy.client.WebSocketClientProtocol` | |
9999
+-------------------------------------------------------------------+-----------------------------------------------------+
100100

@@ -112,12 +112,12 @@ Server APIs
112112
| ``websockets.server.unix_serve()`` |br| | :func:`websockets.asyncio.server.unix_serve` |
113113
| :func:`websockets.legacy.server.unix_serve` | |
114114
+-------------------------------------------------------------------+-----------------------------------------------------+
115-
| ``websockets.WebSocketServer`` |br| | :class:`websockets.asyncio.server.Server` |
116-
| ``websockets.server.WebSocketServer`` |br| | |
115+
| ``websockets.WebSocketServer`` |br| | ``websockets.Server`` *(since 14.2)* |br| |
116+
| ``websockets.server.WebSocketServer`` |br| | :class:`websockets.asyncio.server.Server` |
117117
| :class:`websockets.legacy.server.WebSocketServer` | |
118118
+-------------------------------------------------------------------+-----------------------------------------------------+
119-
| ``websockets.WebSocketServerProtocol`` |br| | :class:`websockets.asyncio.server.ServerConnection` |
120-
| ``websockets.server.WebSocketServerProtocol`` |br| | |
119+
| ``websockets.WebSocketServerProtocol`` |br| | ``websockets.ServerConnection`` *(since 14.2)* |br| |
120+
| ``websockets.server.WebSocketServerProtocol`` |br| | :class:`websockets.asyncio.server.ServerConnection` |
121121
| :class:`websockets.legacy.server.WebSocketServerProtocol` | |
122122
+-------------------------------------------------------------------+-----------------------------------------------------+
123123
| ``websockets.broadcast()`` *(before 14.0)* |br| | ``websockets.broadcast()`` *(since 14.0)* |br| |

src/websockets/__init__.py

+42-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
# .asyncio.client
1111
"connect",
1212
"unix_connect",
13+
"ClientConnection",
1314
# .asyncio.server
1415
"basic_auth",
1516
"broadcast",
1617
"serve",
1718
"unix_serve",
19+
"ServerConnection",
20+
"Server",
1821
# .client
1922
"ClientProtocol",
2023
# .datastructures
@@ -44,6 +47,18 @@
4447
"ProtocolError",
4548
"SecurityError",
4649
"WebSocketException",
50+
# .frames
51+
"Close",
52+
"CloseCode",
53+
"Frame",
54+
"Opcode",
55+
# .http11
56+
"Request",
57+
"Response",
58+
# .protocol
59+
"Protocol",
60+
"Side",
61+
"State",
4762
# .server
4863
"ServerProtocol",
4964
# .typing
@@ -58,8 +73,15 @@
5873

5974
# When type checking, import non-deprecated aliases eagerly. Else, import on demand.
6075
if typing.TYPE_CHECKING:
61-
from .asyncio.client import connect, unix_connect
62-
from .asyncio.server import basic_auth, broadcast, serve, unix_serve
76+
from .asyncio.client import ClientConnection, connect, unix_connect
77+
from .asyncio.server import (
78+
Server,
79+
ServerConnection,
80+
basic_auth,
81+
broadcast,
82+
serve,
83+
unix_serve,
84+
)
6385
from .client import ClientProtocol
6486
from .datastructures import Headers, HeadersLike, MultipleValuesError
6587
from .exceptions import (
@@ -86,6 +108,9 @@
86108
SecurityError,
87109
WebSocketException,
88110
)
111+
from .frames import Close, CloseCode, Frame, Opcode
112+
from .http11 import Request, Response
113+
from .protocol import Protocol, Side, State
89114
from .server import ServerProtocol
90115
from .typing import (
91116
Data,
@@ -103,11 +128,14 @@
103128
# .asyncio.client
104129
"connect": ".asyncio.client",
105130
"unix_connect": ".asyncio.client",
131+
"ClientConnection": ".asyncio.client",
106132
# .asyncio.server
107133
"basic_auth": ".asyncio.server",
108134
"broadcast": ".asyncio.server",
109135
"serve": ".asyncio.server",
110136
"unix_serve": ".asyncio.server",
137+
"ServerConnection": ".asyncio.server",
138+
"Server": ".asyncio.server",
111139
# .client
112140
"ClientProtocol": ".client",
113141
# .datastructures
@@ -137,6 +165,18 @@
137165
"ProtocolError": ".exceptions",
138166
"SecurityError": ".exceptions",
139167
"WebSocketException": ".exceptions",
168+
# .frames
169+
"Close": ".frames",
170+
"CloseCode": ".frames",
171+
"Frame": ".frames",
172+
"Opcode": ".frames",
173+
# .http11
174+
"Request": ".http11",
175+
"Response": ".http11",
176+
# .protocol
177+
"Protocol": ".protocol",
178+
"Side": ".protocol",
179+
"State": ".protocol",
140180
# .server
141181
"ServerProtocol": ".server",
142182
# .typing

src/websockets/datastructures.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from typing import Any, Protocol, Union
55

66

7-
__all__ = ["Headers", "HeadersLike", "MultipleValuesError"]
7+
__all__ = [
8+
"Headers",
9+
"HeadersLike",
10+
"MultipleValuesError",
11+
]
812

913

1014
class MultipleValuesError(LookupError):

src/websockets/frames.py

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"OP_PONG",
2929
"DATA_OPCODES",
3030
"CTRL_OPCODES",
31+
"CloseCode",
3132
"Frame",
3233
"Close",
3334
]

src/websockets/http11.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
from .version import version as websockets_version
1414

1515

16-
__all__ = ["SERVER", "USER_AGENT", "Request", "Response"]
16+
__all__ = [
17+
"SERVER",
18+
"USER_AGENT",
19+
"Request",
20+
"Response",
21+
]
1722

1823

1924
PYTHON_VERSION = "{}.{}".format(*sys.version_info)

tests/test_exports.py

+17-19
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,30 @@
1111
import websockets.uri
1212

1313

14-
combined_exports = (
15-
[]
16-
+ websockets.asyncio.client.__all__
17-
+ websockets.asyncio.server.__all__
18-
+ websockets.client.__all__
19-
+ websockets.datastructures.__all__
20-
+ websockets.exceptions.__all__
21-
+ websockets.server.__all__
22-
+ websockets.typing.__all__
23-
)
24-
25-
# These API are intentionally not re-exported by the top-level module.
26-
missing_reexports = [
27-
# websockets.asyncio.client
28-
"ClientConnection",
29-
# websockets.asyncio.server
30-
"ServerConnection",
31-
"Server",
14+
combined_exports = [
15+
name
16+
for name in (
17+
[]
18+
+ websockets.asyncio.client.__all__
19+
+ websockets.asyncio.server.__all__
20+
+ websockets.client.__all__
21+
+ websockets.datastructures.__all__
22+
+ websockets.exceptions.__all__
23+
+ websockets.frames.__all__
24+
+ websockets.http11.__all__
25+
+ websockets.protocol.__all__
26+
+ websockets.server.__all__
27+
+ websockets.typing.__all__
28+
)
29+
if not name.isupper() # filter out constants
3230
]
3331

3432

3533
class ExportsTests(unittest.TestCase):
3634
def test_top_level_module_reexports_submodule_exports(self):
3735
self.assertEqual(
3836
set(combined_exports),
39-
set(websockets.__all__ + missing_reexports),
37+
set(websockets.__all__),
4038
)
4139

4240
def test_submodule_exports_are_globally_unique(self):

0 commit comments

Comments
 (0)