Python 3.4 Visual Studio PTVS Incorrect Intellisense

962 Views Asked by At

I have installed Visual Studio 2013 Community and Python Tools for Visual Studio 2.1 with Python 3.4.2 on Windows Server 2012. The intellisense does not appear to be working correctly

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)

I have tried rebuilding the DB several times, uninstalled and reinstalled Python and PTVS but it always incorrectly identifies these objects. Am I doing something wrong? Is there something more I can do?

1

There are 1 best solutions below

1
On BEST ANSWER

IntelliSense in PTVS is driven by type inference engine. Since Python itself is ultimately a dynamically typed language, it can only do so much, and has to make assumptions and guesses about what's going on. For example, in case of json.loads, it looks at the code, analyses all possible code paths through it, and produces a union of the types that can come out of each. So if json.loads can return a bool for some input (which it can, if input itself is a boolean literal), then that type is going to be listed in the return types. Completion list will then include members of all the types in the union, each member marked with the name of the type it came from.

(The reason why object shows up in the list is, I believe, because of the object_hook callback that allows pretty much arbitrary JSON decoding.)

One thing that it does not do is actually try to run the code to see what the string that you pass it to would get parsed as. So don't expect to see a specific dict with "chicken" etc in IntelliSense here.

The completion for requests does look wrong, though. I would suggest filing a bug.