1
1
from __future__ import annotations
2
+
3
+ from collections .abc import Sequence
2
4
from typing import Any
3
5
4
6
from .alignment import Alignment
5
-
6
7
from .annotations import SupportsStr
7
8
8
9
9
10
class Table2AsciiError (Exception ):
10
11
"""Base class for all table2ascii exceptions"""
11
12
12
- def _message (self ):
13
+ def _message (self ) -> str :
13
14
"""Return the error message"""
14
15
raise NotImplementedError
15
16
@@ -39,16 +40,16 @@ class FooterColumnCountMismatchError(ColumnCountMismatchError):
39
40
This class is a subclass of :class:`ColumnCountMismatchError`.
40
41
41
42
Attributes:
42
- footer (list [SupportsStr]): The footer that caused the error
43
+ footer (Sequence [SupportsStr]): The footer that caused the error
43
44
expected_columns (int): The number of columns that were expected
44
45
"""
45
46
46
- def __init__ (self , footer : list [SupportsStr ], expected_columns : int ):
47
+ def __init__ (self , footer : Sequence [SupportsStr ], expected_columns : int ):
47
48
self .footer = footer
48
49
self .expected_columns = expected_columns
49
50
super ().__init__ (self ._message ())
50
51
51
- def _message (self ):
52
+ def _message (self ) -> str :
52
53
return (
53
54
f"Footer column count mismatch: { len (self .footer )} columns "
54
55
f"found, expected { self .expected_columns } ."
@@ -62,20 +63,20 @@ class BodyColumnCountMismatchError(ColumnCountMismatchError):
62
63
This class is a subclass of :class:`ColumnCountMismatchError`.
63
64
64
65
Attributes:
65
- body (list[list [SupportsStr]]): The body that caused the error
66
+ body (Sequence[Sequence [SupportsStr]]): The body that caused the error
66
67
expected_columns (int): The number of columns that were expected
67
- first_invalid_row (list [SupportsStr]): The first row with an invalid column count
68
+ first_invalid_row (Sequence [SupportsStr]): The first row with an invalid column count
68
69
"""
69
70
70
- def __init__ (self , body : list [ list [SupportsStr ]], expected_columns : int ):
71
+ def __init__ (self , body : Sequence [ Sequence [SupportsStr ]], expected_columns : int ):
71
72
self .body = body
72
73
self .expected_columns = expected_columns
73
74
self .first_invalid_row = next (
74
75
(row for row in self .body if len (row ) != self .expected_columns )
75
76
)
76
77
super ().__init__ (self ._message ())
77
78
78
- def _message (self ):
79
+ def _message (self ) -> str :
79
80
return (
80
81
f"Body column count mismatch: A row with { len (self .first_invalid_row )} "
81
82
f"columns was found, expected { self .expected_columns } ."
@@ -89,16 +90,16 @@ class AlignmentCountMismatchError(ColumnCountMismatchError):
89
90
This class is a subclass of :class:`ColumnCountMismatchError`.
90
91
91
92
Attributes:
92
- alignments (list [Alignment]): The alignments that caused the error
93
+ alignments (Sequence [Alignment]): The alignments that caused the error
93
94
expected_columns (int): The number of columns that were expected
94
95
"""
95
96
96
- def __init__ (self , alignments : list [Alignment ], expected_columns : int ):
97
+ def __init__ (self , alignments : Sequence [Alignment ], expected_columns : int ):
97
98
self .alignments = alignments
98
99
self .expected_columns = expected_columns
99
100
super ().__init__ (self ._message ())
100
101
101
- def _message (self ):
102
+ def _message (self ) -> str :
102
103
return (
103
104
f"Alignment count mismatch: { len (self .alignments )} alignments "
104
105
f"found, expected { self .expected_columns } ."
@@ -112,22 +113,35 @@ class ColumnWidthsCountMismatchError(ColumnCountMismatchError):
112
113
This class is a subclass of :class:`ColumnCountMismatchError`.
113
114
114
115
Attributes:
115
- column_widths (list [Optional[int]]): The column widths that caused the error
116
+ column_widths (Sequence [Optional[int]]): The column widths that caused the error
116
117
expected_columns (int): The number of columns that were expected
117
118
"""
118
119
119
- def __init__ (self , column_widths : list [int | None ], expected_columns : int ):
120
+ def __init__ (self , column_widths : Sequence [int | None ], expected_columns : int ):
120
121
self .column_widths = column_widths
121
122
self .expected_columns = expected_columns
122
123
super ().__init__ (self ._message ())
123
124
124
- def _message (self ):
125
+ def _message (self ) -> str :
125
126
return (
126
127
f"Column widths count mismatch: { len (self .column_widths )} column widths "
127
128
f"found, expected { self .expected_columns } ."
128
129
)
129
130
130
131
132
+ class NoHeaderBodyOrFooterError (TableOptionError ):
133
+ """Exception raised when no header, body or footer is provided
134
+
135
+ This class is a subclass of :class:`TableOptionError`.
136
+ """
137
+
138
+ def __init__ (self ):
139
+ super ().__init__ (self ._message ())
140
+
141
+ def _message (self ) -> str :
142
+ return "At least one of header, body or footer must be provided."
143
+
144
+
131
145
class InvalidCellPaddingError (TableOptionError ):
132
146
"""Exception raised when the cell padding is invalid
133
147
@@ -141,7 +155,7 @@ def __init__(self, padding: int):
141
155
self .padding = padding
142
156
super ().__init__ (self ._message ())
143
157
144
- def _message (self ):
158
+ def _message (self ) -> str :
145
159
return f"Invalid cell padding: { self .padding } is not a positive integer."
146
160
147
161
@@ -163,7 +177,7 @@ def __init__(self, column_index: int, column_width: int, min_width: int):
163
177
self .min_width = min_width
164
178
super ().__init__ (self ._message ())
165
179
166
- def _message (self ):
180
+ def _message (self ) -> str :
167
181
return (
168
182
f"Column width too small: The column width for column index { self .column_index } "
169
183
f" of `column_widths` is { self .column_width } , but the minimum width "
@@ -184,7 +198,7 @@ def __init__(self, alignment: Any):
184
198
self .alignment = alignment
185
199
super ().__init__ (self ._message ())
186
200
187
- def _message (self ):
201
+ def _message (self ) -> str :
188
202
return (
189
203
f"Invalid alignment: { self .alignment !r} is not a valid alignment. "
190
204
f"Valid alignments are: { ', ' .join (a .__repr__ () for a in Alignment )} "
@@ -208,7 +222,7 @@ def __init__(self, string: str, max_characters: int):
208
222
self .max_characters = max_characters
209
223
super ().__init__ (self ._message ())
210
224
211
- def _message (self ):
225
+ def _message (self ) -> str :
212
226
return (
213
227
f"Too many characters for table style: { len (self .string )} characters "
214
228
f"found, but the maximum number of characters allowed is { self .max_characters } ."
@@ -234,7 +248,7 @@ def __init__(self, string: str, max_characters: int):
234
248
self .max_characters = max_characters
235
249
super ().__init__ (self ._message ())
236
250
237
- def _message (self ):
251
+ def _message (self ) -> str :
238
252
return (
239
253
f"Too few characters for table style: { len (self .string )} characters "
240
254
f"found, but table styles can accept { self .max_characters } characters. "
0 commit comments