Skip to content

ApplicationContext attributes have wrong type annotations #2635

@probablyjassin

Description

@probablyjassin

Summary

The ctx attributes channel, user and message, guild, and possible more, are typed as methods, not attributes

Reproduction Steps

Minimal Reproducible Code

import os

import discord
from discord.ext import commands

from logger import setup_logger
from config import DISCORD_TOKEN
from models.CustomMogiContext import MogiApplicationContext

logger = setup_logger(__name__)

intents = discord.Intents.all()


class customBot(commands.Bot):
    def __init__(self, *args, **kwargs):
        return super().__init__(*args, **kwargs)

    @slash_command(name="test_issue")
    async def test_issue(self, ctx: ApplicationContext):
        await ctx.respond(ctx.channel.id)
        # this works, but `channel` has the type annotation of a method:
        # (method) def channel(self: Self@ApplicationContext) -> (InteractionChannel | None)

        channel = ctx.channel()
        await ctx.send(channel)
        # this however obviously doesn't work:
        # discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: TypeError: 'TextChannel' object is not callable



bot = customBot(
    command_prefix=".",
    intents=intents,
)

bot.run(TOKEN)

Expected Results

Expected the type annotations to treat the ctx attributes channel, user and message as attributes.

And the syntax highlighting reflects them as such, causing following type annotations to break. (For example, theres no more autocomplete for ctx.channel.id, because the editor thinks channel is a method)

Actual Results

They get autocompleted as methods.

Intents

discord.Intents.all()

System Information

  • Python v3.12.7-final
  • py-cord v2.6.1-final
  • aiohttp v3.10.10
  • system info: Windows 11 10.0.22621

Checklist

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

full traceback:

Traceback (most recent call last):
File "C:\Users\Me\MyProject\venv\Lib\site-packages\discord\bot.py", line 1149, in invoke_application_command
await ctx.command.invoke(ctx)
File "C:\Users\Me\MyProject\venv\Lib\site-packages\discord\commands\core.py", line 435, in invoke
await injected(ctx)
File "C:\Users\Me\MyProject\venv\Lib\site-packages\discord\commands\core.py", line 138, in wrapped
ret = await coro(arg)
^^^^^^^^^^^^^^^
File "C:\Users\Me\MyProject\venv\Lib\site-packages\discord\commands\core.py", line 1486, in _invoke
await command.invoke(ctx)
File "C:\Users\Me\MyProject\venv\Lib\site-packages\discord\commands\core.py", line 435, in invoke
await injected(ctx)
File "C:\Users\Me\MyProject\venv\Lib\site-packages\discord\commands\core.py", line 146, in wrapped
raise ApplicationCommandInvokeError(exc) from exc
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: TypeError: 'TextChannel' object is not callable

I went to the definition of ApplicationContext and saw that the properties with this problem have the decorator @cached_property
Replacing this with @Property fixes the issue, but of course I'm new to the codebase so this might not be the best way to fix this.

@property # instead of @cached_property
    def channel(self) -> InteractionChannel | None:
        """Union[:class:`abc.GuildChannel`, :class:`PartialMessageable`, :class:`Thread`]:
        Returns the channel associated with this context's command. Shorthand for :attr:`.Interaction.channel`.
        """
        return self.interaction.channel

so maybe this is the fix, or the definition of @cached_property needs some fixing

Metadata

Metadata

Assignees

No one assigned

    Labels

    unconfirmed bugA bug report that needs triaging

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions