Search code examples
pythonautocompletepycharm

PyCharm autocomplete for imported modules


I'm new to Python and trying to get comfortable with the syntax and the language. I gave PyCharm a shot and found it very comfortable.

The only problem is that auto-completion isn't working as I expected and it is very important to me as part of the learning process and looking into some modules.

My code works even without the autocomplete but I'm very used to it and really wish to enjoy this feature.

I tried changing my project interpreter back and forth and nothing changed. I tried restarting PyCharm, the computer - didn't work. I tried Invalidate Cache, made sure the power save mode is off - nada.

Here is an example of missing autocomplete for lxml:

screenshot of editor

And here is the interpreter window:

screenshot of system settings


Solution

  • Python is a dynamically typed language, so the return type of a function is not always known in advance. PyCharm generally looks at the source of a function to guess what it returns. It can't in this case because etree.parse is written in Cython, not Python. If you ask PyCharm to go to the definition of the function it gives you a stub.

    The Python ecosystem has recently started to tackle this problem by providing various ways to annotate files with type hints for use by external tools, including PyCharm. One way is through .pyi files. A large collection of these can be found in the typeshed project. This issue shows that writing hints for lxml was proving difficult, and not wanting to have incomplete stubs in the typeshed repo, they were moved to their own repo here. The stubs are indeed very incomplete, and when I tried downloading and using them in PyCharm the results were pretty dismal. They correctly identify that etree.parse returns an etree._ElementTree, but the stub for _ElementTree only has two methods.

    I got much better results by annotating directly in the Python file, e.g.

    tree = etree.parse(path)  # type: etree._ElementTree
    

    (you can find out the type by checking type(tree))

    PyCharm itself somehow knows what the methods on _ElementTree are so now autocomplete works. Unfortunately it seems that using .pyi files makes PyCharm forget this knowledge.

    Here is documentation on type hinting in PyCharm.

    And yes, in general you will have to get used to less autocompletion and general type information and static analysis. Fortunately I think there is a lot to make up for it that isn't possible in other languages :)