Skip to content

utils.lock.lock breaks discord commands with forward ref annotations #1323

@Numerlor

Description

@Numerlor

discord.py resolves the forward refs by evaluating their value using the function's globals
https://github.com/Rapptz/discord.py/blob/a4b20d08c33814de80b0591cd1cbe0ec633df59a/discord/ext/commands/core.py#L304-L309, when the lock is applied it replaces the callback with a function that has new globals that only contain names from the utils.lock module causing the piece of code that resolves annotations to fail

from discord.ext import commands
from bot.utils.lock import lock


annotation = object()

@commands.command()
@lock("", "")
async def example_command(param: "annotation"):
    pass

causes

Traceback (most recent call last):
  File "C:\Users\Numerlor\.virtualenvs\bot-K_QdelnW\lib\site-packages\discord\ext\commands\bot.py", line 607, in _load_from_module_spec
    spec.loader.exec_module(lib)
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "D:\pycon\bot\bot\exts\test.py", line 8, in <module>
    async def example_command(param: "annotation"):
  File "C:\Users\Numerlor\.virtualenvs\bot-K_QdelnW\lib\site-packages\discord\ext\commands\core.py", line 1412, in decorator
    return cls(func, name=name, **attrs)
  File "D:\pycon\bot\bot\command.py", line 14, in __init__
    super().__init__(*args, **kwargs)
  File "C:\Users\Numerlor\.virtualenvs\bot-K_QdelnW\lib\site-packages\discord\ext\commands\core.py", line 223, in __init__
    self.callback = func
  File "C:\Users\Numerlor\.virtualenvs\bot-K_QdelnW\lib\site-packages\discord\ext\commands\core.py", line 309, in callback
    self.params[key] = value = value.replace(annotation=eval(value.annotation, function.__globals__))
  File "<string>", line 1, in <module>
NameError: name 'annotation' is not defined

I'm not familiar with typing enough to know if there is something to be done by us to fix this issue

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: backendRelated to internal functionality and utilities (error_handler, logging, security, utils and core)t: bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions