Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[commands module] functools.partial support #3542

Merged
merged 3 commits into from Feb 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 13 additions & 9 deletions redbot/core/commands/commands.py
Expand Up @@ -7,21 +7,18 @@

import inspect
import re
import functools
import weakref
from typing import (
Awaitable,
Callable,
Coroutine,
TypeVar,
Type,
Dict,
List,
Optional,
Tuple,
Union,
MutableMapping,
TYPE_CHECKING,
cast,
)

import discord
Expand All @@ -38,7 +35,6 @@
Greedy,
)

from . import converter as converters
from .errors import ConversionFailure
from .requires import PermState, PrivilegeLevel, Requires, PermStateAllowedStates
from ..i18n import Translator
Expand Down Expand Up @@ -307,11 +303,19 @@ def callback(self):
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()
Expand All @@ -322,7 +326,7 @@ def callback(self, function):
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
Expand Down