Skip to content

Commit 0591ade

Browse files
authored
PR: Fix hover request for numpy alias (np) and ufuncs (#768)
* Fix hover request for numpy alias (np) and ufuncs * Change hover test string
1 parent 21833ea commit 0591ade

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,45 @@ def main():
1313
pass
1414
"""
1515

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

1756
def test_hover():
1857
# Over 'main' in def main():

0 commit comments

Comments
 (0)