【问题标题】:Python 3.4 Visual Studio PTVS Incorrect IntellisensePython 3.4 Visual Studio PTVS 不正确的智能感知
【发布时间】:2015-01-17 10:18:13
【问题描述】:

我已经在 Windows Server 2012 上安装了 Visual Studio 2013 Community 和 Python Tools for Visual Studio 2.1 with Python 3.4.2。智能感知似乎无法正常工作

import gspread
import requests
import json

# correctly calls gc a 'client' type
gc = gspread.login('<user_name redacted>','<password redacted>')
# correctly calls wks a 'Worksheet' type
wks = gc.open('testing_sheet').sheet1

# INCORRECTLY calls the json_test object a 'boolean, NoneType, float, int, object' type
json_test = json.loads('{"chicken":"cluck"}')

# correctly calls post_data a 'dict' type
post_data = {'item':'abc'}
# correctly calls post_headers a 'dict' type
post_headers = {'content-encoding': 'json'}
# INCORRECTLY calls post_requests a 'bool' type, should be type 'Response'
post_requests = requests.post(url = '<redacted>', data = json.dumps(post_data), headers = post_headers)

我曾多次尝试重建数据库,卸载并重新安装 Python 和 PTVS,但它总是错误地识别这些对象。难道我做错了什么?我还能做些什么吗?

【问题讨论】:

    标签: python-3.x visual-studio-2013 ptvs


    【解决方案1】:

    PTVS 中的 IntelliSense 由类型推理引擎驱动。由于 Python 本身最终是一种动态类型语言,它只能做这么多,并且必须对正在发生的事情做出假设和猜测。例如,在json.loads 的情况下,它会查看代码,通过它分析所有可能的代码路径,并生成可以从每个类型中产生的类型的联合。因此,如果json.loads 可以为某些输入返回布尔值(如果输入本身是布尔文字,它可以返回),那么该类型将列在返回类型中。然后,完成列表将包括联合中所有类型的成员,每个成员都标有它来自的类型的名称。

    (我相信object 出现在列表中的原因是因为object_hook 回调允许几乎任意的JSON 解码。)

    做的一件事实际上是尝试运行代码以查看您传递给它的字符串将被解析为什么。所以不要指望在 IntelliSense 中看到带有“chicken”等的特定字典。

    不过,requests 的补全看起来确实有问题。我建议filing a bug

    【讨论】:

    • 我知道json.loads 对于创建的类型是不确定的,因此解析器无法推断它。 requests 错误当然是一个错误。
    猜你喜欢
    • 1970-01-01
    • 2021-01-07
    • 2016-03-20
    • 2014-07-17
    • 2013-12-07
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多