-
-
Notifications
You must be signed in to change notification settings - Fork 335
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
Make our exports more visible to static analysis #316
Conversation
@parity3 Can you check if this fixes your problem? |
Codecov Report
@@ Coverage Diff @@
## master #316 +/- ##
==========================================
- Coverage 99.24% 99.24% -0.01%
==========================================
Files 84 86 +2
Lines 10340 10323 -17
Branches 725 726 +1
==========================================
- Hits 10262 10245 -17
Misses 61 61
Partials 17 17
Continue to review full report at Codecov.
|
For testing, it should be possible to do: |
I checked out your commit. PyCharm still has an issue with resolving trio.run.
Notice the getattr calls (ie it's ugly). PyCharm won't add things to the trio root module that it thinks do not exist unfortunately. Hope this helps. |
Ugh. I really don't want to do that. Any idea why this seems to contradict what you observed here? |
It does seem to contradict the simple experiments I did above; the Maybe just auto-generate a .pyi file and check it into typeshed? I hear pycharm and others use that for stub generation for similar hard-to-introspect packages to trio. |
Typeshed is for declaring the types of variables. I don't actually know if you can use it to declare the attributes of a module? |
Oh well, I don't have any more knowledge here (regarding typeshed). I have yet another question that may provide insight.. how is numpy using |
@parity3 Those numpy imports are similar to the parts of trio that pycharm does understand (ref). I'm guessing that it's actually the |
Maybe numpy's less notable names in
All of those are frightening workarounds; why does this have to be so challenging!? |
Here is a SLIGHTLY better workaround for
... And I think we can remove all references/modifications to Also, note that hazmat.py, defining its |
@parity3 (please don't keep poking at this if you have other things you need to be doing, it is not urgent :-)) If you do that, then does pycharm actually show the correct suggestions for
If that's true then I guess a potential workaround would be to move the trickiness into |
You're right, PyCharm gives no error when resolving trio.checkpoint_if_cancelled when it should (runtime error happens). Ya I really need to stop now ;) |
This isn't really needed currently (we'd know, the symptom is a RecursionError at import time), but it's a genuine bug fix for a problem I ran into while implementing python-triogh-316. And since that PR seems to be stalled at the moment, I want to get this in so it doesn't get forgotten.
In particular, this should make it so PyCharm's completion starts working. Fixes python-triogh-314.
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)
I can confirm that trio.run and many more names are able to be resolved by PyCharm. However, I outsmarted PyCharm by replacing the code in # Some hazmat symbols are platform specific
import itertools
contained_in_core = set(filter(_core.__all__.__contains__, __all__))
globals().update(zip(contained_in_core, map(getattr, itertools.repeat(_core,len(contained_in_code)), contained_in_core)))
any(map(__all__.remove, set(__all__).difference(contained_in_core))) This achieves the same thing but PyCharm somehow does not parse through the functional programming tricks. It also worked with an eval() approach but that's even more clunky. |
@parity3: ugh, of course. Hmm. |
Actually, even simpler.. above the for-loop, just assign a variable like so: |
For anyone else who wants to get involved here, the test code I'm using to verify proper resolution in PyCharm is: import trio, sys
# make sure ".run" isn't a bad color
run = trio.run
# make sure ".checkpoint_if_cancelled" isn't a bad color
checkpoint_if_cancelled = trio.hazmat.checkpoint_if_cancelled
ON_POSIX = 'posix' in sys.builtin_module_names
if ON_POSIX: # haven't tested this but should work
assert 'wait_kevent' in trio.hazmat.__all__ and 'current_iocp' not in trio.hazmat.__all__
else: # yes, tested this
assert 'wait_kevent' not in trio.hazmat.__all__ and 'current_iocp' in trio.hazmat.__all__ |
ughhhh this solution is so terrible and so beautiful in different ways. I guess if that's what we have to do then that's what we have to do. I'm really curious if this fiddling helps with other IDEs. And wish we had a better way to test this. It's seems very likely that PyCharm will someday get cleverer about these, but will it be because it becomes clever enough not to need these tricks, or clever enough to see through our tricks and break everything again? It is a mystery 👻 I wish we could write a test for this, but I suppose that's asking too much. Oh well. |
Yes, confirmed the stupid hack is working now. As for the future, unknown as always. |
Okay, let's cross our fingers and see how it goes... |
In particular, this should make it so PyCharm's completion starts
working.
Fixes gh-314.