File: test_parser_utils.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 (88 lines) | stat: -rw-r--r-- 2,755 bytes parent folder | download | duplicates (2)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import gc
from pathlib import Path

from jedi import parser_utils
from parso import parse
from parso.cache import parser_cache
from parso.python import tree

import pytest


class TestCallAndName:
    def get_call(self, source):
        # Get the simple_stmt and then the first one.
        node = parse(source).children[0]
        if node.type == 'simple_stmt':
            return node.children[0]
        return node

    def test_name_and_call_positions(self):
        name = self.get_call('name\nsomething_else')
        assert name.value == 'name'
        assert name.start_pos == (1, 0)
        assert name.end_pos == (1, 4)

        leaf = self.get_call('1.0\n')
        assert leaf.value == '1.0'
        assert parser_utils.safe_literal_eval(leaf.value) == 1.0
        assert leaf.start_pos == (1, 0)
        assert leaf.end_pos == (1, 3)

    def test_call_type(self):
        call = self.get_call('hello')
        assert isinstance(call, tree.Name)

    def test_literal_type(self):
        literal = self.get_call('1.0')
        assert isinstance(literal, tree.Literal)
        assert type(parser_utils.safe_literal_eval(literal.value)) == float

        literal = self.get_call('1')
        assert isinstance(literal, tree.Literal)
        assert type(parser_utils.safe_literal_eval(literal.value)) == int

        literal = self.get_call('"hello"')
        assert isinstance(literal, tree.Literal)
        assert parser_utils.safe_literal_eval(literal.value) == 'hello'


def test_hex_values_in_docstring():
    source = r'''
        def foo(object):
            """
             \xff
            """
            return 1
        '''

    doc = parser_utils.clean_scope_docstring(next(parse(source).iter_funcdefs()))
    assert doc == '\xff'


@pytest.mark.parametrize(
    'code,signature', [
        ('def my_function(x, typed: Type, z):\n return', 'my_function(x, typed: Type, z)'),
        ('def my_function(x, y, z) -> str:\n return', 'my_function(x, y, z) -> str'),
        ('lambda x, y, z: x + y * z\n', '<lambda>(x, y, z)')
    ])
def test_get_signature(code, signature):
    node = parse(code, version='3.8').children[0]
    if node.type == 'simple_stmt':
        node = node.children[0]
    assert parser_utils.get_signature(node) == signature


def test_parser_cache_clear(Script):
    """
    If parso clears its cache, Jedi should not keep those resources, they
    should be freed.
    """
    script = Script("a = abs\na", path=Path(__file__).parent / 'parser_cache_test_foo.py')
    script.complete()
    module_id = id(script._module_node)
    del parser_cache[script._inference_state.grammar._hashed][script.path]
    del script

    gc.collect()
    assert module_id not in [id(m) for m in gc.get_referrers(tree.Module)]