debputy.lsprotocol.types

src/debputy/lsprotocol/types.py
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
"""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 typing import TYPE_CHECKING, Any, Iterator, List

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 __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)