【问题标题】:Understanding Pycharm inferred type hinting理解 Pycharm 推断类型提示
【发布时间】:2016-10-10 04:06:39
【问题描述】:

我想了解 PyCharm 在快速文档窗口中的推断类型发生了什么。

使用下面的函数,我在快速文档中看到以下内容:

参数host 显示为Optional[str],正如我所期望的那样,但authversion 只显示str | None。从技术上讲(我相信)两者都是正确的,但为什么这些相似的论点之间存在差异?

您会注意到我尝试更改版本的类型提示只是为了看看是否有任何不同。

我是否遗漏了有关类型提示的工作原理的内容?
有什么方法可以让Optional[str] 显示为authversion 吗?

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)

【问题讨论】:

标签: python python-2.7 pycharm type-hinting


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2013-03-07
    • 1970-01-01
    相关资源
    最近更新 更多