Code completion in PyCharm using lxml

Hi, I'm currently testing out PyCharm as a replacement for pydev in eclipse

I'm trying to make use of the lxml library in pycharm, everything is working correctly except for that auto code completion doesn't fully work for some of the classes in lxml,

does anyone know of a way to solve this?

Cheers

8 comments
Comment actions Permalink

Could you please show a code example where completion doesn't work as expected?

0
Comment actions Permalink

No problem

=============================================================

import lxml.html
html
= open('html.html', 'r').read()

root
= lxml.html.fromstring(html)
temp
= root.cssselect("td.author") #Returns a list of HTMLElements which according to the docs have functions, I can call the funtions when I type them, but they don't show up in the editor in smart help

print temp[0].text # I know that the text attribute exists but code completion doesn't show it

temp[0]
====================================================================================

Note: Code completion works for other things just not lxml

The API shows that there are functiuons available http://lxml.de/api/index.html

1
Comment actions Permalink

Python is a dynamically typed language. You know that cssselect() returns a list of objects which have certain methods on them, but there is no way for PyCharm to discover this automatically, other than by us hard-coding return types for all the methods in all the libraries that exist in the world.

-1
Comment actions Permalink

Ok that obviously isn't feasable,

Could I hardcode them all myself though? and add them to PyCharm? Is there a way to do that?

1
Comment actions Permalink

@Dimitry I had no idea that `cssselect()` returns a list of objects which have certain methods on them. None whatsoever! But thank you for informing us that python is dynamically typed I'm gonna go change my whole life now. Be nice to your customers dawg.

Further, there is a way for PyCharm to discover this automatically, basing this on the fact that iPython has it figured out. 

That said, (& after 6 years) when I type:

    from lxml import etree

into PyCharm, my linter goes off and I don't get any completions. Not only that, but let me answer your customer's question for you: 

 

@Fennec, you'll need to look for or make a plugin probably. You could also potentially manually go into your own lxml library and poke at the top-level `__init__.py` until PyCharm sees all the things.

 

Everyone is welcome.

-RT

1
Comment actions Permalink

IPython can show completion variants because it executes your code. An IDE does not execute your code, it can only analyze it statically. You might want to tone down your snarkiness.

1
Comment actions Permalink

Fair enough, but conclusively saying that it's not possible to Fennec in the condescending tone you did was rude...thus the snarkiness. >:]

Seriously though, it's clear I don't know the internals of your platform (or IDEs in general) as you do. This said, lxml is the only commonly-used library I've ever seen PyCharm have this issue with. 

Am I right about the 2 potential solutions I raised?

-RT

0
Comment actions Permalink

The quality of code insight depends on how much dynamic stuff the library is doing, whether it's using some form of type annotations (using the Python 3 syntax, doc comments or something else), and so on. lxml is implemented in C code, so it's basically impossible for PyCharm to introspect its implementation, and as far as I remember it doesn't use any form of type annotations.

A plugin would indeed help, but it would basically require hard-coding all of the APIs in libxml and their return types as part of the plugin implementation.

0

Please sign in to leave a comment.