Telethon 异步类型提示
Telethon asyncio type hint
我在 python 中使用 telethon 库。我正在尝试使用类型提示来使 PyCharm 自动完成功能正常工作。在下面的代码片段中,函数 filter_open_dialogs
将函数 get_dialogs() 的 return 值作为输入。阅读 telethon 文档,我发现 get_dialogs()
的 return 类型是 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
只是方便 class 帮助我 return 一个带有 .total
字段的列表。您可能只想添加这一行:
from telethon.tl.custom import Dialog
def filter_open_dialogs(dialogs, filter_list):
dialog: Dialog
... # rest of code in the method
这应该告诉 PyCharm 正确键入提示。我不认为你可以指定自定义的内部类型 class.
我在 python 中使用 telethon 库。我正在尝试使用类型提示来使 PyCharm 自动完成功能正常工作。在下面的代码片段中,函数 filter_open_dialogs
将函数 get_dialogs() 的 return 值作为输入。阅读 telethon 文档,我发现 get_dialogs()
的 return 类型是 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
只是方便 class 帮助我 return 一个带有 .total
字段的列表。您可能只想添加这一行:
from telethon.tl.custom import Dialog
def filter_open_dialogs(dialogs, filter_list):
dialog: Dialog
... # rest of code in the method
这应该告诉 PyCharm 正确键入提示。我不认为你可以指定自定义的内部类型 class.