@@ -191,6 +191,114 @@ class C:
191
191
assert c .fromlineno == 13
192
192
assert c .tolineno == 14
193
193
194
+ @staticmethod
195
+ def test_class_with_docstring () -> None :
196
+ """Test class nodes which only have docstrings."""
197
+ code = textwrap .dedent (
198
+ '''\
199
+ class A:
200
+ """My docstring"""
201
+ var = 1
202
+
203
+ class B:
204
+ """My docstring"""
205
+
206
+ class C:
207
+ """My docstring
208
+ is long."""
209
+
210
+ class D:
211
+ """My docstring
212
+ is long.
213
+ """
214
+
215
+ class E:
216
+ ...
217
+ '''
218
+ )
219
+
220
+ ast_module = builder .parse (code )
221
+
222
+ a = ast_module .body [0 ]
223
+ assert isinstance (a , nodes .ClassDef )
224
+ assert a .fromlineno == 1
225
+ assert a .tolineno == 3
226
+
227
+ b = ast_module .body [1 ]
228
+ assert isinstance (b , nodes .ClassDef )
229
+ assert b .fromlineno == 5
230
+ assert b .tolineno == 6
231
+
232
+ c = ast_module .body [2 ]
233
+ assert isinstance (c , nodes .ClassDef )
234
+ assert c .fromlineno == 8
235
+ assert c .tolineno == 10
236
+
237
+ d = ast_module .body [3 ]
238
+ assert isinstance (d , nodes .ClassDef )
239
+ assert d .fromlineno == 12
240
+ assert d .tolineno == 15
241
+
242
+ e = ast_module .body [4 ]
243
+ assert isinstance (d , nodes .ClassDef )
244
+ assert e .fromlineno == 17
245
+ assert e .tolineno == 18
246
+
247
+ @staticmethod
248
+ def test_function_with_docstring () -> None :
249
+ """Test function defintions with only docstrings."""
250
+ code = textwrap .dedent (
251
+ '''\
252
+ def a():
253
+ """My docstring"""
254
+ var = 1
255
+
256
+ def b():
257
+ """My docstring"""
258
+
259
+ def c():
260
+ """My docstring
261
+ is long."""
262
+
263
+ def d():
264
+ """My docstring
265
+ is long.
266
+ """
267
+
268
+ def e(a, b):
269
+ """My docstring
270
+ is long.
271
+ """
272
+ '''
273
+ )
274
+
275
+ ast_module = builder .parse (code )
276
+
277
+ a = ast_module .body [0 ]
278
+ assert isinstance (a , nodes .FunctionDef )
279
+ assert a .fromlineno == 1
280
+ assert a .tolineno == 3
281
+
282
+ b = ast_module .body [1 ]
283
+ assert isinstance (b , nodes .FunctionDef )
284
+ assert b .fromlineno == 5
285
+ assert b .tolineno == 6
286
+
287
+ c = ast_module .body [2 ]
288
+ assert isinstance (c , nodes .FunctionDef )
289
+ assert c .fromlineno == 8
290
+ assert c .tolineno == 10
291
+
292
+ d = ast_module .body [3 ]
293
+ assert isinstance (d , nodes .FunctionDef )
294
+ assert d .fromlineno == 12
295
+ assert d .tolineno == 15
296
+
297
+ e = ast_module .body [4 ]
298
+ assert isinstance (e , nodes .FunctionDef )
299
+ assert e .fromlineno == 17
300
+ assert e .tolineno == 20
301
+
194
302
def test_class_lineno (self ) -> None :
195
303
stmts = self .astroid .body
196
304
# on line 20:
0 commit comments