New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PyCharm autodetection of trio.* not working #314
Comments
Yeah, the question here is how PyCharm is actually getting its list of module attributes for autocompletion. It'd be nice to fix this, and I can't blame PyCharm for being confused by our current extremely complex export logic, but without knowing what they need it's hard to fix :-). We're obviously not going to actually literally define all of our exports directly in @parity3 to confirm -- do you have this problem for everything in |
A number of names ARE detected; just not all of them. Here is a screenshot from a brand new project: I hope that info can crack the case, but to me, from the outside, it seems rather arbitrary what PyCharm is picking up and what it's not. As far as trio.hazmat, it doesn't pickup any sub-names at all. |
Ok, so it looks like PyCharm understands: from ._version import __version__ and it understands from ._timeouts import *
__all__ += _timeouts.__all__ (I guess probably it's just picking up on the first line there and ignoring the What it doesn't understand is: for _symbol in _core.__all__:
_value = getattr(_core, _symbol)
if getattr(_value, "_hazmat", False):
setattr(hazmat, _symbol, _value)
hazmat.__all__.append(_symbol)
else:
globals()[_symbol] = _value
__all__.append(_symbol)
del _symbol, _value which... you know... fair enough, I guess. But this explains the whole pattern you see for And then It wouldn't be too bad to switch to keeping an explicit list of items that get imported into The big question is how we can express that list in a way that PyCharm understands, given that the things we're importing are not ultimately visible to it. If you make an empty module with What about a module with |
Defining The |
Oh good. Okay, one more check, if you can... what about this weirdo module: __all__ = []
_imports = ["asdf"]
__all__ += _imports |
asdf did NOT show up in autocomplete when using += or extend on |
Darn. Oh well, I think I can work around that. |
(And thank you very much for helping with my weird questions :-)) |
In particular, this should make it so PyCharm's completion starts working. Fixes python-triogh-314.
It may be that something can be done on the PyCharm side. I've opened issues against PyCharm before and they've been somewhat responsive: |
Before subjecting the support staff to arcane questions about the details of Python static analysis, let's see if @vlasovskikh has a moment to help us out... (The question is: the |
I'm not sure but I think explicitly listing them in PyCharm are always improving and fixing bugs in their inspections so it may be the case that it is actually intended to work. See the latest bugs fixed at https://confluence.jetbrains.com/display/PYH/PyCharm+172.3968.34+Release+Notes |
In particular, this should make it so PyCharm's completion starts working. Fixes python-triogh-314.
@vlasovskikh Thanks for checking! Do you have any idea then why @parity3 reports (confirmation and more description of behavior) that PyCharm does not detect the literal strings in this (Repro: |
PyCharm highlights the strings in Mentioning names like By the way, this is not just the limitation of PyCharm. Mypy also doesn't see
The easiest way to provide static information about your dynamic imports is to actually import all the stuff from from typing import TYPE_CHECKING
if TYPE_CHECKING:
from ._core import * or even: from typing import TYPE_CHECKING
if False:
from ._core import * although some analyzers would actually evaluate |
@vlasovskikh Ahhh ok, thanks, that definitely helps. (Though it would be nice if PyCharm were clever enough to realize that In this case |
Apparently PyCharm gives up on parsing __all__ entirely if it sees __all__ += foo.__all__, so move the static trio.__all__ entries that we want to be visible to static analyzers into their own dedicated submodule. See: python-trio#314 (comment)
It would be nice if PyCharm could resolve things like trio.run, and pretty much everything else.
In the below code snippet, PyCharm is unable to find run:
I don't know how to get around this without either a pre-compile step in the trio package release process (which messes up a ton of other things like line number mappings to the master files for example), or doing a bunch of stubs in trio/
__init__.py
and having to maintain them. PyCharm MAY have a way to get around this (they have done some magic with Django's ORM in detecting Model.objects.* methods but they have 'built-in' support considering Django is so popular) but I have no idea where to begin.The text was updated successfully, but these errors were encountered: