HypothesisWorks / hypothesis

Hypothesis is a powerful, flexible, and easy to use library for property-based testing.
https://hypothesis.works
Other
7.54k stars 586 forks source link

Jedi does not pick up definitions #2323

Closed MaxG87 closed 3 years ago

MaxG87 commented 4 years ago

I use Hypothesis with great enthusiasm. However, I have issues with its code completion.

A minimal environment could be created via

conda create --name testenv python=3.7 hypothesis jedi mypy

A test python file would be:

# test.py
from hypothesis import strategies as st

n = st.integers(min_=123)

Using mypy detects erroneous usage:

$ mypy test.py
test.py:3: error: Unexpected keyword argument "min_" for "integers"
Found 1 error in 1 file (checked 1 source file)

However, jedi-vim is not able to pick up the definitions. Thus, neither code completion nor displaying the doc string do work. On the other hand, PyCharm manages to to so.

On tweak I tried out was to adapt the python path.

export PYTHONPATH="export PYTHONPATH=$HOME/.miniconda3/envs/testenv/lib/python3.7/site-packages/:$PYTHONPATH"

This gives completion for st.intege, but still no docstring detection and still no argument completion.

Other packages, e.g. requests work flawlessly.

I would be very glad if I hypothesis would support the IDE-like setup that is common to many Vim users.

wfa207 commented 4 years ago

I'm coming across the same issue in VS Code running Jedi for IntelliSense. It seems the underlying issue is that Jedi has trouble resolving doc strings on decorated functions. The Jedi maintainer seems to have updated the code to interpret the built-in @wraps decorator correctly but this doesn't seem to extend to Hypothesis' function wrapper mechanisms.

It seems that most strategies are decorated by the @cacheable decorator, preventing Jedi from properly introspecting them. On the other hand, the undecorated just() and one_of() strategies present proper type annotations and auto-completion when used:

Screen Shot 2020-04-16 at 1 38 39 PM Screen Shot 2020-04-16 at 1 38 53 PM

The solution here could be to rely more heavily on @wraps to wrap decorated functions — alternatively, this may be an upstream issue in the Jedi repository.

Zac-HD commented 4 years ago

We would definitely like autocompletion to work - it's on the list, but we have more things to work on than volunteer time and switching to @functools.wraps would break lots of other things :cry:

wfa207 commented 4 years ago

@Zac-HD, that makes sense — this is a wonderful library by the way; keep up the great work!

For other users stumbling onto this issue and are using VS Code, I had some luck with the pyright extension. It seems to work but is a little ugly as it appends its results to Microsoft's default Python extension in the pop-out tooltip. Nonetheless, it's been pretty useful so far and I highly recommend it.

@MaxG87, if you're still having trouble with this, there is also coc-pyright, which appears to work on NeoVIM, but maybe there's a way to get it to work in vanilla VIM as well.

MaxG87 commented 4 years ago

@MaxG87, if you're still having trouble with this, there is also coc-pyright, which appears to work on NeoVIM, but maybe there's a way to get it to work in vanilla VIM as well.

Thank you very much. I'll give it a try.

MaxG87 commented 3 years ago

I don't know what happened, but today everything worked out. Maybe its due to improvements on Hypothesis, maybe the packages got better. Anyhow, docstrings can be shown and code completion works.

Thank you very much!