1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
"""Wrapper module around `lsprotocol.types`
This wrapper module is here to facility making `lsprotocol` optional
for -backports. When available, it is a (mostly) transparent wrapper
for the real module. When missing, it returns a placeholder object
for anything for the purpose of making things simpler.
"""
from types import GenericAlias
from typing import TYPE_CHECKING, Any
from collections.abc import Iterator
if TYPE_CHECKING:
from lsprotocol import types
# To defeat "unused" detections that might attempt to
# optimize out the import
assert types is not None
__all__ = dir(types)
else:
try:
from lsprotocol import types
__all__ = dir(types)
except ImportError:
stub_attr = {
"__name__": __name__,
"__file__": __file__,
"__doc__": __doc__,
}
bad_attr = frozenset(
[
"pytestmark",
"pytest_plugins",
]
)
class StubModule:
@staticmethod
def __getattr__(item: Any) -> Any:
if item in stub_attr:
return stub_attr[item]
if item in bad_attr:
raise AttributeError(item)
if isinstance(item, str) and item.startswith("TEXT_"):
return str(item)
return types
def __or__(self, other) -> Any:
if isinstance(other, StubModule):
return self.__class__
if (
other is None
or isinstance(other, type)
or type(other) in (GenericAlias,)
):
return self.__class__ | other
return NotImplemented
# Order is not significant for `X | Y` when it is typing related
# and that is the only thing we support.
__ror__ = __or__
def __call__(self, *args, **kwargs) -> Any:
return self
def __iter__(self) -> Iterator[Any]:
return iter(())
types = StubModule()
__all__ = []
def __dir__() -> list[str]:
return dir(types)
def __getattr__(name: str) -> Any:
return getattr(types, name)
|