Skip to content

Commit b99c4fe

Browse files
committed
Restore real imports for compatibility with mypy.
Fix #940.
1 parent e44e085 commit b99c4fe

File tree

8 files changed

+35
-27
lines changed

8 files changed

+35
-27
lines changed

docs/api/client.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Client
22
======
33

4-
.. automodule:: websockets.legacy.client
4+
.. automodule:: websockets.client
55

66
Opening a connection
77
--------------------

docs/api/index.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,10 @@ both in the client API and server API.
4646
utilities
4747

4848
All public APIs can be imported from the :mod:`websockets` package, unless
49-
noted otherwise. Anything that isn't listed in this API documentation is a
50-
private API, with no guarantees of behavior or backwards-compatibility.
49+
noted otherwise. This convenience feature is incompatible with static code
50+
analysis tools such as mypy_, though.
51+
52+
.. _mypy: https://github.com/python/mypy
53+
54+
Anything that isn't listed in this API documentation is a private API. There's
55+
no guarantees of behavior or backwards-compatibility for private APIs.

docs/api/server.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Server
22
======
33

4-
.. automodule:: websockets.legacy.server
4+
.. automodule:: websockets.server
55

66
Starting a server
77
-----------------
@@ -90,7 +90,7 @@ Server
9090
Basic authentication
9191
--------------------
9292

93-
.. automodule:: websockets.legacy.auth
93+
.. automodule:: websockets.auth
9494

9595
.. autofunction:: basic_auth_protocol_factory
9696

docs/changelog.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ They may change at any time.
3737

3838
* Restored compatibility of ``python -m websockets`` with Python < 3.9.
3939

40+
* Restored compatibility with mypy.
41+
4042
9.0.1
4143
.....
4244

@@ -73,6 +75,20 @@ They may change at any time.
7375
but that never happened. Keeping these APIs public makes it more
7476
difficult to improve websockets for no actual benefit.
7577

78+
.. note::
79+
80+
**Version 9.0 may require changes if you use static code analysis tools.**
81+
82+
Convenience imports from the ``websockets`` module are performed lazily.
83+
While this is supported by Python, static code analysis tools such as mypy
84+
are unable to understand the behavior.
85+
86+
If you depend on such tools, use the real import path, which can be found
87+
in the API documentation::
88+
89+
from websockets.client import connect
90+
from websockets.server import serve
91+
7692
* Added compatibility with Python 3.9.
7793

7894
* Added support for IRIs in addition to URIs.

docs/extensions.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ specification, WebSocket Per-Message Deflate, specified in :rfc:`7692`.
1414
Per-Message Deflate
1515
-------------------
1616

17-
:func:`~websockets.legacy.client.connect` and
18-
:func:`~websockets.legacy.server.serve` enable the Per-Message Deflate
19-
extension by default.
17+
:func:`~websockets.client.connect` and :func:`~websockets.server.serve` enable
18+
the Per-Message Deflate extension by default.
2019

2120
If you want to disable it, set ``compression=None``::
2221

src/websockets/auth.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# See #940 for why lazy_import isn't used here for backwards compatibility.
2+
from .legacy.auth import * # noqa

src/websockets/client.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
)
2525
from .http import USER_AGENT, build_host
2626
from .http11 import Request, Response
27-
from .imports import lazy_import
2827
from .typing import (
2928
ConnectionOption,
3029
ExtensionHeader,
@@ -36,14 +35,9 @@
3635
from .utils import accept_key, generate_key
3736

3837

39-
lazy_import(
40-
globals(),
41-
aliases={
42-
"connect": ".legacy.client",
43-
"unix_connect": ".legacy.client",
44-
"WebSocketClientProtocol": ".legacy.client",
45-
},
46-
)
38+
# See #940 for why lazy_import isn't used here for backwards compatibility.
39+
from .legacy.client import * # isort:skip # noqa
40+
4741

4842
__all__ = ["ClientConnection"]
4943

src/websockets/server.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
)
2727
from .http import USER_AGENT
2828
from .http11 import Request, Response
29-
from .imports import lazy_import
3029
from .typing import (
3130
ConnectionOption,
3231
ExtensionHeader,
@@ -37,15 +36,8 @@
3736
from .utils import accept_key
3837

3938

40-
lazy_import(
41-
globals(),
42-
aliases={
43-
"serve": ".legacy.server",
44-
"unix_serve": ".legacy.server",
45-
"WebSocketServerProtocol": ".legacy.server",
46-
"WebSocketServer": ".legacy.server",
47-
},
48-
)
39+
# See #940 for why lazy_import isn't used here for backwards compatibility.
40+
from .legacy.server import * # isort:skip # noqa
4941

5042

5143
__all__ = ["ServerConnection"]

0 commit comments

Comments
 (0)