Skip to content

Stop re-exporting websocket classes from s2python.connection - #160

Merged
sebastiaan-la-fleur merged 2 commits into
flexiblepower:mainfrom
dirkjanfaber:lazy-websocket-import
Oct 22, 2026
Merged

sebastiaan-la-fleur merged 2 commits into
flexiblepower:mainfrom
dirkjanfaber:lazy-websocket-import

Conversation

@dirkjanfaber

@dirkjanfaber dirkjanfaber commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

websockets is an optional dependency (the ws extra), but s2python.connection and s2python.connection.async_ imported BlockingWebsocketClientRM and WebsocketClientMedium eagerly. As a result:

  • importing anything from s2python.connection (control types, AssetDetails, ...) raised ImportError when websockets was not installed;
  • with websockets installed, 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 websockets only loads when you import a websocket module.

Breaking change: the old import paths no longer work. Use:

from s2python.connection.quickstarts import BlockingWebsocketClientRM
from s2python.connection.async_.medium.websocket import WebsocketClientMedium

The examples and quickstarts.py are 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.py run the imports in a subprocess with websockets blocked.

Note: this is an updated text + title from the original PR, based on the below remark of @sebastiaan-la-fleur.

@sebastiaan-la-fleur

Copy link
Copy Markdown
Member

@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?

@dirkjanfaber

Copy link
Copy Markdown
Contributor Author

I agree, that is the better way, even though it is a small breaking change.
The from s2python.connection import BlockingWebsocketClientRM and from s2python.connection.async_ import WebsocketClientMedium stop working. On the 0.x versioning that should mean a minor bump (0.11.0) rather than 0.10.3
But I am happy with that.

'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.
@dirkjanfaber
dirkjanfaber force-pushed the lazy-websocket-import branch from 9c47b66 to 1c509cb Compare October 21, 2026 19:21
@dirkjanfaber dirkjanfaber changed the title Import websocket medium lazily in s2python.connection Stop re-exporting websocket classes from s2python.connectio Oct 21, 2026
@dirkjanfaber dirkjanfaber changed the title Stop re-exporting websocket classes from s2python.connectio Stop re-exporting websocket classes from s2python.connection Oct 21, 2026
@sebastiaan-la-fleur
sebastiaan-la-fleur merged commit 04bcded into flexiblepower:main Oct 22, 2026
27 checks passed
@sebastiaan-la-fleur

Copy link
Copy Markdown
Member

Released as part of https://github.com/flexiblepower/s2-python/releases/tag/v0.11.0 . Thanks again @dirkjanfaber !

@dirkjanfaber
dirkjanfaber deleted the lazy-websocket-import branch October 22, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants