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

Conversation

zanieb
Copy link
Contributor

@zanieb zanieb commented Jul 25, 2023

Description

The following example fails on main because the annotation is a string rather than a concrete type due to the use of from __future__ import annotations. typing.get_type_hints can be used to resolve the annotations for the signature to support detection in this case.

from __future__ import annotations

from pygls import server

async def example(
    foo: server.LanguageServer,
    params: None,
) -> None:
    pass


from pygls.feature_manager import has_ls_param_or_annotation

assert has_ls_param_or_annotation(example, server.LanguageServer)

This can also be reproduced by wrapping the annotation type in a string manually (as I do in the test in this pull request).

Code review checklist (for code reviewer to complete)

  • Pull request represents a single change (i.e. not fixing disparate/unrelated things in a single PR)
  • Title summarizes what is changing
  • Commit messages are meaningful (see this for details)
  • Tests have been included and/or updated, as appropriate
  • [ ] Docstrings have been included and/or updated, as appropriate
  • [ ] Standalone docs have been updated accordingly
  • CONTRIBUTORS.md was updated, as appropriate
  • Changelog has been updated, as needed (see CHANGELOG.md)

@tombh
Copy link
Collaborator

tombh commented Jul 25, 2023

Looks good ❤️

I'm happy to merge. But maybe @alcarney wants to give it a thumbs up too?

@@ -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.

@tombh tombh self-requested a review July 26, 2023 13:24
@tombh tombh merged commit 6139c71 into openlawlibrary:main Jul 26, 2023
@tombh
Copy link
Collaborator

tombh commented Jul 26, 2023

Thank you 🙇

@alcarney
Copy link
Collaborator

But maybe @alcarney wants to give it a thumbs up too?

Better late than never! 😅 Looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants