disnake icon indicating copy to clipboard operation
disnake copied to clipboard

Pycharm cannot determine type when using `@utils.cached_slot_property`

Open LEv145 opened this issue 6 months ago • 3 comments

Summary

Pycharm does not recognize type, which is why the method hints are not displayed

Reproduction Steps

Write code:

class TestCog(commands.Cog):
    @commands.slash_command()
    async def ping(self, inter: disnake.CommandInteraction) -> None:
        await inter.response.send_message("Pong!")

image image

Minimal Reproducible Code

No response

Expected Results

Type-based hints

Actual Results

Lack of hints

Intents

Intents.default()

System Information

- Python v3.10.11-final
- disnake v2.9.1-final
    - disnake importlib.metadata: v2.9.1
- aiohttp v3.9.1
- system info: Linux 5.15.119 #1-NixOS x86_64
- Pycharm 2022.2.3 (Professional Edition)

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

  1. Pycharm bug report: https://youtrack.jetbrains.com/issue/PY-63737/PyCharm-fails-to-infer-generic-descriptor-type-when-descriptor-is-used-as-a-decorator
  2. Pylance works correctly (VSCode) image
  3. This is not a problem of the library itself, but it makes it difficult to use the library with PyCharm
  4. It seems that in discord.py has the same problem: https://github.com/Rapptz/discord.py/issues/7549#issuecomment-1366519045 image

LEv145 avatar Jan 06 '24 22:01 LEv145

I doubt there's anything we can do about this, unfortunately. PyCharm has been struggling to infer descriptor types (especially generic ones) correctly for a long time, and ultimately it's up to them to fix it - disnake isn't the only library affected by this. There are several open issues that are more or less related:

  • https://youtrack.jetbrains.com/issue/PY-47698
  • https://youtrack.jetbrains.com/issue/PY-26184
  • https://youtrack.jetbrains.com/issue/PY-55531
  • https://youtrack.jetbrains.com/issue/PY-21069
  • https://youtrack.jetbrains.com/issue/PY-59896

Mypy and Pyright can handle these just fine, so those might be your best bet for now.

shiftinv avatar Jan 06 '24 23:01 shiftinv

Thanks!

One of the possible solutions in PyCharm:

    @commands.slash_command()
    async def ping(self, inter: disnake.CommandInteraction) -> None:
        assert isinstance(inter.response, disnake.InteractionResponse)

        await inter.response.send_message("Pong!")

Or

    @commands.slash_command()
    async def ping(self, inter: disnake.CommandInteraction) -> None:
        inter.response: disnake.InteractionResponse  # type: ignore

        await inter.response.send_message("Pong!")

LEv145 avatar Jan 06 '24 23:01 LEv145

This should probably be closed?

elenakrittik avatar May 15 '24 15:05 elenakrittik