Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tomlkit/exceptions.py: 86%

92 statements  

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

1from typing import Collection 

2from typing import Optional 

3 

4 

5class TOMLKitError(Exception): 

6 

7 pass 

8 

9 

10class ParseError(ValueError, TOMLKitError): 

11 """ 

12 This error occurs when the parser encounters a syntax error 

13 in the TOML being parsed. The error references the line and 

14 location within the line where the error was encountered. 

15 """ 

16 

17 def __init__(self, line: int, col: int, message: Optional[str] = None) -> None: 

18 self._line = line 

19 self._col = col 

20 

21 if message is None: 

22 message = "TOML parse error" 

23 

24 super().__init__(f"{message} at line {self._line} col {self._col}") 

25 

26 @property 

27 def line(self): 

28 return self._line 

29 

30 @property 

31 def col(self): 

32 return self._col 

33 

34 

35class MixedArrayTypesError(ParseError): 

36 """ 

37 An array was found that had two or more element types. 

38 """ 

39 

40 def __init__(self, line: int, col: int) -> None: 

41 message = "Mixed types found in array" 

42 

43 super().__init__(line, col, message=message) 

44 

45 

46class InvalidNumberError(ParseError): 

47 """ 

48 A numeric field was improperly specified. 

49 """ 

50 

51 def __init__(self, line: int, col: int) -> None: 

52 message = "Invalid number" 

53 

54 super().__init__(line, col, message=message) 

55 

56 

57class InvalidDateTimeError(ParseError): 

58 """ 

59 A datetime field was improperly specified. 

60 """ 

61 

62 def __init__(self, line: int, col: int) -> None: 

63 message = "Invalid datetime" 

64 

65 super().__init__(line, col, message=message) 

66 

67 

68class InvalidDateError(ParseError): 

69 """ 

70 A date field was improperly specified. 

71 """ 

72 

73 def __init__(self, line: int, col: int) -> None: 

74 message = "Invalid date" 

75 

76 super().__init__(line, col, message=message) 

77 

78 

79class InvalidTimeError(ParseError): 

80 """ 

81 A date field was improperly specified. 

82 """ 

83 

84 def __init__(self, line: int, col: int) -> None: 

85 message = "Invalid time" 

86 

87 super().__init__(line, col, message=message) 

88 

89 

90class InvalidNumberOrDateError(ParseError): 

91 """ 

92 A numeric or date field was improperly specified. 

93 """ 

94 

95 def __init__(self, line: int, col: int) -> None: 

96 message = "Invalid number or date format" 

97 

98 super().__init__(line, col, message=message) 

99 

100 

101class InvalidUnicodeValueError(ParseError): 

102 """ 

103 A unicode code was improperly specified. 

104 """ 

105 

106 def __init__(self, line: int, col: int) -> None: 

107 message = "Invalid unicode value" 

108 

109 super().__init__(line, col, message=message) 

110 

111 

112class UnexpectedCharError(ParseError): 

113 """ 

114 An unexpected character was found during parsing. 

115 """ 

116 

117 def __init__(self, line: int, col: int, char: str) -> None: 

118 message = f"Unexpected character: {repr(char)}" 

119 

120 super().__init__(line, col, message=message) 

121 

122 

123class EmptyKeyError(ParseError): 

124 """ 

125 An empty key was found during parsing. 

126 """ 

127 

128 def __init__(self, line: int, col: int) -> None: 

129 message = "Empty key" 

130 

131 super().__init__(line, col, message=message) 

132 

133 

134class EmptyTableNameError(ParseError): 

135 """ 

136 An empty table name was found during parsing. 

137 """ 

138 

139 def __init__(self, line: int, col: int) -> None: 

140 message = "Empty table name" 

141 

142 super().__init__(line, col, message=message) 

143 

144 

145class InvalidCharInStringError(ParseError): 

146 """ 

147 The string being parsed contains an invalid character. 

148 """ 

149 

150 def __init__(self, line: int, col: int, char: str) -> None: 

151 message = f"Invalid character {repr(char)} in string" 

152 

153 super().__init__(line, col, message=message) 

154 

155 

156class UnexpectedEofError(ParseError): 

157 """ 

158 The TOML being parsed ended before the end of a statement. 

159 """ 

160 

161 def __init__(self, line: int, col: int) -> None: 

162 message = "Unexpected end of file" 

163 

164 super().__init__(line, col, message=message) 

165 

166 

167class InternalParserError(ParseError): 

168 """ 

169 An error that indicates a bug in the parser. 

170 """ 

171 

172 def __init__(self, line: int, col: int, message: Optional[str] = None) -> None: 

173 msg = "Internal parser error" 

174 if message: 

175 msg += f" ({message})" 

176 

177 super().__init__(line, col, message=msg) 

178 

179 

180class NonExistentKey(KeyError, TOMLKitError): 

181 """ 

182 A non-existent key was used. 

183 """ 

184 

185 def __init__(self, key): 

186 message = f'Key "{key}" does not exist.' 

187 

188 super().__init__(message) 

189 

190 

191class KeyAlreadyPresent(TOMLKitError): 

192 """ 

193 An already present key was used. 

194 """ 

195 

196 def __init__(self, key): 

197 key = getattr(key, "key", key) 

198 message = f'Key "{key}" already exists.' 

199 

200 super().__init__(message) 

201 

202 

203class InvalidControlChar(ParseError): 

204 def __init__(self, line: int, col: int, char: int, type: str) -> None: 

205 display_code = "\\u00" 

206 

207 if char < 16: 

208 display_code += "0" 

209 

210 display_code += hex(char)[2:] 

211 

212 message = ( 

213 "Control characters (codes less than 0x1f and 0x7f)" 

214 f" are not allowed in {type}, " 

215 f"use {display_code} instead" 

216 ) 

217 

218 super().__init__(line, col, message=message) 

219 

220 

221class InvalidStringError(ValueError, TOMLKitError): 

222 def __init__(self, value: str, invalid_sequences: Collection[str], delimiter: str): 

223 repr_ = repr(value)[1:-1] 

224 super().__init__( 

225 f"Invalid string: {delimiter}{repr_}{delimiter}. " 

226 f"The character sequences {invalid_sequences} are invalid." 

227 )