Skip to content

Commit

Permalink
Add type aliases for Align, VAlign, HeaderStyle (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Oct 15, 2024
1 parent e9bc9f3 commit 7401618
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/prettytable/prettytable.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import re
from collections.abc import Callable, Iterable, Sequence
from html.parser import HTMLParser
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING, Any, Final, Literal

if TYPE_CHECKING:
from sqlite3 import Cursor
Expand All @@ -60,9 +60,12 @@
DOUBLE_BORDER = 15
SINGLE_BORDER = 16
RANDOM = 20
BASE_ALIGN_VALUE = "base_align_value"
BASE_ALIGN_VALUE: Final = "base_align_value"

RowType: TypeAlias = list[Any]
AlignType: TypeAlias = Literal["l", "c", "r"]
VAlignType: TypeAlias = Literal["t", "m", "b"]
HeaderStyleType: TypeAlias = Literal["cap", "title", "upper", "lower", None]

_re = re.compile(r"\033\[[0-9;]*m|\033\(B")

Expand All @@ -76,6 +79,8 @@ def _get_size(text: str) -> tuple[int, int]:

class PrettyTable:
_xhtml: bool
_align: dict[str, AlignType]
_valign: dict[str, VAlignType]
_min_table_width: int | None
_max_table_width: int | None
_fields: Sequence[str | None] | None
Expand All @@ -86,6 +91,7 @@ class PrettyTable:
_reversesort: bool
_sort_key: Callable[[RowType], SupportsRichComparison]
_header: bool
_header_style: HeaderStyleType
_border: bool
_preserve_internal_border: bool
_padding_width: int
Expand Down Expand Up @@ -323,7 +329,7 @@ def __init__(self, field_names: Sequence[str] | None = None, **kwargs) -> None:
self._xhtml = kwargs["xhtml"] or False
self._attributes = kwargs["attributes"] or {}

def _justify(self, text: str, width: int, align) -> str:
def _justify(self, text: str, width: int, align: AlignType) -> str:
excess = width - _str_block_width(text)
if align == "l":
return text + excess * " "
Expand Down Expand Up @@ -517,9 +523,9 @@ def _validate_align(self, val):

def _validate_valign(self, val):
try:
assert val in ["t", "m", "b", None]
assert val in ["t", "m", "b"]
except AssertionError:
msg = f"Alignment {val} is invalid, use t, m, b or None"
msg = f"Alignment {val} is invalid, use t, m, b"
raise ValueError(msg)

def _validate_nonnegative_int(self, name, val):
Expand Down Expand Up @@ -894,7 +900,7 @@ def header(self, val: bool) -> None:
self._header = val

@property
def header_style(self):
def header_style(self) -> HeaderStyleType:
"""Controls stylisation applied to field names in header
Arguments:
Expand All @@ -904,7 +910,7 @@ def header_style(self):
return self._header_style

@header_style.setter
def header_style(self, val) -> None:
def header_style(self, val: HeaderStyleType) -> None:
self._validate_header_style(val)
self._header_style = val

Expand Down Expand Up @@ -1515,8 +1521,8 @@ def add_column(
self,
fieldname: str,
column: Sequence[Any],
align: str = "c",
valign: str = "t",
align: AlignType = "c",
valign: VAlignType = "t",
) -> None:
"""Add a column to the table.
Expand Down

0 comments on commit 7401618

Please sign in to comment.