1
Fork 0

lsp testing: validate AnyExpected types to correctly represent the union of expected_* module types

This commit is contained in:
automaticp 2023-10-24 19:47:23 +06:00
commit 39218db8cd
4 changed files with 19 additions and 1 deletions

11
tests/_expected_utils.py Normal file
View file

@ -0,0 +1,11 @@
import sys, inspect, typing
# Yes, we validate the type alias that is used for validation later.
# Yes, I also considered testing the testing framework itself. It's a good idea, I agree.
def validate_any_expected(module_name: str, any_expected: typing.TypeAlias):
types_in_module = set(c[1] for c in inspect.getmembers(sys.modules[module_name], inspect.isclass))
types_in_any_expected = set(typing.get_args(any_expected))
assert types_in_module == types_in_any_expected, \
f"{module_name}.AnyExpected types ({types_in_any_expected}) are not the same as the types in the module {module_name} ({types_in_module})."

View file

@ -1,5 +1,6 @@
from dataclasses import dataclass
import typing
from _expected_utils import validate_any_expected
@dataclass
@ -11,6 +12,7 @@ class Generic:
# This has to contain all of the types in this module.
# We cannot do this programmatically, unfortunately.
#
# TODO: Switch to Union once there's more than one class here.
# Wrapping in a typing.Type is needed for get_args() to return
@ -19,3 +21,5 @@ AnyExpected = typing.Type[Generic]
def is_any_expected(expected: AnyExpected) -> bool:
return isinstance(expected, typing.get_args(AnyExpected))
validate_any_expected(__name__, AnyExpected)

View file

@ -1,5 +1,6 @@
from dataclasses import dataclass
import typing
from _expected_utils import validate_any_expected
@dataclass
@ -17,7 +18,10 @@ class FunctionIdent:
# This has to contain all of the types in this module.
# We cannot do this programmatically, unfortunately.
AnyExpected = Generic | FunctionIdent
def is_any_expected(expected: AnyExpected) -> bool:
return isinstance(expected, typing.get_args(AnyExpected))
validate_any_expected(__name__, AnyExpected)

View file

@ -1,5 +1,4 @@
import typing
from enum import Enum
import lsprotocol.types as lspt
import expected_hover