首页 > 解决方案 > 从函数返回“任何”时,在 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

SDK 来自https://pypi.org/project/azure-devops/

在此处输入图像描述

标签: python

解决方案


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 = ...)实际上只是为了语法帮助(和自动文档的东西......)


推荐阅读