Repository navigation
Stop re-exporting websocket classes from s2python.connection - #160
Conversation
|
@dirkjanfaber Lets get you sorted so websockets remains an optional import but you can still use the connection stuff. The current approach unfortunately will break a bunch of stuff due to the dynamic nature. And it is also a bit more difficult to read. I would like to propose an alternative pattern: Lets remove those imports from those init.py's. Rationale: That keeps typing, code completion and other stuff perfectly intact. It forces you to use specific medium/quickstart packages so it will load dependencies such as websockets only if it needs to. That keeps memory down as well. I don't think a lot of people use the quickstart/existing mediums as they are mainly meant for example purposes. But even then, I would rather request users to switch import statements than introduce a pattern that will be difficult to maintain if we ever add more mediums/quickstarts. What do you think? |
|
I agree, that is the better way, even though it is a small breaking change. |
'websockets' is an optional dependency (the 'ws' extra), but
s2python.connection and s2python.connection.async_ imported
BlockingWebsocketClientRM and WebsocketClientMedium eagerly. Importing
anything from s2python.connection (control types, AssetDetails, ...)
therefore raised ImportError without 'websockets' installed, and loaded
the websocket and sync connection stack even when using another medium.
Remove both re-exports. Users import them from their own modules:
from s2python.connection.quickstarts import BlockingWebsocketClientRM
from s2python.connection.async_.medium.websocket import WebsocketClientMedium
This is a breaking change for code using the old import paths.
9c47b66 to
1c509cb
Compare
|
Released as part of https://github.com/flexiblepower/s2-python/releases/tag/v0.11.0 . Thanks again @dirkjanfaber ! |
websocketsis an optional dependency (thewsextra), buts2python.connectionands2python.connection.async_importedBlockingWebsocketClientRMandWebsocketClientMediumeagerly. As a result:s2python.connection(control types,AssetDetails, ...) raisedImportErrorwhenwebsocketswas not installed;websocketsinstalled, it still loaded the websocket client and the whole sync connection stack, even for code using another medium.As discussed below, both re-exports are removed instead of loaded lazily. This keeps typing and code completion intact, and
websocketsonly loads when you import a websocket module.Breaking change: the old import paths no longer work. Use:
The examples and
quickstarts.pyare updated. Because this is still 0.x, the next release should be a minor bump (0.11.0).Tests in
tests/unit/connection/init_test.pyrun the imports in a subprocess withwebsocketsblocked.Note: this is an updated text + title from the original PR, based on the below remark of @sebastiaan-la-fleur.