99
1010DOC_URI = uris .from_fs_path (__file__ )
1111DOC = """
12+ from random import randint
13+ from typing import overload
1214
13- def main():
14- \" \" \" hello world\" \" \"
15+ class A:
16+ \" \" \" Docstring for class A\" \" \"
17+
18+ b = 42
19+ \" \" \" Docstring for the class property A.b\" \" \"
20+
21+ def foo(self):
22+ \" \" \" Docstring for A.foo\" \" \"
23+ pass
24+
25+ if randint(0, 1) == 0:
26+ int_or_string_value = 10
27+ else:
28+ int_or_string_value = "10"
29+
30+ @overload
31+ def overload_function(s: int) -> int:
32+ ...
33+
34+ @overload
35+ def overload_function(s: str) -> str:
36+ ...
37+
38+ def overload_function(s):
39+ \" \" \" Docstring of overload function\" \" \"
1540 pass
41+
42+ int_value = 10
43+ string_value = "foo"
44+ instance_of_a = A()
45+ copy_of_class_a = A
46+ copy_of_property_b = A.b
47+ int_or_string_value
48+ overload_function
49+
1650"""
1751
1852NUMPY_DOC = """
@@ -23,6 +57,83 @@ def main():
2357"""
2458
2559
60+ def _hover_result_in_doc (workspace , position ):
61+ doc = Document (DOC_URI , workspace , DOC )
62+ return pylsp_hover (
63+ doc ._config , doc , {"line" : position [0 ], "character" : position [1 ]}
64+ )["contents" ]["value" ]
65+
66+
67+ def test_hover_over_nothing (workspace ):
68+ # Over blank line
69+ assert "" == _hover_result_in_doc (workspace , (3 , 0 ))
70+
71+
72+ def test_hover_on_keyword (workspace ):
73+ # Over "class" in "class A:"
74+ res = _hover_result_in_doc (workspace , (4 , 1 ))
75+ assert "Class definitions" in res
76+
77+
78+ def test_hover_on_variables (workspace ):
79+ # Over "int_value" in "int_value = 10"
80+ res = _hover_result_in_doc (workspace , (31 , 2 ))
81+ assert "int" in res # type
82+
83+ # Over "string_value" in "string_value = "foo""
84+ res = _hover_result_in_doc (workspace , (32 , 2 ))
85+ assert "string" in res # type
86+
87+
88+ def test_hover_on_class (workspace ):
89+ # Over "A" in "class A:"
90+ res = _hover_result_in_doc (workspace , (4 , 7 ))
91+ assert "A()" in res # signature
92+ assert "Docstring for class A" in res # docstring
93+
94+ # Over "A" in "instance_of_a = A()"
95+ res = _hover_result_in_doc (workspace , (33 , 17 ))
96+ assert "A()" in res # signature
97+ assert "Docstring for class A" in res # docstring
98+
99+ # Over "copy_of_class_a" in "copy_of_class_a = A" - needs infer
100+ res = _hover_result_in_doc (workspace , (34 , 4 ))
101+ assert "A()" in res # signature
102+ assert "Docstring for class A" in res # docstring
103+
104+
105+ def test_hover_on_property (workspace ):
106+ # Over "b" in "b = 42"
107+ res = _hover_result_in_doc (workspace , (7 , 5 ))
108+ assert "int" in res # type
109+ assert "Docstring for the class property A.b" in res # docstring
110+
111+ # Over "b" in "A.b"
112+ res = _hover_result_in_doc (workspace , (35 , 24 ))
113+ assert "int" in res # type
114+ assert "Docstring for the class property A.b" in res # docstring
115+
116+
117+ def test_hover_on_method (workspace ):
118+ # Over "foo" in "def foo(self):"
119+ res = _hover_result_in_doc (workspace , (10 , 10 ))
120+ assert "foo(self)" in res # signature
121+ assert "Docstring for A.foo" in res # docstring
122+
123+
124+ def test_hover_multiple_definitions (workspace ):
125+ # Over "int_or_string_value"
126+ res = _hover_result_in_doc (workspace , (36 , 5 ))
127+ assert "```python\n Union[int, str]\n ```" == res .strip () # only type
128+
129+ # Over "overload_function"
130+ res = _hover_result_in_doc (workspace , (37 , 5 ))
131+ assert (
132+ "overload_function(s: int) -> int\n overload_function(s: str) -> str" in res
133+ ) # signature
134+ assert "Docstring of overload function" in res # docstring
135+
136+
26137def test_numpy_hover (workspace ):
27138 # Over the blank line
28139 no_hov_position = {"line" : 1 , "character" : 0 }
@@ -38,7 +149,9 @@ def test_numpy_hover(workspace):
38149 doc = Document (DOC_URI , workspace , NUMPY_DOC )
39150
40151 contents = ""
41- assert contents in pylsp_hover (doc ._config , doc , no_hov_position )["contents" ]
152+ assert (
153+ contents in pylsp_hover (doc ._config , doc , no_hov_position )["contents" ]["value" ]
154+ )
42155
43156 contents = "NumPy\n =====\n \n Provides\n "
44157 assert (
@@ -71,21 +184,6 @@ def test_numpy_hover(workspace):
71184 )
72185
73186
74- def test_hover (workspace ):
75- # Over 'main' in def main():
76- hov_position = {"line" : 2 , "character" : 6 }
77- # Over the blank second line
78- no_hov_position = {"line" : 1 , "character" : 0 }
79-
80- doc = Document (DOC_URI , workspace , DOC )
81-
82- contents = {"kind" : "markdown" , "value" : "```python\n main()\n ```\n \n \n hello world" }
83-
84- assert {"contents" : contents } == pylsp_hover (doc ._config , doc , hov_position )
85-
86- assert {"contents" : "" } == pylsp_hover (doc ._config , doc , no_hov_position )
87-
88-
89187def test_document_path_hover (workspace_other_root_path , tmpdir ):
90188 # Create a dummy module out of the workspace's root_path and try to get
91189 # a definition on it in another file placed next to it.
0 commit comments