Add __or__ to all of the converters exposed in Red (#6748)

This commit is contained in:
Jakub Kuczys
2026-05-16 22:58:22 +02:00
committed by GitHub
parent 32ed5e0b6b
commit de11f52c41
+34 -11
View File
@@ -13,6 +13,7 @@ from datetime import timedelta
from dateutil.relativedelta import relativedelta from dateutil.relativedelta import relativedelta
from typing import ( from typing import (
TYPE_CHECKING, TYPE_CHECKING,
Any,
Optional, Optional,
Optional as NoParseOptional, Optional as NoParseOptional,
Tuple, Tuple,
@@ -20,6 +21,7 @@ from typing import (
Dict, Dict,
Type, Type,
TypeVar, TypeVar,
Union,
Union as UserInputOptional, Union as UserInputOptional,
) )
@@ -237,6 +239,9 @@ class RawUserIdConverter(dpy_commands.Converter):
there is no user with such ID. there is no user with such ID.
""" """
def __or__(self, rhs: Any) -> Any:
return Union[self, rhs]
async def convert(self, ctx: "Context", argument: str) -> int: async def convert(self, ctx: "Context", argument: str) -> int:
# This is for the hackban and unban commands, where we receive IDs that # This is for the hackban and unban commands, where we receive IDs that
# are most likely not in the guild. # are most likely not in the guild.
@@ -269,17 +274,20 @@ if TYPE_CHECKING:
finite_float = float finite_float = float
else: else:
def finite_float(arg: str) -> float: class finite_float(dpy_commands.Converter):
""" """Converts a user provided string into a finite float."""
This converts a user provided string into a finite float.
""" def __or__(self, rhs: Any) -> Any:
try: return Union[self, rhs]
ret = float(arg)
except ValueError: async def convert(self, ctx: "Context", arg: str) -> float:
raise BadArgument(_("`{arg}` is not a number.").format(arg=arg)) try:
if not math.isfinite(ret): ret = float(arg)
raise BadArgument(_("`{arg}` is not a finite number.").format(arg=ret)) except ValueError:
return ret 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: if TYPE_CHECKING:
@@ -296,6 +304,9 @@ else:
self.delims = delims or [" "] self.delims = delims or [" "]
self.pattern = re.compile(r"|".join(re.escape(d) for d in self.delims)) self.pattern = re.compile(r"|".join(re.escape(d) for d in self.delims))
def __or__(self, rhs: Any) -> Any:
return Union[self, rhs]
async def convert(self, ctx: "Context", argument: str) -> Dict[str, str]: async def convert(self, ctx: "Context", argument: str) -> Dict[str, str]:
ret: Dict[str, str] = {} ret: Dict[str, str] = {}
args = self.pattern.split(argument) args = self.pattern.split(argument)
@@ -379,6 +390,9 @@ else:
self.minimum = minimum self.minimum = minimum
self.maximum = maximum self.maximum = maximum
def __or__(self, rhs: Any) -> Any:
return Union[self, rhs]
async def convert(self, ctx: "Context", argument: str) -> timedelta: async def convert(self, ctx: "Context", argument: str) -> timedelta:
if self.default_unit and argument.isdecimal(): if self.default_unit and argument.isdecimal():
argument = argument + self.default_unit argument = argument + self.default_unit
@@ -487,6 +501,9 @@ else:
self.allowed_units = allowed_units self.allowed_units = allowed_units
self.default_unit = default_unit self.default_unit = default_unit
def __or__(self, rhs: Any) -> Any:
return Union[self, rhs]
async def convert(self, ctx: "Context", argument: str) -> relativedelta: async def convert(self, ctx: "Context", argument: str) -> relativedelta:
if self.default_unit and argument.isdecimal(): if self.default_unit and argument.isdecimal():
argument = argument + self.default_unit argument = argument + self.default_unit
@@ -537,6 +554,9 @@ else:
class CommandConverter(dpy_commands.Converter): class CommandConverter(dpy_commands.Converter):
"""Converts a command name to the matching `redbot.core.commands.Command` object.""" """Converts a command name to the matching `redbot.core.commands.Command` object."""
def __or__(self, rhs: Any) -> Any:
return Union[self, rhs]
async def convert(self, ctx: "Context", argument: str): async def convert(self, ctx: "Context", argument: str):
arg = argument.strip() arg = argument.strip()
command = ctx.bot.get_command(arg) command = ctx.bot.get_command(arg)
@@ -547,6 +567,9 @@ else:
class CogConverter(dpy_commands.Converter): class CogConverter(dpy_commands.Converter):
"""Converts a cog name to the matching `redbot.core.commands.Cog` object.""" """Converts a cog name to the matching `redbot.core.commands.Cog` object."""
def __or__(self, rhs: Any) -> Any:
return Union[self, rhs]
async def convert(self, ctx: "Context", argument: str): async def convert(self, ctx: "Context", argument: str):
arg = argument.strip() arg = argument.strip()
cog = ctx.bot.get_cog(arg) cog = ctx.bot.get_cog(arg)