| 1 |
""" |
| 2 |
Testing of docstring related issues and especially ``jedi.docstrings``. |
| 3 |
""" |
| 4 |
|
| 5 |
import os |
| 6 |
from textwrap import dedent |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
import jedi |
| 11 |
from ..helpers import test_dir |
| 12 |
|
| 13 |
try: |
| 14 |
import numpydoc |
| 15 |
except ImportError: |
| 16 |
numpydoc_unavailable = True |
| 17 |
else: |
| 18 |
numpydoc_unavailable = False |
| 19 |
|
| 20 |
try: |
| 21 |
import numpy |
| 22 |
except ImportError: |
| 23 |
numpy_unavailable = True |
| 24 |
else: |
| 25 |
numpy_unavailable = False |
| 26 |
|
| 27 |
|
| 28 |
def test_function_doc(Script): |
| 29 |
defs = Script(""" |
| 30 |
def func(): |
| 31 |
'''Docstring of `func`.''' |
| 32 |
func""").infer() |
| 33 |
assert defs[0].docstring() == 'func()\n\nDocstring of `func`.' |
| 34 |
|
| 35 |
|
| 36 |
def test_class_doc(Script): |
| 37 |
defs = Script(""" |
| 38 |
class TestClass(): |
| 39 |
'''Docstring of `TestClass`.''' |
| 40 |
TestClass""").infer() |
| 41 |
|
| 42 |
expected = 'Docstring of `TestClass`.' |
| 43 |
assert defs[0].docstring(raw=True) == expected |
| 44 |
assert defs[0].docstring() == 'TestClass()\n\n' + expected |
| 45 |
|
| 46 |
|
| 47 |
def test_class_doc_with_init(Script): |
| 48 |
d, = Script(""" |
| 49 |
class TestClass(): |
| 50 |
'''Docstring''' |
| 51 |
def __init__(self, foo, bar=3): pass |
| 52 |
TestClass""").infer() |
| 53 |
|
| 54 |
assert d.docstring() == 'TestClass(foo, bar=3)\n\nDocstring' |
| 55 |
|
| 56 |
|
| 57 |
def test_instance_doc(Script): |
| 58 |
defs = Script(""" |
| 59 |
class TestClass(): |
| 60 |
'''Docstring of `TestClass`.''' |
| 61 |
tc = TestClass() |
| 62 |
tc""").infer() |
| 63 |
assert defs[0].docstring() == 'TestClass()\n\nDocstring of `TestClass`.' |
| 64 |
|
| 65 |
|
| 66 |
def test_multiple_docstrings(Script): |
| 67 |
d, = Script(""" |
| 68 |
def func(): |
| 69 |
'''Original docstring.''' |
| 70 |
x = func |
| 71 |
'''Docstring of `x`.''' |
| 72 |
x""").help() |
| 73 |
assert d.docstring() == 'Docstring of `x`.' |
| 74 |
|
| 75 |
|
| 76 |
def test_completion(Script): |
| 77 |
assert not Script(''' |
| 78 |
class DocstringCompletion(): |
| 79 |
#? [] |
| 80 |
""" asdfas """''').complete() |
| 81 |
|
| 82 |
|
| 83 |
def test_docstrings_type_dotted_import(Script): |
| 84 |
s = """ |
| 85 |
def func(arg): |
| 86 |
''' |
| 87 |
:type arg: random.Random |
| 88 |
''' |
| 89 |
arg.""" |
| 90 |
names = [c.name for c in Script(s).complete()] |
| 91 |
assert 'seed' in names |
| 92 |
|
| 93 |
|
| 94 |
def test_docstrings_param_type(Script): |
| 95 |
s = """ |
| 96 |
def func(arg): |
| 97 |
''' |
| 98 |
:param str arg: some description |
| 99 |
''' |
| 100 |
arg.""" |
| 101 |
names = [c.name for c in Script(s).complete()] |
| 102 |
assert 'join' in names |
| 103 |
|
| 104 |
|
| 105 |
def test_docstrings_type_str(Script): |
| 106 |
s = """ |
| 107 |
def func(arg): |
| 108 |
''' |
| 109 |
:type arg: str |
| 110 |
''' |
| 111 |
arg.""" |
| 112 |
|
| 113 |
names = [c.name for c in Script(s).complete()] |
| 114 |
assert 'join' in names |
| 115 |
|
| 116 |
|
| 117 |
def test_docstring_instance(Script): |
| 118 |
|
| 119 |
s = dedent(""" |
| 120 |
class A: |
| 121 |
def __init__(self,a): |
| 122 |
''' |
| 123 |
:type a: threading.Thread |
| 124 |
''' |
| 125 |
|
| 126 |
if a is not None: |
| 127 |
a.start() |
| 128 |
|
| 129 |
self.a = a |
| 130 |
|
| 131 |
|
| 132 |
def method_b(c): |
| 133 |
''' |
| 134 |
:type c: A |
| 135 |
''' |
| 136 |
|
| 137 |
c.""") |
| 138 |
|
| 139 |
names = [c.name for c in Script(s).complete()] |
| 140 |
assert 'a' in names |
| 141 |
assert '__init__' in names |
| 142 |
assert 'mro' not in names |
| 143 |
|
| 144 |
|
| 145 |
def test_docstring_keyword(Script): |
| 146 |
completions = Script('assert').complete() |
| 147 |
assert 'assert' in completions[0].docstring() |
| 148 |
|
| 149 |
|
| 150 |
def test_docstring_params_formatting(Script): |
| 151 |
defs = Script(""" |
| 152 |
def func(param1, |
| 153 |
param2, |
| 154 |
param3): |
| 155 |
pass |
| 156 |
func""").infer() |
| 157 |
assert defs[0].docstring() == 'func(param1, param2, param3)' |
| 158 |
|
| 159 |
|
| 160 |
def test_import_function_docstring(Script): |
| 161 |
code = "from stub_folder import with_stub; with_stub.stub_function" |
| 162 |
path = os.path.join(test_dir, 'completion', 'import_function_docstring.py') |
| 163 |
c, = Script(code, path=path).complete() |
| 164 |
|
| 165 |
doc = 'stub_function(x: int, y: float) -> str\n\nPython docstring' |
| 166 |
assert c.docstring() == doc |
| 167 |
assert c.type == 'function' |
| 168 |
func, = c.goto(prefer_stubs=True) |
| 169 |
assert func.docstring() == doc |
| 170 |
func, = c.goto() |
| 171 |
assert func.docstring() == doc |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
@pytest.mark.skipif(numpydoc_unavailable, |
| 177 |
reason='numpydoc module is unavailable') |
| 178 |
def test_numpydoc_parameters(): |
| 179 |
s = dedent(''' |
| 180 |
def foobar(x, y): |
| 181 |
""" |
| 182 |
Parameters |
| 183 |
---------- |
| 184 |
x : int |
| 185 |
y : str |
| 186 |
""" |
| 187 |
y.''') |
| 188 |
names = [c.name for c in jedi.Script(s).complete()] |
| 189 |
assert 'isupper' in names |
| 190 |
assert 'capitalize' in names |
| 191 |
|
| 192 |
|
| 193 |
@pytest.mark.skipif(numpydoc_unavailable, |
| 194 |
reason='numpydoc module is unavailable') |
| 195 |
def test_numpydoc_parameters_set_of_values(): |
| 196 |
s = dedent(''' |
| 197 |
def foobar(x, y): |
| 198 |
""" |
| 199 |
Parameters |
| 200 |
---------- |
| 201 |
x : {'foo', 'bar', 100500}, optional |
| 202 |
""" |
| 203 |
x.''') |
| 204 |
names = [c.name for c in jedi.Script(s).complete()] |
| 205 |
assert 'isupper' in names |
| 206 |
assert 'capitalize' in names |
| 207 |
assert 'numerator' in names |
| 208 |
|
| 209 |
|
| 210 |
@pytest.mark.skipif(numpydoc_unavailable, |
| 211 |
reason='numpydoc module is unavailable') |
| 212 |
def test_numpydoc_parameters_set_single_value(): |
| 213 |
""" |
| 214 |
This is found in numpy masked-array I'm not too sure what this means but should not crash |
| 215 |
""" |
| 216 |
s = dedent(''' |
| 217 |
def foobar(x, y): |
| 218 |
""" |
| 219 |
Parameters |
| 220 |
---------- |
| 221 |
x : {var}, optional |
| 222 |
""" |
| 223 |
x.''') |
| 224 |
names = [c.name for c in jedi.Script(s).complete()] |
| 225 |
|
| 226 |
assert names == [] |
| 227 |
|
| 228 |
|
| 229 |
@pytest.mark.skipif(numpydoc_unavailable, |
| 230 |
reason='numpydoc module is unavailable') |
| 231 |
def test_numpydoc_parameters_alternative_types(): |
| 232 |
s = dedent(''' |
| 233 |
def foobar(x, y): |
| 234 |
""" |
| 235 |
Parameters |
| 236 |
---------- |
| 237 |
x : int or str or list |
| 238 |
""" |
| 239 |
x.''') |
| 240 |
names = [c.name for c in jedi.Script(s).complete()] |
| 241 |
assert 'isupper' in names |
| 242 |
assert 'capitalize' in names |
| 243 |
assert 'numerator' in names |
| 244 |
assert 'append' in names |
| 245 |
|
| 246 |
|
| 247 |
@pytest.mark.skipif(numpydoc_unavailable, |
| 248 |
reason='numpydoc module is unavailable') |
| 249 |
def test_numpydoc_invalid(): |
| 250 |
s = dedent(''' |
| 251 |
def foobar(x, y): |
| 252 |
""" |
| 253 |
Parameters |
| 254 |
---------- |
| 255 |
x : int (str, py.path.local |
| 256 |
""" |
| 257 |
x.''') |
| 258 |
|
| 259 |
assert not jedi.Script(s).complete() |
| 260 |
|
| 261 |
|
| 262 |
@pytest.mark.skipif(numpydoc_unavailable, |
| 263 |
reason='numpydoc module is unavailable') |
| 264 |
def test_numpydoc_returns(): |
| 265 |
s = dedent(''' |
| 266 |
def foobar(): |
| 267 |
""" |
| 268 |
Returns |
| 269 |
---------- |
| 270 |
x : int |
| 271 |
y : str |
| 272 |
""" |
| 273 |
return x |
| 274 |
|
| 275 |
def bazbiz(): |
| 276 |
z = foobar() |
| 277 |
z.''') |
| 278 |
names = [c.name for c in jedi.Script(s).complete()] |
| 279 |
assert 'isupper' in names |
| 280 |
assert 'capitalize' in names |
| 281 |
assert 'numerator' in names |
| 282 |
|
| 283 |
|
| 284 |
@pytest.mark.skipif(numpydoc_unavailable, |
| 285 |
reason='numpydoc module is unavailable') |
| 286 |
def test_numpydoc_returns_set_of_values(): |
| 287 |
s = dedent(''' |
| 288 |
def foobar(): |
| 289 |
""" |
| 290 |
Returns |
| 291 |
---------- |
| 292 |
x : {'foo', 'bar', 100500} |
| 293 |
""" |
| 294 |
return x |
| 295 |
|
| 296 |
def bazbiz(): |
| 297 |
z = foobar() |
| 298 |
z.''') |
| 299 |
names = [c.name for c in jedi.Script(s).complete()] |
| 300 |
assert 'isupper' in names |
| 301 |
assert 'capitalize' in names |
| 302 |
assert 'numerator' in names |
| 303 |
|
| 304 |
|
| 305 |
@pytest.mark.skipif(numpydoc_unavailable, |
| 306 |
reason='numpydoc module is unavailable') |
| 307 |
def test_numpydoc_returns_alternative_types(): |
| 308 |
s = dedent(''' |
| 309 |
def foobar(): |
| 310 |
""" |
| 311 |
Returns |
| 312 |
---------- |
| 313 |
int or list of str |
| 314 |
""" |
| 315 |
return x |
| 316 |
|
| 317 |
def bazbiz(): |
| 318 |
z = foobar() |
| 319 |
z.''') |
| 320 |
names = [c.name for c in jedi.Script(s).complete()] |
| 321 |
assert 'isupper' not in names |
| 322 |
assert 'capitalize' not in names |
| 323 |
assert 'numerator' in names |
| 324 |
assert 'append' in names |
| 325 |
|
| 326 |
|
| 327 |
@pytest.mark.skipif(numpydoc_unavailable, |
| 328 |
reason='numpydoc module is unavailable') |
| 329 |
def test_numpydoc_returns_list_of(): |
| 330 |
s = dedent(''' |
| 331 |
def foobar(): |
| 332 |
""" |
| 333 |
Returns |
| 334 |
---------- |
| 335 |
list of str |
| 336 |
""" |
| 337 |
return x |
| 338 |
|
| 339 |
def bazbiz(): |
| 340 |
z = foobar() |
| 341 |
z.''') |
| 342 |
names = [c.name for c in jedi.Script(s).complete()] |
| 343 |
assert 'append' in names |
| 344 |
assert 'isupper' not in names |
| 345 |
assert 'capitalize' not in names |
| 346 |
|
| 347 |
|
| 348 |
@pytest.mark.skipif(numpydoc_unavailable, |
| 349 |
reason='numpydoc module is unavailable') |
| 350 |
def test_numpydoc_returns_obj(): |
| 351 |
s = dedent(''' |
| 352 |
def foobar(x, y): |
| 353 |
""" |
| 354 |
Returns |
| 355 |
---------- |
| 356 |
int or random.Random |
| 357 |
""" |
| 358 |
return x + y |
| 359 |
|
| 360 |
def bazbiz(): |
| 361 |
z = foobar(x, y) |
| 362 |
z.''') |
| 363 |
script = jedi.Script(s) |
| 364 |
names = [c.name for c in script.complete()] |
| 365 |
assert 'numerator' in names |
| 366 |
assert 'seed' in names |
| 367 |
|
| 368 |
|
| 369 |
@pytest.mark.skipif(numpydoc_unavailable, |
| 370 |
reason='numpydoc module is unavailable') |
| 371 |
def test_numpydoc_yields(): |
| 372 |
s = dedent(''' |
| 373 |
def foobar(): |
| 374 |
""" |
| 375 |
Yields |
| 376 |
---------- |
| 377 |
x : int |
| 378 |
y : str |
| 379 |
""" |
| 380 |
return x |
| 381 |
|
| 382 |
def bazbiz(): |
| 383 |
z = foobar(): |
| 384 |
z.''') |
| 385 |
names = [c.name for c in jedi.Script(s).complete()] |
| 386 |
assert 'isupper' in names |
| 387 |
assert 'capitalize' in names |
| 388 |
assert 'numerator' in names |
| 389 |
|
| 390 |
|
| 391 |
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable, |
| 392 |
reason='numpydoc or numpy module is unavailable') |
| 393 |
def test_numpy_returns(): |
| 394 |
s = dedent( |
| 395 |
''' |
| 396 |
import numpy |
| 397 |
x = numpy.asarray([]) |
| 398 |
x.d''' |
| 399 |
) |
| 400 |
names = [c.name for c in jedi.Script(s).complete()] |
| 401 |
assert 'diagonal' in names |
| 402 |
|
| 403 |
|
| 404 |
@pytest.mark.skipif(numpydoc_unavailable or numpy_unavailable, |
| 405 |
reason='numpydoc or numpy module is unavailable') |
| 406 |
def test_numpy_comp_returns(): |
| 407 |
s = dedent( |
| 408 |
''' |
| 409 |
import numpy |
| 410 |
x = numpy.array([]) |
| 411 |
x.d''' |
| 412 |
) |
| 413 |
names = [c.name for c in jedi.Script(s).complete()] |
| 414 |
assert 'diagonal' in names |
| 415 |
|
| 416 |
|
| 417 |
def test_decorator(Script): |
| 418 |
code = dedent(''' |
| 419 |
def decorator(name=None): |
| 420 |
def _decorate(func): |
| 421 |
@wraps(func) |
| 422 |
def wrapper(*args, **kwargs): |
| 423 |
"""wrapper docstring""" |
| 424 |
return func(*args, **kwargs) |
| 425 |
return wrapper |
| 426 |
return _decorate |
| 427 |
|
| 428 |
|
| 429 |
@decorator('testing') |
| 430 |
def check_user(f): |
| 431 |
"""Nice docstring""" |
| 432 |
pass |
| 433 |
|
| 434 |
check_user''') |
| 435 |
|
| 436 |
d, = Script(code).infer() |
| 437 |
assert d.docstring(raw=True) == 'Nice docstring' |
| 438 |
|
| 439 |
|
| 440 |
def test_method_decorator(Script): |
| 441 |
code = dedent(''' |
| 442 |
def decorator(func): |
| 443 |
@wraps(func) |
| 444 |
def wrapper(*args, **kwargs): |
| 445 |
"""wrapper docstring""" |
| 446 |
return func(*args, **kwargs) |
| 447 |
return wrapper |
| 448 |
|
| 449 |
class Foo(): |
| 450 |
@decorator |
| 451 |
def check_user(self, f): |
| 452 |
"""Nice docstring""" |
| 453 |
pass |
| 454 |
|
| 455 |
Foo().check_user''') |
| 456 |
|
| 457 |
d, = Script(code).infer() |
| 458 |
assert d.docstring() == 'wrapper(f)\n\nNice docstring' |
| 459 |
|
| 460 |
|
| 461 |
def test_partial(Script): |
| 462 |
code = dedent(''' |
| 463 |
def foo(): |
| 464 |
'x y z' |
| 465 |
from functools import partial |
| 466 |
x = partial(foo) |
| 467 |
x''') |
| 468 |
|
| 469 |
for p in Script(code).infer(): |
| 470 |
assert p.docstring(raw=True) == 'x y z' |
| 471 |
|
| 472 |
|
| 473 |
def test_basic_str_init_signature(Script, disable_typeshed): |
| 474 |
|
| 475 |
code = dedent(''' |
| 476 |
class Foo(str): |
| 477 |
pass |
| 478 |
Foo(''') |
| 479 |
c, = Script(code).get_signatures() |
| 480 |
assert c.name == 'Foo' |
| 481 |
|
| 482 |
|
| 483 |
def test_doctest_result_completion(Script): |
| 484 |
code = '''\ |
| 485 |
""" |
| 486 |
comment |
| 487 |
|
| 488 |
>>> something = 3 |
| 489 |
somethi |
| 490 |
""" |
| 491 |
something_else = 8 |
| 492 |
''' |
| 493 |
c1, c2 = Script(code).complete(line=5) |
| 494 |
assert c1.complete == 'ng' |
| 495 |
assert c2.complete == 'ng_else' |
| 496 |
|
| 497 |
|
| 498 |
def test_doctest_function_start(Script): |
| 499 |
code = dedent('''\ |
| 500 |
def test(a, b): |
| 501 |
""" |
| 502 |
From GH #1585 |
| 503 |
|
| 504 |
>>> a = {} |
| 505 |
>>> b = {} |
| 506 |
>>> get_remainder(a, b) == { |
| 507 |
... "foo": 10, "bar": 7 |
| 508 |
... } |
| 509 |
""" |
| 510 |
return |
| 511 |
''') |
| 512 |
assert Script(code).complete(7, 8) |
| 513 |
|
| 514 |
|
| 515 |
@pytest.mark.parametrize( |
| 516 |
"name, docstring", [ |
| 517 |
('prop1', 'Returns prop1.'), |
| 518 |
('prop2', 'Returns None or ...'), |
| 519 |
('prop3', 'Non-sense property.'), |
| 520 |
('prop4', 'Django like property'), |
| 521 |
] |
| 522 |
) |
| 523 |
def test_property(name, docstring, goto_or_complete): |
| 524 |
code = dedent(''' |
| 525 |
from typing import Optional |
| 526 |
class Test: |
| 527 |
@property |
| 528 |
def prop1(self) -> int: |
| 529 |
"""Returns prop1.""" |
| 530 |
|
| 531 |
@property |
| 532 |
def prop2(self) -> Optional[int]: |
| 533 |
"""Returns None or ...""" |
| 534 |
|
| 535 |
@property |
| 536 |
def prop3(self) -> None: |
| 537 |
"""Non-sense property.""" |
| 538 |
|
| 539 |
@cached_property # Not imported, but Jedi uses a heuristic |
| 540 |
def prop4(self) -> None: |
| 541 |
"""Django like property""" |
| 542 |
''') |
| 543 |
n, = goto_or_complete(code + 'Test().' + name) |
| 544 |
assert n.docstring() == docstring |