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

Add structure hook for Union[str, List[InlayHintLabelPart]] #219

Merged
merged 2 commits into from
Jun 21, 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
4 changes: 4 additions & 0 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ jobs:
run: python -m pip install -r ./packages/python/requirements.txt
shell: bash

- name: Install pygls
run: python -m pip install --no-deps ./smoke_tests
shell: bash

- name: Pip List
run: python -m pip list
shell: bash
Expand Down
14 changes: 14 additions & 0 deletions packages/python/lsprotocol/_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,16 @@ def _inlay_hint_provider_hook(
else:
return converter.structure(object_, lsp_types.InlayHintOptions)

def _inlay_hint_label_part_hook(
object_: Any, _: type
) -> Union[str, List[lsp_types.InlayHintLabelPart]]:
if isinstance(object_, str):
return object_

return [
converter.structure(item, lsp_types.InlayHintLabelPart) for item in object_
]

def _diagnostic_provider_hook(
object_: Any, _: type
) -> Union[
Expand Down Expand Up @@ -775,6 +785,10 @@ def _watch_kind_hook(
],
_inlay_hint_provider_hook,
),
(
Union[str, List[lsp_types.InlayHintLabelPart]],
_inlay_hint_label_part_hook,
),
(
Optional[
Union[
Expand Down
86 changes: 86 additions & 0 deletions tests/python/requests/test_inlay_hint_resolve_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import json
import uuid

import hamcrest
import jsonrpc
import pytest
from cattrs.errors import ClassValidationError

ID = str(uuid.uuid4())

TEST_DATA = [
(
{
"id": ID,
"method": "inlayHint/resolve",
"params": {
"position": {"line": 6, "character": 5},
"label": "a label",
"kind": 1,
"paddingLeft": False,
"paddingRight": True,
},
"jsonrpc": "2.0",
},
json.dumps(
{
"id": ID,
"params": {
"position": {"line": 6, "character": 5},
"label": "a label",
"kind": 1,
"paddingLeft": False,
"paddingRight": True,
},
"method": "inlayHint/resolve",
"jsonrpc": "2.0",
}
),
),
(
{
"id": ID,
"method": "inlayHint/resolve",
"params": {
"position": {"line": 6, "character": 5},
"label": [
{"value": "part 1"},
{"value": "part 2", "tooltip": "a tooltip"},
],
"kind": 1,
"paddingLeft": False,
"paddingRight": True,
},
"jsonrpc": "2.0",
},
json.dumps(
{
"id": ID,
"params": {
"position": {"line": 6, "character": 5},
"label": [
{"value": "part 1"},
{"value": "part 2", "tooltip": "a tooltip"},
],
"kind": 1,
"paddingLeft": False,
"paddingRight": True,
},
"method": "inlayHint/resolve",
"jsonrpc": "2.0",
}
),
),
]


@pytest.mark.parametrize("index", list(range(0, len(TEST_DATA))))
def test_inlay_hint_resolve_request_serialization(index):
data, expected = TEST_DATA[index]
data_str = json.dumps(data)
parsed = jsonrpc.from_json(data_str)
actual_str = jsonrpc.to_json(parsed)
hamcrest.assert_that(actual_str, hamcrest.is_(expected))