File: main_test.py

package info (click to toggle)
pyannotate 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 384 kB
  • sloc: python: 5,001; makefile: 5
file content (180 lines) | stat: -rw-r--r-- 5,503 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import contextlib
import os
import tempfile
import textwrap
import unittest

from typing import Iterator

from pyannotate_tools.annotations.infer import InferError
from pyannotate_tools.annotations.main import (generate_annotations_json,
                                               generate_annotations_json_string)


class TestMain(unittest.TestCase):
    def test_generation(self):
        # type: () -> None
        data = """
        [
            {
                "path": "pkg/thing.py",
                "line": 422,
                "func_name": "my_function",
                "type_comments": [
                    "(List[int], str) -> None"
                ],
                "samples": 3
            }
        ]
        """
        with self.temporary_file() as target_path:
            with self.temporary_json_file(data) as source_path:
                generate_annotations_json(source_path, target_path)
            with open(target_path) as target:
                actual = target.read()

        actual = actual.replace(' \n', '\n')
        expected = textwrap.dedent("""\
            [
                {
                    "func_name": "my_function",
                    "line": 422,
                    "path": "pkg/thing.py",
                    "samples": 3,
                    "signature": {
                        "arg_types": [
                            "List[int]",
                            "str"
                        ],
                        "return_type": "None"
                    }
                }
            ]""")
        assert actual == expected

    def test_ambiguous_kind(self):
        # type: () -> None
        data = """
        [
            {
                "path": "pkg/thing.py",
                "line": 422,
                "func_name": "my_function",
                "type_comments": [
                    "(List[int], str) -> None",
                    "(List[int], *str) -> None"
                ],
                "samples": 3
            }
        ]
        """
        with self.assertRaises(InferError) as e:
            with self.temporary_json_file(data) as source_path:
                generate_annotations_json(source_path, '/dummy')
        assert str(e.exception) == textwrap.dedent("""\
            Ambiguous argument kinds:
            (List[int], str) -> None
            (List[int], *str) -> None""")

    def test_generate_to_memory(self):
        # type: () -> None
        data = """
        [
            {
                "path": "pkg/thing.py",
                "line": 422,
                "func_name": "my_function",
                "type_comments": [
                    "(List[int], str) -> None"
                ],
                "samples": 3
            }
        ]
        """
        with self.temporary_json_file(data) as source_path:
            output_data = generate_annotations_json_string(source_path)
        assert output_data == [
            {
                "path": "pkg/thing.py",
                "line": 422,
                "func_name": "my_function",
                "signature": {
                    "arg_types": [
                        "List[int]",
                        "str"
                    ],
                    "return_type": "None"
                },
                "samples": 3
            }
        ]

        with self.temporary_json_file(data) as source_path:
            output_data = generate_annotations_json_string(source_path, only_simple=True)
        assert output_data == []

    def test_generate_simple_signatures(self):
        # type: () -> None
        data = """
        [
            {
                "path": "pkg/thing.py",
                "line": 422,
                "func_name": "complex_function",
                "type_comments": [
                    "(List[int], str) -> None"
                ],
                "samples": 3
            },
            {
                "path": "pkg/thing.py",
                "line": 9000,
                "func_name": "simple_function",
                "type_comments": [
                    "(int, str) -> None"
                ],
                "samples": 3
            }
        ]
        """
        with self.temporary_json_file(data) as source_path:
            output_data = generate_annotations_json_string(source_path, only_simple=True)
        assert output_data == [
            {
                "path": "pkg/thing.py",
                "line": 9000,
                "func_name": "simple_function",
                "signature": {
                    "arg_types": [
                        "int",
                        "str"
                    ],
                    "return_type": "None"
                },
                "samples": 3
            }
        ]

    @contextlib.contextmanager
    def temporary_json_file(self, data):
        # type: (str) -> Iterator[str]
        source = None
        try:
            with tempfile.NamedTemporaryFile(mode='w', delete=False) as source:
                source.write(data)
            yield source.name
        finally:
            if source is not None:
                os.remove(source.name)

    @contextlib.contextmanager
    def temporary_file(self):
        # type: () -> Iterator[str]
        target = None
        try:
            with tempfile.NamedTemporaryFile(mode='w', delete=False) as target:
                pass
            yield target.name
        finally:
            if target is not None:
                os.remove(target.name)