python - 从函数返回“任何”时,在 Python 中声明或“转换”为实际类型
问题描述
我使用的函数any
作为类型返回,我无法在代码中获得建议。
如何使我的core_client
变量成为实际类型<CoreClient> <azure.devops.released.core.core_client.CoreClient>
?
我尝试查看任何规范,但在那里找不到它的指南。
有哪些选项可以让它发挥作用?
这是我正在调用的 SDK 函数:
def get_core_client(self):
"""get_core_client.
Gets the 5.1 version of the CoreClient
:rtype: :class:`<CoreClient> <azure.devops.released.core.core_client.CoreClient>`
"""
return self._connection.get_client('azure.devops.released.core.core_client.CoreClient')
我无法得到任何建议,似乎any
返回类型是原因:
core_client = connection.clients.get_core_client()
core_client.*** # no suggestions here
解决方案
core_client:CoreClient = connection.clients.get_core_client()
那么你应该得到提示......(取决于你的IDE)
通常 ide也理解 isinstance
如果您添加assert isinstance(core_client,CoreClient)
大多数IDE 的遗嘱(有些人会识别一个块if isinstance(core_client,CoreClient):
)并选择它,但通常在生产代码中使用断言不是一个好主意(但您可以......)
请记住粗略的指定类型并没有指定任何类型的可执行合同(core_client:CoreClient = ...
)实际上只是为了语法帮助(和自动文档的东西......)
推荐阅读
- python - 无法安装pyperclip python3 Mac?
- python - 如何在一个类中创建多个对象,并在一个循环中为每个对象分配变量?
- java - 对数组列表进行排序时遇到问题
- java - 为什么使用 for 循环的倒数之和比流快 400 倍?
- excel - VBA - 根据列值删除行
- reactjs - 在 React 中获取 post 对象和 slug 以正确更新
- assembly - Mips 腐烂加密
- java - 如何使用 Spring Boot 在 Keycloak 中设置 redirect_uri
- python - Django:在 /admin 处不存在
- amazon-ec2 - 使用 Boto3 按实例 ID 获取 EC2 实例每月的总 NetworkOut 字节数