From 19f75b407e25bf423041b1f29dbe823d8e32a220 Mon Sep 17 00:00:00 2001 From: Alexander Kukushkin Date: Tue, 19 Nov 2024 09:09:09 +0100 Subject: [PATCH] Compatibility with prettytable>=3.12.0 (#3217) They started showing deprecation warning when importing ALL and FRAME constants. --- patroni/ctl.py | 11 +++++++++-- tests/test_ctl.py | 11 +++++++++-- typings/prettytable/__init__.pyi | 8 ++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/patroni/ctl.py b/patroni/ctl.py index d775c6ad..71c1a39d 100644 --- a/patroni/ctl.py +++ b/patroni/ctl.py @@ -38,7 +38,14 @@ import dateutil.tz import urllib3 import yaml -from prettytable import ALL, FRAME, PrettyTable +from prettytable import PrettyTable + +try: # pragma: no cover + from prettytable import HRuleStyle + hrule_all = HRuleStyle.ALL + hrule_frame = HRuleStyle.FRAME +except ImportError: # pragma: no cover + from prettytable import ALL as hrule_all, FRAME as hrule_frame if TYPE_CHECKING: # pragma: no cover from psycopg import Cursor @@ -437,7 +444,7 @@ def print_output(columns: Optional[List[str]], rows: List[List[Any]], alignment: else: # If any value is multi-line, then add horizontal between all table rows while printing to get a clear # visual separation of rows. - hrules = ALL if any(any(isinstance(c, str) and '\n' in c for c in r) for r in rows) else FRAME + hrules = hrule_all if any(any(isinstance(c, str) and '\n' in c for c in r) for r in rows) else hrule_frame table = PatronictlPrettyTable(header, columns, hrules=hrules) table.align = 'l' for k, v in (alignment or {}).items(): diff --git a/tests/test_ctl.py b/tests/test_ctl.py index 07359ca6..6435e775 100644 --- a/tests/test_ctl.py +++ b/tests/test_ctl.py @@ -9,7 +9,14 @@ import click import etcd from click.testing import CliRunner -from prettytable import ALL, PrettyTable +from prettytable import PrettyTable + +try: + from prettytable import HRuleStyle + hrule_all = HRuleStyle.ALL +except ImportError: + from prettytable import ALL as hrule_all + from urllib3 import PoolManager from patroni import global_config @@ -747,7 +754,7 @@ class TestCtl(unittest.TestCase): class TestPatronictlPrettyTable(unittest.TestCase): def setUp(self): - self.pt = PatronictlPrettyTable(' header', ['foo', 'bar'], hrules=ALL) + self.pt = PatronictlPrettyTable(' header', ['foo', 'bar'], hrules=hrule_all) def test__get_hline(self): expected = '+-----+-----+' diff --git a/typings/prettytable/__init__.pyi b/typings/prettytable/__init__.pyi index fd7d5398..f941c57d 100644 --- a/typings/prettytable/__init__.pyi +++ b/typings/prettytable/__init__.pyi @@ -1,6 +1,10 @@ +from enum import IntEnum from typing import Any, Dict, List -FRAME = 1 -ALL = 1 +class HRuleStyle(IntEnum): + FRAME = 0 + ALL = 1 +FRAME = HRuleStyle.FRAME +ALL = HRuleStyle.ALL class PrettyTable: def __init__(self, *args: str, **kwargs: Any) -> None: ... def _stringify_hrule(self, options: Dict[str, Any], where: str = '') -> str: ...