Debian Codegraph @

test/test_api/test_api_classes_follow_definition.py

raw symbols SCIP
1 from os.path import join
2 from itertools import chain
3 from functools import partial
4
5 import jedi
6 from ..helpers import test_dir
7
8
9 def test_import_empty(Script):
10 """ github #340, return the full word. """
11 completion = Script("import ").complete()[0]
12 definition = completion.infer()[0]
13 assert definition
14
15
16 def check_follow_definition_types(Script, source):
17 # nested import
18 completions = Script(source, path='some_path.py').complete()
19 defs = chain.from_iterable(c.infer() for c in completions)
20 return [d.type for d in defs]
21
22
23 def test_follow_import_incomplete(Script, environment):
24 """
25 Completion on incomplete imports should always take the full completion
26 to do any type inference.
27 """
28 datetime = check_follow_definition_types(Script, "import itertool")
29 assert datetime == ['module']
30
31 # empty `from * import` parts
32 itert = jedi.Script("from itertools import ").complete()
33 definitions = [d for d in itert if d.name == 'chain']
34 assert len(definitions) == 1
35 assert [d.type for d in definitions[0].infer()] == ['class']
36
37 # incomplete `from * import` part
38 datetime = check_follow_definition_types(Script, "from datetime import datetim")
39 assert set(datetime) == {'class', 'instance'} # py3: builtin and pure py version
40 # os.path check
41 ospath = check_follow_definition_types(Script, "from os.path import abspat")
42 assert set(ospath) == {'function'}
43
44 # alias
45 alias = check_follow_definition_types(Script, "import io as abcd; abcd")
46 assert alias == ['module']
47
48
49 def test_follow_definition_nested_import(Script):
50 Script = partial(Script, project=jedi.Project(join(test_dir, 'completion', 'import_tree')))
51 types = check_follow_definition_types(Script, "import pkg.mod1; pkg")
52 assert types == ['module']
53
54 types = check_follow_definition_types(Script, "import pkg.mod1; pkg.mod1")
55 assert types == ['module']
56
57 types = check_follow_definition_types(Script, "import pkg.mod1; pkg.mod1.a")
58 assert types == ['instance']
59
60
61 def test_follow_definition_land_on_import(Script):
62 types = check_follow_definition_types(Script, "import datetime; datetim")
63 assert types == ['module']