Debian Codegraph @

test/test_inference/test_gradual/test_stubs.py

raw symbols SCIP
1 import os
2
3 import pytest
4
5 from jedi.api.project import Project
6 from test.helpers import root_dir
7
8
9 @pytest.mark.parametrize('type_', ['goto', 'infer'])
10 @pytest.mark.parametrize('way', ['direct', 'indirect'])
11 @pytest.mark.parametrize(
12 'kwargs', [
13 dict(only_stubs=False, prefer_stubs=False),
14 dict(only_stubs=False, prefer_stubs=True),
15 dict(only_stubs=True, prefer_stubs=False),
16 ]
17 )
18 @pytest.mark.parametrize(
19 ('code', 'full_name', 'has_stub', 'has_python', 'options'), [
20 ['import os; os.walk', 'os.walk', True, True, {}],
21 ['from collections import Counter', 'collections.Counter', True, True, {}],
22 ['from collections', 'collections', True, True, {}],
23 ['from collections import Counter; Counter', 'collections.Counter', True, True, {}],
24 ['from collections import Counter; Counter()', 'collections.Counter', True, True, {}],
25 ['from collections import Counter; Counter.most_common',
26 'collections.Counter.most_common', True, True, {}],
27 ['from collections import deque', 'collections.deque', True, False,
28 {'goto_has_python': True}],
29
30 ['from keyword import kwlist; kwlist', 'typing.Sequence', True, True,
31 {'goto_full_name': 'keyword.kwlist'}],
32 ['from keyword import kwlist', 'typing.Sequence', True, True,
33 {'goto_full_name': 'keyword.kwlist'}],
34
35 ['from socket import AF_INET', 'socket.AddressFamily', True, False,
36 {'goto_full_name': 'socket.AF_INET'}],
37 ['from socket import socket', 'socket.socket', True, True, {}],
38
39 ['import with_stub', 'with_stub', True, True, {}],
40 ['import with_stub', 'with_stub', True, True, {}],
41 ['import with_stub_folder.python_only', 'with_stub_folder.python_only', False, True, {}],
42 ['import stub_only', 'stub_only', True, False, {}],
43 ])
44 def test_infer_and_goto(Script, code, full_name, has_stub, has_python, way,
45 kwargs, type_, options, environment):
46 if type_ == 'infer' and full_name == 'typing.Sequence':
47 # Since Python 3.7+ there's not really a sequence definition, there's just
48 # a name that leads nowhere.
49 has_python = False
50
51 project = Project(os.path.join(root_dir, 'test', 'completion', 'stub_folder'))
52 s = Script(code, project=project)
53 prefer_stubs = kwargs['prefer_stubs']
54 only_stubs = kwargs['only_stubs']
55
56 if type_ == 'goto':
57 full_name = options.get('goto_full_name', full_name)
58 has_python = options.get('goto_has_python', has_python)
59
60 if way == 'direct':
61 if type_ == 'goto':
62 defs = s.goto(follow_imports=True, **kwargs)
63 else:
64 defs = s.infer(**kwargs)
65 else:
66 goto_defs = s.goto(
67 # Prefering stubs when we want to go to python and vice versa
68 prefer_stubs=not (prefer_stubs or only_stubs),
69 follow_imports=True,
70 )
71 if type_ == 'goto':
72 defs = [d for goto_def in goto_defs for d in goto_def.goto(**kwargs)]
73 else:
74 defs = [d for goto_def in goto_defs for d in goto_def.infer(**kwargs)]
75
76 if not has_stub and only_stubs:
77 assert not defs
78 else:
79 assert defs
80
81 for d in defs:
82 if prefer_stubs and has_stub:
83 assert d.is_stub()
84 elif only_stubs:
85 assert d.is_stub()
86 else:
87 assert has_python == (not d.is_stub())
88 assert d.full_name == full_name
89
90 assert d.is_stub() == (d.module_path.suffix == '.pyi')