From 6857f4454f035803f3db9b21f9f3e7983323fbb0 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Thu, 14 Sep 2023 17:13:26 -0700 Subject: [PATCH 1/2] Add PositionEncodingKind hook --- packages/python/lsprotocol/_hooks.py | 18 ++++++++++++++ tests/python/test_cattrs_special_cases.py | 29 +++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/packages/python/lsprotocol/_hooks.py b/packages/python/lsprotocol/_hooks.py index 2a6a44d..db88336 100644 --- a/packages/python/lsprotocol/_hooks.py +++ b/packages/python/lsprotocol/_hooks.py @@ -146,6 +146,15 @@ def _references_provider_hook( return object_ return converter.structure(object_, lsp_types.ReferenceOptions) + def _position_encoding_hook( + object_: Any, _: type + ) -> Union[lsp_types.PositionEncodingKind, OptionalPrimitive]: + if object_ is None: + return None + if isinstance(object_, (bool, int, str, float)): + return object_ + return converter.structure(object_, lsp_types.ReferenceOptions) + def _document_highlight_provider_hook( object_: Any, _: type ) -> Union[OptionalPrimitive, lsp_types.DocumentHighlightOptions]: @@ -926,6 +935,15 @@ def _notebook_sync_option_selector_hook( ], _notebook_sync_option_selector_hook, ), + ( + Optional[ + Union[ + lsp_types.PositionEncodingKind, + str, + ] + ], + _position_encoding_hook, + ), ] for type_, hook in structure_hooks: converter.register_structure_hook(type_, hook) diff --git a/tests/python/test_cattrs_special_cases.py b/tests/python/test_cattrs_special_cases.py index ac52650..b25033f 100644 --- a/tests/python/test_cattrs_special_cases.py +++ b/tests/python/test_cattrs_special_cases.py @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +from typing import Optional, Union +import attrs import hamcrest import pytest from cattrs.errors import ClassValidationError @@ -274,3 +276,30 @@ def test_notebook_sync_options(): converter.unstructure(obj, lsp.NotebookDocumentSyncOptions), hamcrest.is_(data), ) + + +@attrs.define +class TestPosEncoding: + """Defines the capabilities provided by a language + server.""" + + position_encoding: Optional[Union[lsp.PositionEncodingKind, str]] = attrs.field( + default=None + ) + + +@pytest.mark.parametrize("e", [None, "utf-8", "utf-16", "utf-32", "something"]) +def test_position_encoding_kind(e): + data = {"positionEncoding": e} + converter = cv.get_converter() + obj = converter.structure(data, TestPosEncoding) + hamcrest.assert_that(obj, hamcrest.instance_of(TestPosEncoding)) + + if e is None: + hamcrest.assert_that( + converter.unstructure(obj, TestPosEncoding), hamcrest.is_({}) + ) + else: + hamcrest.assert_that( + converter.unstructure(obj, TestPosEncoding), hamcrest.is_(data) + ) From 8bc25eb1383e902c328f8d4a400eab5e8992e373 Mon Sep 17 00:00:00 2001 From: Karthik Nadig Date: Thu, 14 Sep 2023 17:17:11 -0700 Subject: [PATCH 2/2] Fix formatting --- packages/python/lsprotocol/_hooks.py | 8 ++------ tests/python/test_cattrs_special_cases.py | 1 + 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/python/lsprotocol/_hooks.py b/packages/python/lsprotocol/_hooks.py index db88336..dbd51f9 100644 --- a/packages/python/lsprotocol/_hooks.py +++ b/packages/python/lsprotocol/_hooks.py @@ -147,13 +147,9 @@ def _references_provider_hook( return converter.structure(object_, lsp_types.ReferenceOptions) def _position_encoding_hook( - object_: Any, _: type + object_: Union[lsp_types.PositionEncodingKind, OptionalPrimitive], _: type ) -> Union[lsp_types.PositionEncodingKind, OptionalPrimitive]: - if object_ is None: - return None - if isinstance(object_, (bool, int, str, float)): - return object_ - return converter.structure(object_, lsp_types.ReferenceOptions) + return object_ def _document_highlight_provider_hook( object_: Any, _: type diff --git a/tests/python/test_cattrs_special_cases.py b/tests/python/test_cattrs_special_cases.py index b25033f..c5e0e9c 100644 --- a/tests/python/test_cattrs_special_cases.py +++ b/tests/python/test_cattrs_special_cases.py @@ -2,6 +2,7 @@ # Licensed under the MIT License. from typing import Optional, Union + import attrs import hamcrest import pytest