sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

(websockets 12.0) DeprecationWarning: websockets.connection was renamed to websockets.protocol and Connection was renamed to Protocol

Gerhut opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The DeprecationWarning is thrown here:

try: # websockets < 11.0
from websockets.connection import State
from websockets.server import ServerConnection as ServerProtocol
except ImportError: # websockets >= 11.0
from websockets.protocol import State # type: ignore
from websockets.server import ServerProtocol # type: ignore

With websockets 12 the try block would run successfully with the warning while the catch block does not have chance to be run.

Code snippet

No response

Expected Behavior

The catch block is being run instead.

How do you run Sanic?

Sanic CLI

Operating System

MacOS

Sanic Version

Sanic 23.6.0; Routing 23.6.0

Additional context

No response

Although it triggers a deprecation warning, the try...except... block will work when the classes are actually removed in websockets package in the future. Checking version of a package requires an additional dependency packaging, which we wouldn't like to add, thus I think we can just ignore the warning.

See more discussion in #2880

Although it triggers a deprecation warning, the try...except... block will work when the classes are actually removed in websockets package in the future. Checking version of a package requires an additional dependency packaging, which we wouldn't like to add, thus I think we can just ignore the warning.

Can't you try the new API, which will give ImportError or such but no deprecation warnings, and except fallback to old API?

Looks like you can also use websockets.__version__ for a version string.

@Tronic You are right, lol. I will try that way.