首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Telethon异步提示

Telethon异步提示
EN

Stack Overflow用户
提问于 2018-08-31 18:53:58
回答 1查看 659关注 0票数 2

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

代码语言:javascript
复制
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”代替.

有什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-19 11:21:04

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

代码语言:javascript
复制
from telethon.tl.custom import Dialog

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52121291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档