Skip to content

Commit

Permalink
Emit DeprecationWarnings for hrule and tableStyle constants (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Oct 29, 2024
2 parents 5ffbc2d + 90444d6 commit f3f3be7
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 74 deletions.
33 changes: 20 additions & 13 deletions src/prettytable/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
from __future__ import annotations

from typing import Any

from ._version import __version__
from .prettytable import (
ALL,
DEFAULT,
DOUBLE_BORDER,
FRAME,
HEADER,
MARKDOWN,
MSWORD_FRIENDLY,
NONE,
ORGMODE,
PLAIN_COLUMNS,
RANDOM,
SINGLE_BORDER,
from .prettytable import ( # noqa: F401
_DEPRECATED_ALL,
_DEPRECATED_DEFAULT,
_DEPRECATED_DOUBLE_BORDER,
_DEPRECATED_FRAME,
_DEPRECATED_HEADER,
_DEPRECATED_MARKDOWN,
_DEPRECATED_MSWORD_FRIENDLY,
_DEPRECATED_NONE,
_DEPRECATED_ORGMODE,
_DEPRECATED_PLAIN_COLUMNS,
_DEPRECATED_RANDOM,
_DEPRECATED_SINGLE_BORDER,
HRuleStyle,
PrettyTable,
RowType,
TableHandler,
TableStyle,
VRuleStyle,
_warn_deprecation,
from_csv,
from_db_cursor,
from_html,
Expand Down Expand Up @@ -53,3 +56,7 @@
"from_json",
"__version__",
]


def __getattr__(name: str) -> Any:
return _warn_deprecation(name, module_globals=globals())
44 changes: 32 additions & 12 deletions src/prettytable/prettytable.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import io
import re
import warnings
from collections.abc import Callable, Iterable, Mapping, Sequence
from enum import IntEnum
from html.parser import HTMLParser
Expand Down Expand Up @@ -72,18 +73,18 @@ class TableStyle(IntEnum):


# keep for backwards compatibility
FRAME: Final = 0
ALL: Final = 1
NONE: Final = 2
HEADER: Final = 3
DEFAULT: Final = TableStyle.DEFAULT
MSWORD_FRIENDLY: Final = TableStyle.MSWORD_FRIENDLY
PLAIN_COLUMNS: Final = TableStyle.PLAIN_COLUMNS
MARKDOWN: Final = TableStyle.MARKDOWN
ORGMODE: Final = TableStyle.ORGMODE
DOUBLE_BORDER: Final = TableStyle.DOUBLE_BORDER
SINGLE_BORDER: Final = TableStyle.SINGLE_BORDER
RANDOM: Final = TableStyle.RANDOM
_DEPRECATED_FRAME: Final = 0
_DEPRECATED_ALL: Final = 1
_DEPRECATED_NONE: Final = 2
_DEPRECATED_HEADER: Final = 3
_DEPRECATED_DEFAULT: Final = TableStyle.DEFAULT
_DEPRECATED_MSWORD_FRIENDLY: Final = TableStyle.MSWORD_FRIENDLY
_DEPRECATED_PLAIN_COLUMNS: Final = TableStyle.PLAIN_COLUMNS
_DEPRECATED_MARKDOWN: Final = TableStyle.MARKDOWN
_DEPRECATED_ORGMODE: Final = TableStyle.ORGMODE
_DEPRECATED_DOUBLE_BORDER: Final = TableStyle.DOUBLE_BORDER
_DEPRECATED_SINGLE_BORDER: Final = TableStyle.SINGLE_BORDER
_DEPRECATED_RANDOM: Final = TableStyle.RANDOM
# --------------------------------

BASE_ALIGN_VALUE: Final = "base_align_value"
Expand Down Expand Up @@ -2851,3 +2852,22 @@ def from_html_one(html_code: str, **kwargs) -> PrettyTable:
msg = "More than one <table> in provided HTML code. Use from_html instead."
raise ValueError(msg)
return tables[0]


def _warn_deprecation(name: str, module_globals: dict[str, Any]) -> Any:
if (val := module_globals.get(f"_DEPRECATED_{name}")) is None:
msg = f"module '{__name__}' has no attribute '{name}"
raise AttributeError(msg)
if name in {"FRAME", "ALL", "NONE", "HEADER"}:
msg = (
f"the '{name}' constant is deprecated, "
"use the 'HRuleStyle' and 'VRuleStyle' enums instead"
)
else:
msg = f"the '{name}' constant is deprecated, use the 'TableStyle' enum instead"
warnings.warn(msg, DeprecationWarning, stacklevel=3)
return val


def __getattr__(name: str) -> Any:
return _warn_deprecation(name, module_globals=globals())
Loading

0 comments on commit f3f3be7

Please sign in to comment.