11from __future__ import annotations
2+
3+ from collections .abc import Sequence
24from typing import Any
35
46from .alignment import Alignment
5-
67from .annotations import SupportsStr
78
89
910class Table2AsciiError (Exception ):
1011 """Base class for all table2ascii exceptions"""
1112
12- def _message (self ):
13+ def _message (self ) -> str :
1314 """Return the error message"""
1415 raise NotImplementedError
1516
@@ -39,16 +40,16 @@ class FooterColumnCountMismatchError(ColumnCountMismatchError):
3940 This class is a subclass of :class:`ColumnCountMismatchError`.
4041
4142 Attributes:
42- footer (list [SupportsStr]): The footer that caused the error
43+ footer (Sequence [SupportsStr]): The footer that caused the error
4344 expected_columns (int): The number of columns that were expected
4445 """
4546
46- def __init__ (self , footer : list [SupportsStr ], expected_columns : int ):
47+ def __init__ (self , footer : Sequence [SupportsStr ], expected_columns : int ):
4748 self .footer = footer
4849 self .expected_columns = expected_columns
4950 super ().__init__ (self ._message ())
5051
51- def _message (self ):
52+ def _message (self ) -> str :
5253 return (
5354 f"Footer column count mismatch: { len (self .footer )} columns "
5455 f"found, expected { self .expected_columns } ."
@@ -62,20 +63,20 @@ class BodyColumnCountMismatchError(ColumnCountMismatchError):
6263 This class is a subclass of :class:`ColumnCountMismatchError`.
6364
6465 Attributes:
65- body (list[list [SupportsStr]]): The body that caused the error
66+ body (Sequence[Sequence [SupportsStr]]): The body that caused the error
6667 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
6869 """
6970
70- def __init__ (self , body : list [ list [SupportsStr ]], expected_columns : int ):
71+ def __init__ (self , body : Sequence [ Sequence [SupportsStr ]], expected_columns : int ):
7172 self .body = body
7273 self .expected_columns = expected_columns
7374 self .first_invalid_row = next (
7475 (row for row in self .body if len (row ) != self .expected_columns )
7576 )
7677 super ().__init__ (self ._message ())
7778
78- def _message (self ):
79+ def _message (self ) -> str :
7980 return (
8081 f"Body column count mismatch: A row with { len (self .first_invalid_row )} "
8182 f"columns was found, expected { self .expected_columns } ."
@@ -89,16 +90,16 @@ class AlignmentCountMismatchError(ColumnCountMismatchError):
8990 This class is a subclass of :class:`ColumnCountMismatchError`.
9091
9192 Attributes:
92- alignments (list [Alignment]): The alignments that caused the error
93+ alignments (Sequence [Alignment]): The alignments that caused the error
9394 expected_columns (int): The number of columns that were expected
9495 """
9596
96- def __init__ (self , alignments : list [Alignment ], expected_columns : int ):
97+ def __init__ (self , alignments : Sequence [Alignment ], expected_columns : int ):
9798 self .alignments = alignments
9899 self .expected_columns = expected_columns
99100 super ().__init__ (self ._message ())
100101
101- def _message (self ):
102+ def _message (self ) -> str :
102103 return (
103104 f"Alignment count mismatch: { len (self .alignments )} alignments "
104105 f"found, expected { self .expected_columns } ."
@@ -112,22 +113,35 @@ class ColumnWidthsCountMismatchError(ColumnCountMismatchError):
112113 This class is a subclass of :class:`ColumnCountMismatchError`.
113114
114115 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
116117 expected_columns (int): The number of columns that were expected
117118 """
118119
119- def __init__ (self , column_widths : list [int | None ], expected_columns : int ):
120+ def __init__ (self , column_widths : Sequence [int | None ], expected_columns : int ):
120121 self .column_widths = column_widths
121122 self .expected_columns = expected_columns
122123 super ().__init__ (self ._message ())
123124
124- def _message (self ):
125+ def _message (self ) -> str :
125126 return (
126127 f"Column widths count mismatch: { len (self .column_widths )} column widths "
127128 f"found, expected { self .expected_columns } ."
128129 )
129130
130131
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+
131145class InvalidCellPaddingError (TableOptionError ):
132146 """Exception raised when the cell padding is invalid
133147
@@ -141,7 +155,7 @@ def __init__(self, padding: int):
141155 self .padding = padding
142156 super ().__init__ (self ._message ())
143157
144- def _message (self ):
158+ def _message (self ) -> str :
145159 return f"Invalid cell padding: { self .padding } is not a positive integer."
146160
147161
@@ -163,7 +177,7 @@ def __init__(self, column_index: int, column_width: int, min_width: int):
163177 self .min_width = min_width
164178 super ().__init__ (self ._message ())
165179
166- def _message (self ):
180+ def _message (self ) -> str :
167181 return (
168182 f"Column width too small: The column width for column index { self .column_index } "
169183 f" of `column_widths` is { self .column_width } , but the minimum width "
@@ -184,7 +198,7 @@ def __init__(self, alignment: Any):
184198 self .alignment = alignment
185199 super ().__init__ (self ._message ())
186200
187- def _message (self ):
201+ def _message (self ) -> str :
188202 return (
189203 f"Invalid alignment: { self .alignment !r} is not a valid alignment. "
190204 f"Valid alignments are: { ', ' .join (a .__repr__ () for a in Alignment )} "
@@ -208,7 +222,7 @@ def __init__(self, string: str, max_characters: int):
208222 self .max_characters = max_characters
209223 super ().__init__ (self ._message ())
210224
211- def _message (self ):
225+ def _message (self ) -> str :
212226 return (
213227 f"Too many characters for table style: { len (self .string )} characters "
214228 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):
234248 self .max_characters = max_characters
235249 super ().__init__ (self ._message ())
236250
237- def _message (self ):
251+ def _message (self ) -> str :
238252 return (
239253 f"Too few characters for table style: { len (self .string )} characters "
240254 f"found, but table styles can accept { self .max_characters } characters. "
0 commit comments