3
3
"""
4
4
import os
5
5
import re
6
+ import typing as t
7
+ from collections .abc import Callable , Sequence
6
8
7
9
from click .testing import CliRunner
10
+ from click .testing import Result as ClickTestResult
8
11
9
- from code_annotations .base import BaseSearch , VerboseEcho
12
+ from code_annotations .base import BaseSearch
10
13
from code_annotations .cli import entry_point
14
+ from code_annotations .helpers import VerboseEcho
15
+
16
+ # Re-export Result for convenience
17
+ Result = ClickTestResult
11
18
12
19
EXIT_CODE_SUCCESS = 0
13
20
EXIT_CODE_FAILURE = 1
@@ -39,10 +46,10 @@ class FakeConfig:
39
46
Simple config for testing without reading a config file.
40
47
"""
41
48
42
- annotations : dict [str , str ] = {}
49
+ annotations : dict [str , t . Any ] = {}
43
50
annotation_regexes : list [str ] = []
44
51
annotation_tokens : list [str ] = []
45
- groups : list [str ] = []
52
+ groups : t . Union [ list [str ], dict [ str , list [ str ]] ] = []
46
53
echo = VerboseEcho ()
47
54
48
55
@@ -51,13 +58,17 @@ class FakeSearch(BaseSearch):
51
58
Simple test class for directly testing BaseSearch since it's abstract.
52
59
"""
53
60
54
- def search (self ):
61
+ def search (self ) -> dict [ str , list [ dict [ str , t . Any ]]] :
55
62
"""
56
63
Override for abstract base method.
64
+
65
+ Returns:
66
+ Empty dict to satisfy the abstract method requirement
57
67
"""
68
+ return {}
58
69
59
70
60
- def delete_report_files (file_extension ) :
71
+ def delete_report_files (file_extension : str ) -> None :
61
72
"""
62
73
Delete all files with the given extension from the test_reports directory.
63
74
@@ -76,7 +87,7 @@ def delete_report_files(file_extension):
76
87
pass
77
88
78
89
79
- def call_script (args_list , delete_test_reports = True , delete_test_docs = True ):
90
+ def call_script (args_list : Sequence [ str ] , delete_test_reports : bool = True , delete_test_docs : bool = True ) -> Result :
80
91
"""
81
92
Call the code_annotations script with the given params and a generic config file.
82
93
@@ -108,11 +119,11 @@ def call_script(args_list, delete_test_reports=True, delete_test_docs=True):
108
119
109
120
110
121
def call_script_isolated (
111
- args_list ,
112
- test_filesystem_cb = None ,
113
- test_filesystem_report_cb = None ,
114
- fake_safelist_data = "{}"
115
- ):
122
+ args_list : list [ str ] ,
123
+ test_filesystem_cb : Callable [[], None ] | None = None ,
124
+ test_filesystem_report_cb : Callable [[ str ], None ] | None = None ,
125
+ fake_safelist_data : str = "{}"
126
+ ) -> Result :
116
127
"""
117
128
Call the code_annotations script with the given params and a generic config file.
118
129
@@ -122,9 +133,6 @@ def call_script_isolated(
122
133
cleared. Use this if you need access to non-report files in the temp filesystem.
123
134
test_filesystem_report_cb: Callback function, called after the command is run, before the temp filesystem
124
135
is cleared. Callback is called with the raw text contents of the report file.
125
- fake_safelist_data: Raw text to write to the safelist file before the command is called.
126
- safelist_path: File path to write the safelist to. Used when writing a fake safelist, but not automatically
127
- passed to the command.
128
136
129
137
Returns:
130
138
click.testing.Result: Result from the `CliRunner.invoke()` call.
@@ -151,7 +159,9 @@ def call_script_isolated(
151
159
152
160
if test_filesystem_report_cb :
153
161
try :
154
- report_file = re .search (r'Generating report to (.*)' , result .output ).groups ()[0 ]
162
+ report_match = re .search (r'Generating report to (.*)' , result .output )
163
+ assert report_match is not None
164
+ report_file = report_match .groups ()[0 ]
155
165
with open (report_file ) as f :
156
166
report_contents = f .read ()
157
167
@@ -163,7 +173,7 @@ def call_script_isolated(
163
173
return result
164
174
165
175
166
- def get_report_filename_from_output (output ) :
176
+ def get_report_filename_from_output (output : str ) -> str | None :
167
177
"""
168
178
Find the report filename in a find_static or find_django output and return it.
169
179
@@ -172,9 +182,10 @@ def get_report_filename_from_output(output):
172
182
173
183
Returns:
174
184
Filename of the found report, or None of no name is found
175
-
176
185
"""
186
+ match = re .search (r'Generating report to (.*)' , output )
187
+ assert match is not None
177
188
try :
178
- return re . search ( r'Generating report to (.*)' , output ) .groups ()[0 ]
189
+ return match .groups ()[0 ]
179
190
except IndexError :
180
191
return None
0 commit comments