Coverage for src/debputy/lsp/vendoring/_deb822_repro/types.py: 85%

26 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-04-07 12:14 +0200

1try: 

2 from typing import TypeVar, Union, Tuple, List, Callable, Iterator, TYPE_CHECKING 

3 

4 if TYPE_CHECKING: 

5 from .tokens import Deb822Token, Deb822FieldNameToken 

6 from .parsing import ( 

7 Deb822Element, 

8 Deb822CommentElement, 

9 Deb822ParsedValueElement, 

10 ) 

11 from .formatter import FormatterContentToken 

12 

13 TokenOrElement = Union["Deb822Element", "Deb822Token"] 

14 TE = TypeVar("TE", bound=TokenOrElement) 

15 

16 # Used as a resulting element for "mapping" functions that map TE -> R (see _combine_parts) 

17 R = TypeVar("R", bound="Deb822Element") 

18 

19 VE = TypeVar("VE", bound="Deb822Element") 

20 

21 ST = TypeVar("ST", bound="Deb822Token") 

22 

23 # Internal type for part of the paragraph key. Used to facility _unpack_key. 

24 ParagraphKeyBase = Union["Deb822FieldNameToken", str] 

25 

26 ParagraphKey = Union[ParagraphKeyBase, Tuple[str, int]] 

27 

28 Commentish = Union[List[str], "Deb822CommentElement"] 

29 

30 FormatterCallback = Callable[ 

31 [str, "FormatterContentToken", Iterator["FormatterContentToken"]], 

32 Iterator[Union["FormatterContentToken", str]], 

33 ] 

34 try: 

35 # Set __doc__ attributes if possible 

36 TE.__doc__ = """ 

37 Generic "Token or Element" type 

38 """ 

39 R.__doc__ = """ 

40 For internal usage in _deb822_repro 

41 """ 

42 VE.__doc__ = """ 

43 Value type/element in a list interpretation of a field value 

44 """ 

45 ST.__doc__ = """ 

46 Separator type/token in a list interpretation of a field value 

47 """ 

48 ParagraphKeyBase.__doc__ = """ 

49 For internal usage in _deb822_repro 

50 """ 

51 ParagraphKey.__doc__ = """ 

52 Anything accepted as a key for a paragraph field lookup. The simple case being 

53 a str. Alternative variants are mostly interesting for paragraphs with repeated 

54 fields (to enable unambiguous lookups) 

55 """ 

56 Commentish.__doc__ = """ 

57 Anything accepted as input for a Comment. The simple case is the list 

58 of string (each element being a line of comment). The alternative format is 

59 there for enable reuse of an existing element (e.g. to avoid "unpacking" 

60 only to "re-pack" an existing comment element). 

61 """ 

62 FormatterCallback.__doc__ = """\ 

63 Formatter callback used with the round-trip safe parser 

64 

65 See debian._repro_deb822.formatter.format_field for details 

66 """ 

67 except AttributeError: 

68 # Python 3.5 does not allow update to the __doc__ attribute - ignore that 

69 pass 

70except ImportError: 

71 pass 

72 

73 

74class AmbiguousDeb822FieldKeyError(KeyError): 

75 """Specialized version of KeyError to denote a valid but ambiguous field name 

76 

77 This exception occurs if: 

78 * the field is accessed via a str on a configured view that does not automatically 

79 resolve ambiguous field names (see Deb822ParagraphElement.configured_view), AND 

80 * a concrete paragraph contents a repeated field (which is not valid in deb822 

81 but the module supports parsing them) 

82 

83 Note that the default is to automatically resolve ambiguous fields. Accordingly 

84 you will only see this exception if you have "opted in" on wanting to know that 

85 the lookup was ambiguous. 

86 

87 The ambiguity can be resolved by using a tuple of (<field-name>, <filed-index>) 

88 instead of <field-name>. 

89 """ 

90 

91 

92class SyntaxOrParseError(ValueError): 

93 """Specialized version of ValueError for syntax/parse errors."""