lsp testing: validate AnyExpected
types to correctly represent the union of expected_* module types
This commit is contained in:
parent
8d297eb9e5
commit
39218db8cd
4 changed files with 19 additions and 1 deletions
11
tests/_expected_utils.py
Normal file
11
tests/_expected_utils.py
Normal 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})."
|
||||
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import typing
|
||||
from enum import Enum
|
||||
|
||||
import lsprotocol.types as lspt
|
||||
import expected_hover
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue