-
-
Notifications
You must be signed in to change notification settings - Fork 106
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
types: use enum class for enums #92
Conversation
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.
Looks great. I didn't run mypy
for type checking before, so I am glad you have found and fixed those issues. Thanks much!
After we merge #90, you can update the changelog as well (to avoid conflicts).
pygls/protocol.py
Outdated
@@ -369,8 +370,12 @@ def _send_data(self, data): | |||
if not data: | |||
return | |||
|
|||
def default(o): |
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.
Could you just move this function under call_user_feature
(line 70.) and perhaps rename it to default_serializer
or something similar?
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.
Thanks, good idea, I pushed more fixup commits to address this.
@perrinjerome PR #90 is merged. If you feel comfortable, you can rebase branch with master and update the changelog for this PR. |
Thanks @danixeee . I was not familiar with For example when language server is implemented as: @server.feature(COMPLETION, trigger_characters=[','])
def completions(params: CompletionParams = None) -> CompletionList:
"""Returns completion items."""
return CompletionList(False, []) what this function receive as Line 83 in df8950d
This also means that @self.feature(COMPLETION, trigger_characters=['{', '('])
def completions(params: CompletionParams):
if (params.context.triggerKind ==
CompletionTriggerKind.TriggerCharacter): will be always false, because int are different from enum. This small issue also interesting in context of #81 But the problem seems that I wrongly used
So I will amend this patch to use I will also change the string enums to inherit from I will also revisit the |
That's what I did as part of c062341 but |
Using enums allows more precise type checking. The current code in types.py was already using *Kind classes as if they were enums, which mypy reported as type errors: pygls/types.py:377: error: Incompatible default for argument "severity" (default has type "int", argument has type "DiagnosticSeverity") pygls/types.py:663: error: Incompatible default for argument "trace" (default has type "str", argument has type "Optional[Trace]") By using enum.IntEnum, enum.IntFlag or subclassing both str and enum.Enum for string enums, we ensure equality with protocol object deserialized as simple int or strings. To keep compatibility with python 3.5 which does not support enum.IntFlag, WatchKind is not implemented as an enum on python 3.5.
354ff54
to
c2b4419
Compare
I amended to support python 3.5 , the change compared to previous version was 354ff54 . Then I rebased on master, squashed all the intermediate "fixup" commits from this branch, added an entry in change log, edited commit message to explain the need for Please take another look, I think this is OK now. |
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.
@perrinjerome I was quite busy these days, sorry for waiting for the approval. Great work and explanation, I am now more familiar with enums. :)
EDIT: You can now update #89, so we can merge it as well.
Thank you @danixeee . I also learned about enums in the process :) Don't worry about the delay, everybody is busy. |
Description
Using enums allows more precise type checking. The current code in types.py was already using *Kind classes as if they were enums, which mypy reported as type errors:
WatchKind is handled a bit differently, because valid values can be combined with |, like the default value 7.
Code review checklist (for code reviewer to complete)