Skip to content

Commit

Permalink
query_result: reimplement strtobool
Browse files Browse the repository at this point in the history
Python 3.12 removes `distutils` module completely, as advised
in PEP 632. Migration notes [1] advises to reimplement `strtobool` as
there is no module that replaces it.

The implementation of the function is straighforward since the whole
logic is described in `distutils` API reference [2].

This commit reimplements this function to drop `distutils` dependency
completely.

[1]: https://peps.python.org/pep-0632/#migration-advice
[2]: https://docs.python.org/3.9/distutils/apiref.html#distutils.util.strtobool

Signed-off-by: Mikhail Koviazin <mikhail.koviazin@aiven.io>
  • Loading branch information
mkmkme committed Jul 11, 2024
1 parent f1898b9 commit 8debd8a
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions valkey/commands/graph/query_result.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sys
from collections import OrderedDict
from distutils.util import strtobool

# from prettytable import PrettyTable
from valkey import ResponseError
Expand Down Expand Up @@ -39,6 +38,22 @@
]


def strtobool(value: str) -> bool:
"""
Convert a string representation of truth to True or False.
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'value' is anything else.
"""
value = value.lower()
if value in ("y", "yes", "t", "true", "on", "1"):
return True
elif value in ("n", "no", "f", "false", "off", "0"):
return False
else:
raise ValueError(f"invalid truth value {value}")


class ResultSetColumnTypes:
COLUMN_UNKNOWN = 0
COLUMN_SCALAR = 1
Expand Down Expand Up @@ -270,7 +285,7 @@ def parse_boolean(self, value):
"""
value = value.decode() if isinstance(value, bytes) else value
try:
scalar = True if strtobool(value) else False
scalar = strtobool(value)
except ValueError:
sys.stderr.write("unknown boolean type\n")
scalar = None
Expand Down

0 comments on commit 8debd8a

Please sign in to comment.