pythonpython-3.xanacondaintellisenseptvs

PTVS IntelliSense not working for Built-in Function


class Class:

def __init__(self, path):

    self._path = path
    string = open(self._path, 'r'). #HERE

When I try to type read() intelliSense says no completions.
However, I know open() function returns file object, which has read() function. I want to see all supported function after typing a dot.

PyCharm shows me recommanded function list, but PTVS does not support. I want to know this is casual things in PTVS or only happening to me.

My current Python Enviroment is Anaconda 4.3.0 (Python 3.5.3)

How can I fix it?


Solution

  • We've already fixed the specific case of open for our upcoming update (not the one that released today - the next one), but in short the problem is that you don't really know what open is going to return. In our fix, we guess one of two likely types, which should cover most use cases.

    To work around it right now, your best option is to assign the result of open to a variable and force it to a certain type using an assert statement. For example:

    f = open(self._path, 'r')
    import io
    assert isinstance(f, io.TextIOWrapper)
    
    f = open(self._path, 'rb')
    import io
    assert isinstance(f, io.BufferedIOBase)
    

    Note that your code will now fail if the variable is not the expected type, and that the code for Python 2 would be different from this, but until you can get the update where we embed this knowledge into our code it is the best you can do.