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

Remove dependency on typeguard #411

Merged
merged 4 commits into from
Nov 10, 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
244 changes: 130 additions & 114 deletions poetry.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions pygls/feature_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,13 @@ class FeatureManager:
2. name: LanguageServer - add typings
"""

def __init__(self, server=None):
def __init__(self, server=None, converter=None):
self._builtin_features = {}
self._feature_options = {}
self._features = {}
self._commands = {}
self.server = server
self.converter = converter

def add_builtin_feature(self, feature_name: str, func: Callable) -> None:
"""Registers builtin (predefined) feature."""
Expand Down Expand Up @@ -189,7 +190,9 @@ def decorator(f):

if options:
options_type = get_method_options_type(feature_name)
if options_type and not is_instance(options, options_type):
if options_type and not is_instance(
self.converter, options, options_type
):
raise TypeError(
(
f'Options of method "{feature_name}"'
Expand Down
6 changes: 3 additions & 3 deletions pygls/lsp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and #
# limitations under the License. #
############################################################################
import cattrs
from typing import Any, Callable, List, Optional, Union

from lsprotocol.types import (
Expand All @@ -35,7 +36,6 @@
SemanticTokensRegistrationOptions,
ShowDocumentResult,
)
from typeguard import check_type

from pygls.exceptions import MethodTypeNotRegisteredError

Expand Down Expand Up @@ -131,9 +131,9 @@ def get_method_return_type(method_name, lsp_methods_map=METHOD_TO_TYPES):
raise MethodTypeNotRegisteredError(method_name)


def is_instance(o, t):
def is_instance(cv: cattrs.Converter, o, t):
try:
check_type(o, t)
cv.unstructure(o, t)
return True
except TypeError:
return False
2 changes: 1 addition & 1 deletion pygls/protocol/json_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def __init__(self, server: LanguageServer, converter):
self._request_futures: Dict[str, Future[Any]] = {}
self._result_types: Dict[str, Any] = {}

self.fm = FeatureManager(server)
self.fm = FeatureManager(server, converter)
self.transport: Optional[
Union[asyncio.WriteTransport, WebSocketTransportAdapter]
] = None
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ readme = "README.md"
[tool.poetry.dependencies]
python = ">=3.7.9,<4"
lsprotocol = "2023.0.0b1"
typeguard = ">=3.0.0, <5"
websockets = {version = "^11.0.3", optional = true}

[tool.poetry.extras]
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import sys

import pytest
from lsprotocol import types
from lsprotocol import types, converters

from pygls import uris, IS_PYODIDE, IS_WIN
from pygls.feature_manager import FeatureManager
Expand Down Expand Up @@ -113,7 +113,7 @@ def server_dir():
@pytest.fixture
def feature_manager():
"""Return a feature manager"""
return FeatureManager()
return FeatureManager(None, converters.get_converter())


@pytest.fixture
Expand Down
9 changes: 2 additions & 7 deletions tests/test_feature_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
wrap_with_server,
)
from lsprotocol import types as lsp
from typeguard import TypeCheckError
from typeguard._utils import qualified_name


class Temp:
Expand Down Expand Up @@ -104,11 +102,8 @@ class Options:
pass

with pytest.raises(
TypeCheckError,
match=(
f"{qualified_name(Options)} is not an instance of "
"lsprotocol.types.CompletionOptions"
), # noqa
AttributeError,
match=("'Options' object has no attribute 'trigger_characters'"), # noqa
):

@feature_manager.feature(lsp.TEXT_DOCUMENT_COMPLETION, Options())
Expand Down
Loading