| 1 | from functools import partial |
| 2 | from test.helpers import get_example_dir |
| 3 | from jedi.api.project import Project |
| 4 | |
| 5 | import pytest |
| 6 | |
| 7 | |
| 8 | @pytest.fixture |
| 9 | def ScriptInStubFolder(Script): |
| 10 | path = get_example_dir('stub_packages') |
| 11 | project = Project(path, sys_path=[path], smart_sys_path=False) |
| 12 | return partial(Script, project=project) |
| 13 | |
| 14 | |
| 15 | @pytest.mark.parametrize( |
| 16 | ('code', 'expected'), [ |
| 17 | ('from no_python import foo', ['int']), |
| 18 | ('from with_python import stub_only', ['str']), |
| 19 | ('from with_python import python_only', ['int']), |
| 20 | ('from with_python import both', ['int']), |
| 21 | ('from with_python import something_random', []), |
| 22 | ('from with_python.module import in_sub_module', ['int']), |
| 23 | ] |
| 24 | ) |
| 25 | def test_find_stubs_infer(ScriptInStubFolder, code, expected): |
| 26 | defs = ScriptInStubFolder(code).infer() |
| 27 | assert [d.name for d in defs] == expected |
| 28 | |
| 29 | |
| 30 | func_without_stub_doc = 'func_without_stub(a)\n\nnostubdoc' |
| 31 | func_with_stub_doc = 'func_with_stub(b: int) -> float\n\nwithstubdoc' |
| 32 | |
| 33 | |
| 34 | @pytest.mark.parametrize( |
| 35 | ('code', 'expected'), [ |
| 36 | ('from with_python import stub_only', ''), |
| 37 | ('from with_python import python_only', ''), |
| 38 | ('from with_python import both', ''), |
| 39 | |
| 40 | ('import with_python; with_python.func_without_stub', ''), |
| 41 | ('import with_python.module; with_python.module.func_without_stub', func_without_stub_doc), |
| 42 | ('from with_python import module; module.func_without_stub', func_without_stub_doc), |
| 43 | ('from with_python.module import func_without_stub', func_without_stub_doc), |
| 44 | ('from with_python.module import func_without_stub as f; f', func_without_stub_doc), |
| 45 | ('from with_python.module import func_without_stub; func_without_stub', |
| 46 | func_without_stub_doc), |
| 47 | ('from with_python import func_without_stub', ''), |
| 48 | ('from with_python import func_without_stub as f; f', ''), |
| 49 | ('from with_python import func_without_stub; func_without_stub', ''), |
| 50 | |
| 51 | ('import with_python; with_python.func_with_stub', func_with_stub_doc), |
| 52 | ('import with_python.module; with_python.module.func_with_stub', func_with_stub_doc), |
| 53 | ('from with_python import module; module.func_with_stub', func_with_stub_doc), |
| 54 | ('from with_python.module import func_with_stub', func_with_stub_doc), |
| 55 | ('from with_python.module import func_with_stub as f; f', func_with_stub_doc), |
| 56 | ('from with_python.module import func_with_stub; func_with_stub', func_with_stub_doc), |
| 57 | ('from with_python import func_with_stub', func_with_stub_doc), |
| 58 | ('from with_python import func_with_stub as f; f', func_with_stub_doc), |
| 59 | ('from with_python import func_with_stub; func_with_stub', func_with_stub_doc), |
| 60 | ] |
| 61 | ) |
| 62 | def test_docstrings(ScriptInStubFolder, code, expected): |
| 63 | d, = ScriptInStubFolder(code).help() |
| 64 | assert d.docstring() == expected |