python-websockets / websockets

Library for building WebSocket servers and clients in Python
https://websockets.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
5.16k stars 513 forks source link

Use `lazy_loader` for imports or implement stubs/hints in `lazy_import` #1385

Closed wolph closed 1 year ago

wolph commented 1 year ago

Most IDE's currently don't properly understand the example code due to the current implementation of the lazy_import function. Luckily, there's a simple solution for this besides changing your imports from import websockets to import websockets.client :)

There is a great little project called lazy_loader that effectively does the same as what lazy_import does now but it also properly supports type hints and stubs: https://github.com/scientific-python/lazy_loader#type-checkers

I believe a switch to lazy_loader, or an update for lazy_import if you want to avoid a dependency, could improve the usability of the library quite a bit.

128f commented 1 year ago

After reading this I was curious how this project gets built, and eventually went looking for signs of a dependency management tool, but I couldn't find one.

Then I found out It doesn't have any dependencies., which is rather incredible.

Given that, I would guess the maintainer of this project would be reluctant to add an outside project as a dependency.

aaugustin commented 1 year ago

I can confirm that I'm reluctant to add dependencies. Indeed, dependencies increase the cost of maintenance — simply because they increase the number of things that can change outside of my control — while I cannot dedicate much time to maintaining websockets.

I looked at lazy_loader when this issue was filed. It appears to require duplicating type information in .pyi files, which I don't want to do because it's yet another thing to maintain.

I think I'm just going to adopt explicit import paths everywhere eventually. Convenience imports were cool when the library was younger. We've outgrown them 🤷

aaugustin commented 1 year ago

PS: why would websockets need any dependencies? ;-)

128f commented 1 year ago

PS: why would websockets need any dependencies? ;-)

I know you're half kidding but it's quite shocking to me to see a library like this not have any at all.

Eg rust websocket has dependencies, and I would usually expect to see dev dependencies.

I was rather confused for a while, insisting to myself that I must be mistaken and just not seeing the dependency management artifacts.

Anyways, kudos to you for avoiding it altogether

wolph commented 1 year ago

I totally get that :) Many of my libraries have no dependencies or only dependencies I maintain myself. Less stuff that can break that way ;)

As a 3rd option, I believe that a stub file could fix it as well without requiring any other changes. The only issue with stub files is that they could become out of date. But this library is rather stable and you could easily have pyright automatically test for that.

aaugustin commented 1 year ago

439dafa6 should fix it. Duplicating 50 lines in a file is still easier to maintain than taking a dependency.