From c2143fdf86dce0b14aab229639f848c342aabcee Mon Sep 17 00:00:00 2001 From: Michael H Date: Wed, 12 Feb 2020 09:28:52 -0500 Subject: [PATCH] [commands module] functools.partial support (#3542) * No reason we can't support this * meh * there we go... --- redbot/core/commands/commands.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/redbot/core/commands/commands.py b/redbot/core/commands/commands.py index 1a7794d13..dd1c7c1c9 100644 --- a/redbot/core/commands/commands.py +++ b/redbot/core/commands/commands.py @@ -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 - self.module = function.__module__ + 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