Skip to content

Commit

Permalink
Restore real imports for compatibility with mypy.
Browse files Browse the repository at this point in the history
Fix #940.
  • Loading branch information
aaugustin committed May 13, 2021
1 parent e44e085 commit b99c4fe
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/api/client.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Client
======

.. automodule:: websockets.legacy.client
.. automodule:: websockets.client

Opening a connection
--------------------
Expand Down
9 changes: 7 additions & 2 deletions docs/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,10 @@ both in the client API and server API.
utilities

All public APIs can be imported from the :mod:`websockets` package, unless
noted otherwise. Anything that isn't listed in this API documentation is a
private API, with no guarantees of behavior or backwards-compatibility.
noted otherwise. This convenience feature is incompatible with static code
analysis tools such as mypy_, though.

.. _mypy: https://github.com/python/mypy

Anything that isn't listed in this API documentation is a private API. There's
no guarantees of behavior or backwards-compatibility for private APIs.
4 changes: 2 additions & 2 deletions docs/api/server.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Server
======

.. automodule:: websockets.legacy.server
.. automodule:: websockets.server

Starting a server
-----------------
Expand Down Expand Up @@ -90,7 +90,7 @@ Server
Basic authentication
--------------------

.. automodule:: websockets.legacy.auth
.. automodule:: websockets.auth

.. autofunction:: basic_auth_protocol_factory

Expand Down
16 changes: 16 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ They may change at any time.

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

* Restored compatibility with mypy.

This comment has been minimized.

Copy link
@lig

lig May 14, 2021

👍🏻


9.0.1
.....

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

.. note::

**Version 9.0 may require changes if you use static code analysis tools.**

Convenience imports from the ``websockets`` module are performed lazily.
While this is supported by Python, static code analysis tools such as mypy
are unable to understand the behavior.

If you depend on such tools, use the real import path, which can be found
in the API documentation::

from websockets.client import connect
from websockets.server import serve

* Added compatibility with Python 3.9.

* Added support for IRIs in addition to URIs.
Expand Down
5 changes: 2 additions & 3 deletions docs/extensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ specification, WebSocket Per-Message Deflate, specified in :rfc:`7692`.
Per-Message Deflate
-------------------

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

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

Expand Down
2 changes: 2 additions & 0 deletions src/websockets/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# See #940 for why lazy_import isn't used here for backwards compatibility.
from .legacy.auth import * # noqa
12 changes: 3 additions & 9 deletions src/websockets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
)
from .http import USER_AGENT, build_host
from .http11 import Request, Response
from .imports import lazy_import
from .typing import (
ConnectionOption,
ExtensionHeader,
Expand All @@ -36,14 +35,9 @@
from .utils import accept_key, generate_key


lazy_import(
globals(),
aliases={
"connect": ".legacy.client",
"unix_connect": ".legacy.client",
"WebSocketClientProtocol": ".legacy.client",
},
)
# See #940 for why lazy_import isn't used here for backwards compatibility.
from .legacy.client import * # isort:skip # noqa


__all__ = ["ClientConnection"]

Expand Down
12 changes: 2 additions & 10 deletions src/websockets/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
)
from .http import USER_AGENT
from .http11 import Request, Response
from .imports import lazy_import
from .typing import (
ConnectionOption,
ExtensionHeader,
Expand All @@ -37,15 +36,8 @@
from .utils import accept_key


lazy_import(
globals(),
aliases={
"serve": ".legacy.server",
"unix_serve": ".legacy.server",
"WebSocketServerProtocol": ".legacy.server",
"WebSocketServer": ".legacy.server",
},
)
# See #940 for why lazy_import isn't used here for backwards compatibility.
from .legacy.server import * # isort:skip # noqa


__all__ = ["ServerConnection"]
Expand Down

0 comments on commit b99c4fe

Please sign in to comment.