Python 3.4 Visual Studio PTVS错误的智能感知

时间:2014-11-19 18:48:48

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

我在Windows Server 2012上使用Python 3.4.2为Visual Studio 2.1安装了Visual Studio 2013社区和Python工具.intellisense似乎无法正常工作

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,但它总是错误地识别这些对象。难道我做错了什么?还有什么我可以做的吗?

1 个答案:

答案 0 :(得分:1)

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

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

做的一件事实际上是尝试运行代码以查看传递给它的字符串将被解析为。因此,不要期望看到与鸡肉有关的特定字典。这里有IntelliSense等。

requests的完成确实看起来不错。我建议filing a bug