python-3.x - Telethon 异步类型提示

标签 python-3.x pycharm type-hinting telethon

我正在Python中使用telethon库。我正在尝试使用类型提示来使 PyCharm 自动完成功能正常工作。在下面的代码片段中,函数filter_open_dialogs采用函数get_dialogs()的返回值作为输入。阅读 telethon 文档,我发现 get_dialogs() 的返回类型是 TotalList,因此将类型提示添加到 dialogs 输入参数中。然后我尝试调用函数filter_open_dialogs:

from telethon.tl.types import User
from telethon.helpers import TotalList
from telethon import TelegramClient, sync

class Crawler:

    def __init__(self, fetch: bool):

        self._client = TelegramClient('some_name', my_api_id, 'my_secret_api_hash')
        self._me = self._client.start(phone='my_phone_number', password='my_2fa_password')
        if fetch:
            self.get_open_dialogs()

    def get_open_dialogs(self):
        if self._me:
            Crawler.filter_open_dialogs(self._me.get_dialogs(), [])
            return self._me.get_dialogs()

    @staticmethod
    def filter_open_dialogs(dialogs: TotalList, filter_list: list):
        result = []
        if dialogs and dialogs.total:
            for dialog in dialogs:
                entity = dialog.entity
                if not isinstance(entity, User) and entity.id not in filter_list:
                    result.append(entity)
        return result

但是在 filter_open_dialogs(self._me.get_dialogs(), []) 行中,PyCharm 显示此警告:

预期类型为 TotalList',却得到了“Coroutine”...

有没有想过出了什么问题?

最佳答案

TotalList 只是一个方便的类,可帮助我返回带有 .total 字段的列表。您可能只想添加这一行:

from telethon.tl.custom import Dialog

def filter_open_dialogs(dialogs, filter_list):
    dialog: Dialog
    ...  # rest of code in the method

这应该告诉 PyCharm 正确输入提示。我认为您无法指定自定义类的内部类型。

关于python-3.x - Telethon 异步类型提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52121291/

相关文章:

php - 如何在 PHP 7 之前解决 "must be an instance of string, string given"?

python - 检查所有列表元素是否相同的代码返回 IndexError?

python - 从文本文件创建迷宫数组

Python 二进制字符串转 ASCII 文本

python - Pycharm - 更改导航 Pane 的主题?

python - 在 pyCharm 或任何 IDE 中选择配对大括号之间的内容

linux - 找不到pycharm的安装位置

python - 使用 types.FunctionType 与 typing.Callable 的类型提示注释?

python - 如何使用函数注解来验证函数调用类型

c++ - 在嵌入式 Python C++ 应用程序中导入 Numpy