Debian Codegraph @

test/test_inference/test_annotations.py

raw symbols SCIP
1 from textwrap import dedent
2
3 import pytest
4
5
6 def test_simple_annotations(Script, environment):
7 """
8 Annotations only exist in Python 3.
9 If annotations adhere to PEP-0484, we use them (they override inference),
10 else they are parsed but ignored
11 """
12 source = dedent("""\
13 def annot(a:3):
14 return a
15
16 annot('')""")
17
18 assert [d.name for d in Script(source).infer()] == ['str']
19
20 source = dedent("""\
21
22 def annot_ret(a:3) -> 3:
23 return a
24
25 annot_ret('')""")
26 assert [d.name for d in Script(source).infer()] == ['str']
27
28 source = dedent("""\
29 def annot(a:int):
30 return a
31
32 annot('')""")
33
34 assert [d.name for d in Script(source).infer()] == ['int']
35
36
37 @pytest.mark.parametrize('reference', [
38 'assert 1',
39 '1',
40 'def x(): pass',
41 '1, 2',
42 r'1\n'
43 ])
44 def test_illegal_forward_references(Script, environment, reference):
45 source = 'def foo(bar: "%s"): bar' % reference
46
47 assert not Script(source).infer()
48
49
50 def test_lambda_forward_references(Script, environment):
51 source = 'def foo(bar: "lambda: 3"): bar'
52
53 # For now just receiving the 3 is ok. I'm doubting that this is what we
54 # want. We also execute functions. Should we only execute classes?
55 assert Script(source).infer()