Coverage for src/debputy/lsprotocol/types.py: 22%

36 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-10-12 15:06 +0000

1"""Wrapper module around `lsprotocol.types` 

2 

3This wrapper module is here to facility making `lsprotocol` optional 

4for -backports. When available, it is a (mostly) transparent wrapper 

5for the real module. When missing, it returns a placeholder object 

6for anything for the purpose of making things simpler. 

7""" 

8 

9from types import GenericAlias 

10from typing import TYPE_CHECKING, Any 

11from collections.abc import Iterator 

12 

13if TYPE_CHECKING: 

14 from lsprotocol import types 

15 

16 # To defeat "unused" detections that might attempt to 

17 # optimize out the import 

18 assert types is not None 

19 __all__ = dir(types) 

20 

21else: 

22 try: 

23 from lsprotocol import types 

24 

25 __all__ = dir(types) 

26 

27 except ImportError: 

28 

29 stub_attr = { 

30 "__name__": __name__, 

31 "__file__": __file__, 

32 "__doc__": __doc__, 

33 } 

34 bad_attr = frozenset( 

35 [ 

36 "pytestmark", 

37 "pytest_plugins", 

38 ] 

39 ) 

40 

41 class StubModule: 

42 @staticmethod 

43 def __getattr__(item: Any) -> Any: 

44 if item in stub_attr: 

45 return stub_attr[item] 

46 if item in bad_attr: 

47 raise AttributeError(item) 

48 if isinstance(item, str) and item.startswith("TEXT_"): 

49 return str(item) 

50 return types 

51 

52 def __or__(self, other) -> Any: 

53 if isinstance(other, StubModule): 

54 return self.__class__ 

55 if ( 

56 other is None 

57 or isinstance(other, type) 

58 or type(other) in (GenericAlias,) 

59 ): 

60 return self.__class__ | other 

61 return NotImplemented 

62 

63 # Order is not significant for `X | Y` when it is typing related 

64 # and that is the only thing we support. 

65 __ror__ = __or__ 

66 

67 def __call__(self, *args, **kwargs) -> Any: 

68 return self 

69 

70 def __iter__(self) -> Iterator[Any]: 

71 return iter(()) 

72 

73 types = StubModule() 

74 __all__ = [] 

75 

76 

77def __dir__() -> list[str]: 

78 return dir(types) 

79 

80 

81def __getattr__(name: str) -> Any: 

82 return getattr(types, name)