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
Please sign in to leave a comment.
Could you please show a code example where completion doesn't work as expected?
No problem
=============================================================
Note: Code completion works for other things just not lxml
The API shows that there are functiuons available http://lxml.de/api/index.html
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.
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?
@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:
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
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.
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
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.