理解Pycharm推断类型提示

时间:2016-06-09 13:14:36

标签: python python-2.7 pycharm type-hinting

我想了解快速文档窗口中PyCharm中带有推断类型的内容。

使用下面的功能,我在快速文档中看到以下内容: Quick Edit Window

论据host显示为我预期的Optional[str],但authversion仅显示str | None。技术上(我相信)两者都是正确的,但为什么这些类似论点之间的差异呢?

你会注意到我试图改变版本的类型提示只是为了看看是否有任何区别。

我是否遗漏了类型提示的工作原理? 我可以通过某种方式为Optional[str]auth显示version吗?

def getBaseUrl(service, host=None, auth=None, version=None):
    """
    Defaulted arguments will automatically fetch the values from config 

    :param str service: wms, wfs
    :param str or None host:    [internal | external | {user submitted host}] -
                                internal and external will use the config file to automatically determine
                                anything else will be assumed as a server address including possible port.
    :param str or None auth:    basic, digest, oauth 
                                anything else will raise exception
    :param version: eg wms_v1, wfs_v1
    :type version:  str or None
    :return: base url for use in the gis Webservice
    """
    assert service.lower() == 'wms' or service.lower() == 'wfs', "Unsupported service: %s" % service

    internalhost = cfg.get('GIS', 'internalhost')
    externalhost = cfg.get('GIS', 'externalhost')

    if host is None:
        host = cfg.get('GIS', 'host')
    if version is None:
        version = cfg.get(service, 'version')

    if host == "internal":
        host = "{host}".format(host=internalhost)

    elif host == 'external':
        host = "{host}".format(host=externalhost)

    else:
        host = "{host}".format(host=host)

    return buildBaseUrl(host=host, version=version, auth=auth, service=service)

1 个答案:

答案 0 :(得分:0)

看起来这确实是Pycharm的一个错误。将在2016.2版本中修复。

https://youtrack.jetbrains.com/issue/PY-17705