DisnakeDev / disnake

An API wrapper for Discord written in Python.
https://docs.disnake.dev
MIT License
719 stars 137 forks source link

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

Closed LEv145 closed 5 months ago

LEv145 commented 9 months ago

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

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
shiftinv commented 9 months ago

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:

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

LEv145 commented 9 months ago

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!")
elenakrittik commented 5 months ago

This should probably be closed?