mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-12-07 09:52:30 -05:00
Add public positive_int and finite_float converters (#5969)
Co-authored-by: Kreusada <67752638+Kreusada@users.noreply.github.com>
This commit is contained in:
@@ -28,10 +28,12 @@ from .converter import (
|
||||
DictConverter as DictConverter,
|
||||
RelativedeltaConverter as RelativedeltaConverter,
|
||||
TimedeltaConverter as TimedeltaConverter,
|
||||
finite_float as finite_float,
|
||||
get_dict_converter as get_dict_converter,
|
||||
get_timedelta_converter as get_timedelta_converter,
|
||||
parse_relativedelta as parse_relativedelta,
|
||||
parse_timedelta as parse_timedelta,
|
||||
positive_int as positive_int,
|
||||
NoParseOptional as NoParseOptional,
|
||||
UserInputOptional as UserInputOptional,
|
||||
RawUserIdConverter as RawUserIdConverter,
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user