File: test_annotations.py

package info (click to toggle)
python-jedi 0.19.1%2Bds1-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,680 kB
  • sloc: python: 28,783; makefile: 172; ansic: 13
file content (55 lines) | stat: -rw-r--r-- 1,278 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from textwrap import dedent

import pytest


def test_simple_annotations(Script, environment):
    """
    Annotations only exist in Python 3.
    If annotations adhere to PEP-0484, we use them (they override inference),
    else they are parsed but ignored
    """
    source = dedent("""\
    def annot(a:3):
        return a

    annot('')""")

    assert [d.name for d in Script(source).infer()] == ['str']

    source = dedent("""\

    def annot_ret(a:3) -> 3:
        return a

    annot_ret('')""")
    assert [d.name for d in Script(source).infer()] == ['str']

    source = dedent("""\
    def annot(a:int):
        return a

    annot('')""")

    assert [d.name for d in Script(source).infer()] == ['int']


@pytest.mark.parametrize('reference', [
    'assert 1',
    '1',
    'def x(): pass',
    '1, 2',
    r'1\n'
])
def test_illegal_forward_references(Script, environment, reference):
    source = 'def foo(bar: "%s"): bar' % reference

    assert not Script(source).infer()


def test_lambda_forward_references(Script, environment):
    source = 'def foo(bar: "lambda: 3"): bar'

    # For now just receiving the 3 is ok. I'm doubting that this is what we
    # want. We also execute functions. Should we only execute classes?
    assert Script(source).infer()