Add public positive_int and finite_float converters (#5969)

Co-authored-by: Kreusada <67752638+Kreusada@users.noreply.github.com>
This commit is contained in:
AAA3A
2023-04-17 23:33:44 +02:00
committed by GitHub
parent fa305cb060
commit eafbb06756
8 changed files with 143 additions and 76 deletions

View File

@@ -6,6 +6,7 @@ This module contains useful functions and classes for command argument conversio
Some of the converters within are included provisionally and are marked as such.
"""
import functools
import math
import re
from datetime import timedelta
from dateutil.relativedelta import relativedelta
@@ -37,10 +38,12 @@ __all__ = [
"NoParseOptional",
"RelativedeltaConverter",
"TimedeltaConverter",
"finite_float",
"get_dict_converter",
"get_timedelta_converter",
"parse_relativedelta",
"parse_timedelta",
"positive_int",
"CommandConverter",
"CogConverter",
]
@@ -233,6 +236,26 @@ class RawUserIdConverter(dpy_commands.Converter):
# which is *not* for type checking for the actual implementation
# and ensure the lies stay correct for how the object should look as a typehint
positive_int = dpy_commands.Range[int, 0, None]
if TYPE_CHECKING:
finite_float = float
else:
def finite_float(arg: str) -> float:
"""
This converts a user provided string into a finite float.
"""
try:
ret = float(arg)
except ValueError:
raise BadArgument(_("`{arg}` is not a number.").format(arg=arg))
if not math.isfinite(ret):
raise BadArgument(_("`{arg}` is not a finite number.").format(arg=ret))
return ret
if TYPE_CHECKING:
DictConverter = Dict[str, str]
else: