Add explanation of imports to API reference #1191
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Yesterday, while trying to type check my code using the
websockets
library, I was getting quite frustrated. All of the example code seemed to showimport 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 #940 (comment) which gave me what I needed to start type checking my code that useswebsockets
.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 butfrom 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.