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

27 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2025-01-27 13:59 +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 typing import TYPE_CHECKING, Any, Iterator, List 

10 

11if TYPE_CHECKING: 

12 from lsprotocol import types 

13 

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

15 # optimize out the import 

16 assert types is not None 

17 __all__ = dir(types) 

18 

19else: 

20 try: 

21 from lsprotocol import types 

22 

23 __all__ = dir(types) 

24 

25 except ImportError: 

26 

27 stub_attr = { 

28 "__name__": __name__, 

29 "__file__": __file__, 

30 "__doc__": __doc__, 

31 } 

32 bad_attr = frozenset( 

33 [ 

34 "pytestmark", 

35 "pytest_plugins", 

36 ] 

37 ) 

38 

39 class StubModule: 

40 @staticmethod 

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

42 if item in stub_attr: 

43 return stub_attr[item] 

44 if item in bad_attr: 

45 raise AttributeError(item) 

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

47 return str(item) 

48 return types 

49 

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

51 return self 

52 

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

54 return iter(()) 

55 

56 types = StubModule() 

57 __all__ = [] 

58 

59 

60def __dir__() -> List[str]: 

61 return dir(types) 

62 

63 

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

65 return getattr(types, name)