Skip to content
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

Fix detection of LanguageServer type annotation when future annotations are used #352

Merged
merged 4 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning][semver].

- `pygls` no longer overrides the event loop for the current thread when given an explicit loop to use. ([#334])
- Fixed `MethodTypeNotRegisteredError` when registering a `TEXT_DOCUMENT_DID_SAVE` feature with options. ([#338])
- Fixed detection of `LanguageServer` type annotations when using string-based annotations. ([#352])

[#328]: https://github.com/openlawlibrary/pygls/issues/328
[#334]: https://github.com/openlawlibrary/pygls/issues/334
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
- [Tomoya Tanjo](https://github.com/tom-tan)
- [bollwyvl](https://github.com/bollwyvl)
- [yorodm](https://github.com/yorodm)
- [Zanie Blue](https://github.com/zanieb)
4 changes: 2 additions & 2 deletions pygls/feature_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import inspect
import itertools
import logging
from typing import Any, Callable, Dict, Optional
from typing import Any, Callable, Dict, Optional, get_type_hints

from pygls.constants import (ATTR_COMMAND_TYPE, ATTR_EXECUTE_IN_THREAD, ATTR_FEATURE_TYPE,
ATTR_REGISTERED_NAME, ATTR_REGISTERED_TYPE, PARAM_LS)
Expand Down Expand Up @@ -49,7 +49,7 @@ def has_ls_param_or_annotation(f, annotation):
try:
sig = inspect.signature(f)
first_p = next(itertools.islice(sig.parameters.values(), 0, 1))
return first_p.name == PARAM_LS or first_p.annotation is annotation
return first_p.name == PARAM_LS or get_type_hints(f)[first_p.name] == annotation
Copy link
Contributor Author

@zanieb zanieb Jul 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I changed from is to == here by accident. I'm not sure is is "proper" here but I also don't think it matters.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I feel like == is most often the right choice, so it's probably a good change.

except Exception:
return False

Expand Down
10 changes: 7 additions & 3 deletions tests/test_feature_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@
from typeguard import TypeCheckError
from typeguard._utils import qualified_name

class Temp:
pass

def test_has_ls_param_or_annotation():
class Temp:
pass

def test_has_ls_param_or_annotation():
def f1(ls, a, b, c):
pass

def f2(temp: Temp, a, b, c):
pass

def f3(temp: "Temp", a, b, c):
pass

assert has_ls_param_or_annotation(f1, None)
assert has_ls_param_or_annotation(f2, Temp)
assert has_ls_param_or_annotation(f3, Temp)


def test_register_command_validation_error(feature_manager):
Expand Down