Skip to content

Commit 216b01f

Browse files
committed
Fix hover request for numpy alias (np) and ufuncs
1 parent 21833ea commit 216b01f

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

pyls/plugins/hover.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ def pyls_hover(document, position):
1616
# Find first exact matching definition
1717
definition = next((x for x in definitions if x.name == word), None)
1818

19+
# Ensure a definition is used if only one is available
20+
# even if the word doesn't match. An example of this case is 'np'
21+
# where 'numpy' doesn't match with 'np'. Same for NumPy ufuncs
22+
if len(definitions) == 1:
23+
definition = definitions[0]
24+
1925
if not definition:
2026
return {'contents': ''}
2127

test/plugins/test_hover.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,51 @@ def main():
1313
pass
1414
"""
1515

16+
NUMPY_DOC = """
17+
18+
import numpy as np
19+
np.sin
20+
21+
"""
22+
23+
def test_numpy_hover():
24+
# Over the blank line
25+
no_hov_position = {'line': 1, 'character': 0}
26+
# Over 'numpy' in import numpy as np
27+
numpy_hov_position_1 = {'line': 2, 'character': 8}
28+
# Over 'np' in import numpy as np
29+
numpy_hov_position_2 = {'line': 2, 'character': 17}
30+
# Over 'np' in np.sin
31+
numpy_hov_position_3 = {'line': 3, 'character': 1}
32+
# Over 'sin' in np.sin
33+
numpy_sin_hov_position = {'line': 3, 'character': 4}
34+
35+
doc = Document(DOC_URI, NUMPY_DOC)
36+
37+
if LooseVersion(_utils.JEDI_VERSION) >= LooseVersion('0.15.0'):
38+
contents = [
39+
{'language': 'python', 'value': 'numpy'},
40+
'NumPy\n=====\n\nProvides\n\xa0\xa01. '
41+
'An array object of arbitrary homogeneous items\n\xa0\xa02.']
42+
assert contents in pyls_hover(doc, numpy_hov_position_1)['contents']
43+
44+
contents = [
45+
{'language': 'python', 'value': 'numpy'},
46+
'NumPy\n=====\n\nProvides\n\xa0\xa01. '
47+
'An array object of arbitrary homogeneous items\n\xa0\xa02.']
48+
assert contents in pyls_hover(doc, numpy_hov_position_2)['contents']
49+
50+
contents = [
51+
{'language': 'python', 'value': 'numpy'},
52+
'NumPy\n=====\n\nProvides\n\xa0\xa01. '
53+
'An array object of arbitrary homogeneous items\n\xa0\xa02.']
54+
assert contents in pyls_hover(doc, numpy_hov_position_3)['contents']
55+
56+
contents = [
57+
{'language': 'python', 'value': 'numpy'},
58+
'Trigonometric sine, element-wise.\n\n']
59+
assert contents in pyls_hover(doc, numpy_sin_hov_position)['contents']
60+
1661

1762
def test_hover():
1863
# Over 'main' in def main():

0 commit comments

Comments
 (0)