mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 11:18:54 -05:00
[commands module] functools.partial support (#3542)
* No reason we can't support this * meh * there we go...
This commit is contained in:
parent
da3f86d6ba
commit
c2143fdf86
@ -7,13 +7,11 @@ from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
import re
|
||||
import functools
|
||||
import weakref
|
||||
from typing import (
|
||||
Awaitable,
|
||||
Callable,
|
||||
Coroutine,
|
||||
TypeVar,
|
||||
Type,
|
||||
Dict,
|
||||
List,
|
||||
Optional,
|
||||
@ -21,7 +19,6 @@ from typing import (
|
||||
Union,
|
||||
MutableMapping,
|
||||
TYPE_CHECKING,
|
||||
cast,
|
||||
)
|
||||
|
||||
import discord
|
||||
@ -38,7 +35,6 @@ from discord.ext.commands import (
|
||||
Greedy,
|
||||
)
|
||||
|
||||
from . import converter as converters
|
||||
from .errors import ConversionFailure
|
||||
from .requires import PermState, PrivilegeLevel, Requires, PermStateAllowedStates
|
||||
from ..i18n import Translator
|
||||
@ -307,11 +303,19 @@ class Command(CogCommandMixin, DPYCommand):
|
||||
def callback(self, function):
|
||||
"""
|
||||
Below should be mostly the same as discord.py
|
||||
The only (current) change is to filter out typing.Optional
|
||||
if a user has specified the desire for this behavior
|
||||
|
||||
Currently, we modify behavior for
|
||||
|
||||
- functools.partial support
|
||||
- typing.Optional behavior change as an option
|
||||
"""
|
||||
self._callback = function
|
||||
if isinstance(function, functools.partial):
|
||||
self.module = function.func.__module__
|
||||
globals_ = function.func.__globals__
|
||||
else:
|
||||
self.module = function.__module__
|
||||
globals_ = function.__globals__
|
||||
|
||||
signature = inspect.signature(function)
|
||||
self.params = signature.parameters.copy()
|
||||
@ -322,7 +326,7 @@ class Command(CogCommandMixin, DPYCommand):
|
||||
for key, value in self.params.items():
|
||||
if isinstance(value.annotation, str):
|
||||
self.params[key] = value = value.replace(
|
||||
annotation=eval(value.annotation, function.__globals__)
|
||||
annotation=eval(value.annotation, globals_)
|
||||
)
|
||||
|
||||
# fail early for when someone passes an unparameterized Greedy type
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user