Pycord-Development / pycord

Pycord, a maintained fork of discord.py, is a python wrapper for the Discord API
https://docs.pycord.dev
MIT License
2.69k stars 459 forks source link

Confusing error for typed func passed to bridge_command #1603

Open ActuallyHappening opened 1 year ago

ActuallyHappening commented 1 year ago

Summary

Error raised when a typed function is used with the @bridge_command decorator should be fixed

Reproduction Steps

Recently, I have been using the bridge_command decorator to avoid code duplication and have struck upon an interesting error message being raised when I use from __future__ import annotations.

I have installed py-cord through pip install -U py-cord with python3 -c "import discord; print(f'{discord.__version__=}, {discord.__title__=}') returning "discord.version='2.1.1', discord.title='pycord'"

When I use functions with the future annotations, I seem to get a strange error on the line I defined the function:

Exception has occurred: AttributeError 'str' object has no attribute 'name'

Although this may not be a bug, not experienced enough with py-cord to know if such typing is an internal problem, the error itself gives no hints on its origin. In no way does it aid in identifying the type hints used as an issue, which is my main highlight.

I have attached the minimal reproducible code below

Minimal Reproducible Code

from __future__ import annotations
import discord
from discord.ext import bridge

bot = bridge.Bot(command_prefix="$", intents=discord.Intents.default())

@bot.bridge_command()
async def test(ctx, num: int):
    await ctx.send(f"__test__ {num=}")

Expected Results

The program runs and does nothing, as no call to bot.run was used

Actual Results

image

Stack trace using pretty_traceback:

image

Aliases for entries in sys.path:

: /Users/smartguy88-home/Library/Mobile Documents/com~apple~CloudDocs/Code Env/REPOS/DiscordBotTestbedH : /Users/smartguy88-home/.pyenv/versions/3.10.3/lib/python3.10/site-packages Traceback (most recent call last): /bug.py 10: async def test(ctx, num: int): /discord/ext/bridge/bot.py decorator 73: self.add_bridge_command(result) /discord/ext/bridge/bot.py add_bridge_command 58: command.add_to(self) # type: ignore /discord/ext/bridge/core.py add_to 178: bot.add_application_command(self.slash_variant) /discord/bot.py add_application_command 136: command.cog = None /discord/commands/core.py cog 777: self._validate_parameters() /discord/commands/core.py _validate_parameters 670: self.options: List[Option] = self._parse_options(params) /discord/commands/core.py _parse_options 709: option = Option(option) /discord/commands/options.py __init__ 199: self.input_type = SlashCommandOptionType.from_datatype(input_type) /discord/enums.py from_datatype 689: if datatype.__name__ in ["Member", "User"]: AttributeError: 'str' object has no attribute '__name__' ### Intents discord.Intents.default() ### System Information - Python v3.10.3-final - py-cord v2.1.1-final - aiohttp v3.7.4.post0 - system info: Darwin 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 20:57:23 PDT 2022; root:xnu-8020.121.3~2/RELEASE_ARM64_T8110 ### Checklist - [X] I have searched the open issues for duplicates. - [X] I have shown the entire traceback, if possible. - [X] I have removed my token from display, if visible. ### Additional Context _No response_
Lulalaby commented 1 year ago

https://guide.pycord.dev/extensions/bridge/

ActuallyHappening commented 1 year ago

I have read that page (which is the only reason I am using bridge commands) They do not use from __future__ import annotations however, which I would like to use but is not a requirement The issue I am reporting is the strange error name, which gives very little hint into the nature of the problem

If you got that error, typing would not come to mind even though that is the reason it appears (or so I have found through experimentation)

plun1331 commented 1 year ago

Try upgrading to 2.2.2 and see if the issue is resolved

justalemon commented 1 year ago

Hi, I just stumbled upon this problem with 2.3.2, so its still affecting 2.2.2 and newer.

C:\Users\lemon>python -m discord --version
C:\Python\Python311\Lib\site-packages\discord\__main__.py:47: DeprecationWarning: VersionInfo.releaselevel is deprecated since version 2.3, consider using release_level instead.
  "- py-cord v{0.major}.{0.minor}.{0.micro}-{0.releaselevel}".format(version_info)
C:\Python\Python311\Lib\site-packages\discord\__main__.py:49: DeprecationWarning: VersionInfo.releaselevel is deprecated since version 2.3, consider using release_level instead.
  if version_info.releaselevel != "final":
- Python v3.11.1-final
- py-cord v2.3.2-final
- aiohttp v3.8.3
- system info: Windows 10 10.0.22621

C:\Users\lemon>pip show py-cord
Name: py-cord
Version: 2.3.2
Summary: A Python wrapper for the Discord API
Home-page:
Author: Pycord Development
Author-email:
License: MIT
Location: C:\Python\Python311\Lib\site-packages
Requires: aiohttp
Required-by: lemonbot