11from math import ceil , floor
2- from typing import Any , Callable , List , Optional , Union
2+ from typing import Callable , List , Optional , Union
33
44from .alignment import Alignment
5+ from .annotations import SupportsStr
56from .options import Options
67from .preset_style import PresetStyle
78from .table_style import TableStyle
@@ -12,9 +13,9 @@ class TableToAscii:
1213
1314 def __init__ (
1415 self ,
15- header : Optional [List [Any ]],
16- body : Optional [List [List [Any ]]],
17- footer : Optional [List [Any ]],
16+ header : Optional [List [SupportsStr ]],
17+ body : Optional [List [List [SupportsStr ]]],
18+ footer : Optional [List [SupportsStr ]],
1819 options : Options ,
1920 ):
2021 """
@@ -103,7 +104,9 @@ def widest_line(text: str) -> int:
103104 # get the width necessary for each column
104105 for i in range (self .__columns ):
105106 # col_widest returns the width of the widest line in the ith cell of a given list
106- col_widest : Callable [[List [Any ], int ], int ] = lambda row , i = i : widest_line (str (row [i ]))
107+ col_widest : Callable [[List [SupportsStr ], int ], int ] = lambda row , i = i : widest_line (
108+ str (row [i ])
109+ )
107110 # number of characters in column of i of header, each body row, and footer
108111 header_size = col_widest (self .__header ) if self .__header else 0
109112 body_size = map (col_widest , self .__body ) if self .__body else [0 ]
@@ -112,7 +115,7 @@ def widest_line(text: str) -> int:
112115 column_widths .append (max (header_size , * body_size , footer_size ) + 2 )
113116 return column_widths
114117
115- def __pad (self , cell_value : Any , width : int , alignment : Alignment ) -> str :
118+ def __pad (self , cell_value : SupportsStr , width : int , alignment : Alignment ) -> str :
116119 """
117120 Pad a string of text to a given width with specified alignment
118121
@@ -258,7 +261,7 @@ def __heading_sep_to_ascii(self) -> str:
258261 filler = self .__style .heading_row_sep ,
259262 )
260263
261- def __body_to_ascii (self , body : List [List [Any ]]) -> str :
264+ def __body_to_ascii (self , body : List [List [SupportsStr ]]) -> str :
262265 """
263266 Assembles the body of the ascii table
264267
@@ -310,9 +313,9 @@ def to_ascii(self) -> str:
310313
311314
312315def table2ascii (
313- header : Optional [List [Any ]] = None ,
314- body : Optional [List [List [Any ]]] = None ,
315- footer : Optional [List [Any ]] = None ,
316+ header : Optional [List [SupportsStr ]] = None ,
317+ body : Optional [List [List [SupportsStr ]]] = None ,
318+ footer : Optional [List [SupportsStr ]] = None ,
316319 * ,
317320 first_col_heading : bool = False ,
318321 last_col_heading : bool = False ,
@@ -324,12 +327,12 @@ def table2ascii(
324327 Convert a 2D Python table to ASCII text
325328
326329 Args:
327- header: List of column values in the table's header row. If not specified,
328- the table will not have a header row.
329- body: 2-dimensional list of values in the table's body. If not specified,
330- the table will not have a body.
331- footer: List of column values in the table's footer row. If not specified,
332- the table will not have a footer row.
330+ header: List of column values in the table's header row. All values should be :class:`str`
331+ or support :class:`str` conversion. If not specified, the table will not have a header row.
332+ body: 2-dimensional list of values in the table's body. All values should be :class:`str`
333+ or support :class:`str` conversion. If not specified, the table will not have a body.
334+ footer: List of column values in the table's footer row. All values should be :class:`str`
335+ or support :class:`str` conversion. If not specified, the table will not have a footer row.
333336 first_col_heading: Whether to add a header column separator after the first column.
334337 Defaults to :py:obj:`False`.
335338 last_col_heading: Whether to add a header column separator before the last column.
0 commit comments