websockets icon indicating copy to clipboard operation
websockets copied to clipboard

Add explanation of imports to API reference

Open 0xdade opened this issue 2 years ago • 0 comments

Yesterday, while trying to type check my code using the websockets library, I was getting quite frustrated. All of the example code seemed to show import websockets and then accessing everything directly off the package, which doesn't support type checking or code completion because of the lazy import system. I nearly opened an issue about the matter to see what I was doing wrong, when I then went to double check that there wasn't an existing issue about it before filing. Lo and behold, I found https://github.com/aaugustin/websockets/issues/940#issuecomment-874012438 which gave me what I needed to start type checking my code that uses websockets.

I couldn't find this directly provided as an example or in any of the example code I saw in the docs, but while digging around did see this reference to "use the real import path" at the bottom of the API reference index, without really elaborating on what that is. I consider myself a reasonably experienced python dev and was surprised/confused at first to see import websockets not exposing any of the modules to my IDE but from websockets import exposing all available modules (and thereby giving my autocomplete and letting mypy type check correctly).

So rather than just raise another issue complaining that I couldn't figure it out, I thought I'd add in a couple more lines to the docs to explain what "use the real import paths" actually looks like in code.

I also considered perhaps adding a note to the types page of the docs, but felt that wasn't really the correct place since that's a proper API reference. I also thought about maybe a new docs page in the how-to guides about how to use the websockets library in a way that your IDE and type checkers know what's going on, but thought I'd start with this simple change first.

0xdade avatar Jun 26 '22 18:06 0xdade