-
-
Notifications
You must be signed in to change notification settings - Fork 31
Add inline type hints #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,4 @@ dist | |
| examples/fake.* | ||
| trio_websocket.egg-info | ||
| venv | ||
| .mypy_cache/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [mypy] | ||
| strict = True | ||
| python_version = 3.6 | ||
| show_error_codes = True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,5 @@ sphinx_rtd_theme | |
| trio>=0.14.0 | ||
| trustme | ||
| twine | ||
| mypy; platform.python_implementation == 'CPython' | ||
| trio-typing | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,20 @@ | ||
| from ._impl import ( | ||
| CloseReason, | ||
| ConnectionClosed, | ||
| ConnectionRejected, | ||
| ConnectionTimeout, | ||
| connect_websocket, | ||
| connect_websocket_url, | ||
| DisconnectionTimeout, | ||
| Endpoint, | ||
| HandshakeError, | ||
| open_websocket, | ||
| open_websocket_url, | ||
| WebSocketConnection, | ||
| WebSocketRequest, | ||
| WebSocketServer, | ||
| wrap_client_stream, | ||
| wrap_server_stream, | ||
| serve_websocket, | ||
| CloseReason as CloseReason, | ||
| ConnectionClosed as ConnectionClosed, | ||
| ConnectionRejected as ConnectionRejected, | ||
| ConnectionTimeout as ConnectionTimeout, | ||
| connect_websocket as connect_websocket, | ||
| connect_websocket_url as connect_websocket_url, | ||
| DisconnectionTimeout as DisconnectionTimeout, | ||
| Endpoint as Endpoint, | ||
| HandshakeError as HandshakeError, | ||
| open_websocket as open_websocket, | ||
| open_websocket_url as open_websocket_url, | ||
| WebSocketConnection as WebSocketConnection, | ||
| WebSocketRequest as WebSocketRequest, | ||
| WebSocketServer as WebSocketServer, | ||
| wrap_client_stream as wrap_client_stream, | ||
| wrap_server_stream as wrap_server_stream, | ||
| serve_websocket as serve_websocket, | ||
| ) | ||
| from ._version import __version__ | ||
| from ._version import __version__ as __version__ |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the proposal-- I'm a little hesitant to go full type-hints with trio-websocket.
There are benefits, but they doesn't come for free-- at the cost of more verbose code, more complexity, and more dependencies.
Things like the tedious re-exporting, or having to disable useful pylint checkers, are example downsides. Also, it can be corrected, but it seems unnecessary to type hint variables and args that can be inferred by initialization (
max_message_size: int = MAX_MESSAGE_SIZE, etc.).May I ask if you're actively using trio-websocket in a project? Also, did this exercise uncover any bugs in the implementation?
(This comment is overall for the PR, but I'll start the thread here.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep! Though I have just gotten by by including those stubs in a folder.
Project is open-source actually: https://github.com/A5rocks/bloom
As mentioned, I started with a base set of stubs so I didn't find any bugs in my implementation.
Yep, although I should be able to just extract the stubs out into
.pyifiles (I haven't played with mypy'sstubgenandstubtestyet), if the downsides are too much.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if external pyi files improves much.
stubgenwould just provide a starting point, and would have to be manually edited and kept up to date with the source. And it doesn't address some of the complexity issues (typing can get quite complex, such as nested types, async generators, etc.).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, sorry for taking a while to get back, wanted to do this when I felt I had enough time to actually write a decent response.
Since you seem to already know many of the downsides, let me try my hand at listing the upsides of having type hints in the code itself:
One example of this that comes to mind is
CloseReason.reasonwhich was documented as a string but was actually optional.Here's a relevant part of a diff between stubs extracted from trio-websocket now vs the ones I had included in my project mentioned earlier (left is new stubs, right is old stubs)
Also, I had
CloseReason.reasonas an non-optional string.I'll be honest, I had trouble finding a project using code search that used both mypy and trio-websocket.
However, if I only try to get trio-websocket, then I find some stuff like https://github.com/AcckiyGerman/python_trio_websockets_stress_test
Here's what I get as mypy output:
which isn't great yeah.
If I annotate the function arguments (oops, though
check-untyped-defsshould have helped a bit maybe)... then it still doesn't find anything. Perhaps this project was too trivial to check against.So I invite you to try yourself, but I believe this holds true? (If you want to try against some project of yours that has more complex usage then that would be nice)
I'll definitely rebase soon + try to clean up unnecessary type hints, of course.