[commands module] functools.partial support (#3542)

* No reason we can't support this

* meh

* there we go...
This commit is contained in:
Michael H 2020-02-12 09:28:52 -05:00 committed by GitHub
parent da3f86d6ba
commit c2143fdf86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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