Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/jedi/api/keywords.py: 34%

32 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:07 +0000

1import pydoc 

2from contextlib import suppress 

3from typing import Dict, Optional 

4 

5from jedi.inference.names import AbstractArbitraryName 

6 

7try: 

8 # https://github.com/python/typeshed/pull/4351 adds pydoc_data 

9 from pydoc_data import topics # type: ignore[import] 

10 pydoc_topics: Optional[Dict[str, str]] = topics.topics 

11except ImportError: 

12 # Python 3.6.8 embeddable does not have pydoc_data. 

13 pydoc_topics = None 

14 

15 

16class KeywordName(AbstractArbitraryName): 

17 api_type = 'keyword' 

18 

19 def py__doc__(self): 

20 return imitate_pydoc(self.string_name) 

21 

22 

23def imitate_pydoc(string): 

24 """ 

25 It's not possible to get the pydoc's without starting the annoying pager 

26 stuff. 

27 """ 

28 if pydoc_topics is None: 

29 return '' 

30 

31 h = pydoc.help 

32 with suppress(KeyError): 

33 # try to access symbols 

34 string = h.symbols[string] 

35 string, _, related = string.partition(' ') 

36 

37 def get_target(s): 

38 return h.topics.get(s, h.keywords.get(s)) 

39 

40 while isinstance(string, str): 

41 string = get_target(string) 

42 

43 try: 

44 # is a tuple now 

45 label, related = string 

46 except TypeError: 

47 return '' 

48 

49 try: 

50 return pydoc_topics[label].strip() if pydoc_topics else '' 

51 except KeyError: 

52 return ''