我在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
只是一个方便的类,可以帮助我返回带有.total
字段的列表。您可能只想添加以下行:
from telethon.tl.custom import Dialog
def filter_open_dialogs(dialogs, filter_list):
dialog: Dialog
... # rest of code in the method
这应该告诉 PyCharm 正确键入提示。我认为您无法指定自定义类的内部类型。