KurimuzonAkuma / pyrogram

Elegant, modern and asynchronous Telegram MTProto API framework in Python for users and bots
https://pyrogram.org
GNU Lesser General Public License v3.0
267 stars 72 forks source link

add proper typehints to all of the pyrogram.methods.decorators classes #34

Closed MarsBatya closed 4 months ago

MarsBatya commented 4 months ago

The issue:

mypy (as well as PyLance) is complaining when @Client.on_... is used w/ filters

Reproducing: create a test.py file

from pyrogram import filters, Client

@Client.on_message(filters.private)
async def test(client, message) -> None:
    pass

check it w/ mypy:

mypy test.py

the output is:

test.py:3: error: Argument 1 to "on_message" of "OnMessage" has incompatible type "Filter"; expected "OnMessage"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

it's caused by the lack of typehints in OnMessage and similar classes from pyrogram.methods.decorators:

class OnMessage:
    def on_message(
        self=None, # ⬅️ HERE
        filters=None, # ⬅️ HERE
        group: int = 0
    ) -> Callable:

the applied fix:

class OnMessage:
    def on_message(
        self: Union["OnMessage", Filter, None] = None,
        filters: Optional[Filter] = None,
        group: int = 0,
    ) -> Callable:

verification w/ mypy:

Success: no issues found in 1 source file