righttyper 0.0.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
righttyper/__init__.py ADDED
File without changes
righttyper/__main__.py ADDED
@@ -0,0 +1,4 @@
1
+ import righttyper.righttyper
2
+
3
+ if __name__ == "__main__":
4
+ righttyper.righttyper.main()
@@ -0,0 +1,102 @@
1
+ from typing import (
2
+ Dict,
3
+ List,
4
+ Optional,
5
+ Set,
6
+ Tuple,
7
+ Union,
8
+ )
9
+
10
+ import libcst as cst
11
+
12
+ from righttyper.righttyper_types import (
13
+ ArgumentName,
14
+ Filename,
15
+ FuncInfo,
16
+ FunctionName,
17
+ Typename,
18
+ )
19
+
20
+
21
+ class AnnotateFunctionTransformer(cst.CSTTransformer):
22
+ def __init__(
23
+ self,
24
+ filename: str,
25
+ type_annotations: Dict[
26
+ FuncInfo,
27
+ Tuple[
28
+ List[Tuple[ArgumentName, Typename]],
29
+ Typename,
30
+ ],
31
+ ],
32
+ not_annotated: Dict[FuncInfo, Set[str]],
33
+ ) -> None:
34
+ self.filename = filename
35
+ self.type_annotations = type_annotations
36
+ self.not_annotated = not_annotated
37
+ self.class_name: List[str] = []
38
+
39
+ def visit_ClassDef(self, node: cst.ClassDef) -> Optional[bool]:
40
+ self.class_name.append(node.name.value)
41
+ return None
42
+
43
+ def leave_ClassDef(
44
+ self,
45
+ original_node: cst.ClassDef,
46
+ updated_node: cst.ClassDef,
47
+ ) -> Union[cst.BaseStatement, cst.RemovalSentinel]:
48
+ self.class_name.pop()
49
+ return original_node
50
+
51
+ def leave_FunctionDef(
52
+ self,
53
+ original_node: cst.FunctionDef,
54
+ updated_node: cst.FunctionDef,
55
+ ) -> cst.FunctionDef:
56
+ # real_name = ".".join(self.class_name + [original_node.name.value])
57
+ name = original_node.name.value
58
+
59
+ # TODO: the current handling will lead to a bug if a function
60
+ # appears inside and outside a class definition in the same
61
+ # file. We need to handle this case.
62
+
63
+ key = FuncInfo(
64
+ Filename(self.filename),
65
+ FunctionName(name),
66
+ ) # original_node.name.value))
67
+ if key in self.type_annotations:
68
+ args, return_type = self.type_annotations[key]
69
+ # print(f"{args=}, {return_type=}")
70
+ # Update arguments with type annotations
71
+ new_parameters = []
72
+ for parameter in updated_node.params.params:
73
+ for arg, annotation_ in args:
74
+ if parameter.name.value == arg:
75
+ if arg not in self.not_annotated[key]:
76
+ continue
77
+ parsed_annotation = cst.parse_expression(annotation_)
78
+ new_parameters.append(
79
+ parameter.with_changes(
80
+ annotation=cst.Annotation(
81
+ annotation=parsed_annotation
82
+ )
83
+ )
84
+ )
85
+ break
86
+ else:
87
+ new_parameters.append(parameter)
88
+ parsed_return_type = cst.parse_expression(return_type)
89
+ if "return" in self.not_annotated[key]:
90
+ updated_node = updated_node.with_changes(
91
+ params=updated_node.params.with_changes(
92
+ params=new_parameters
93
+ ),
94
+ returns=cst.Annotation(annotation=parsed_return_type),
95
+ )
96
+ else:
97
+ updated_node = updated_node.with_changes(
98
+ params=updated_node.params.with_changes(
99
+ params=new_parameters
100
+ ),
101
+ )
102
+ return updated_node
@@ -0,0 +1,365 @@
1
+ import ast
2
+ import os
3
+ import warnings
4
+ from typing import Dict, List, Tuple, Union
5
+
6
+ import click
7
+
8
+ from righttyper.righttyper_utils import TOOL_NAME
9
+
10
+ # Suppress SyntaxWarning during AST parsing
11
+ warnings.filterwarnings("ignore", category=SyntaxWarning)
12
+
13
+ partially_annotated: List[Tuple[str, str, int]] = []
14
+ not_annotated: List[Tuple[str, str, int]] = []
15
+
16
+
17
+ class FullyQualifiedNameCollector(ast.NodeVisitor):
18
+ def __init__(self) -> None:
19
+ self.current_class: Union[str, None] = None
20
+ self.qualified_names: Dict[
21
+ ast.FunctionDef | ast.AsyncFunctionDef,
22
+ str,
23
+ ] = {}
24
+
25
+ def visit_ClassDef(self, node: ast.ClassDef) -> None:
26
+ previous_class = self.current_class
27
+ self.current_class = node.name
28
+ self.generic_visit(node)
29
+ self.current_class = previous_class
30
+
31
+ def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
32
+ self._visit_function(node)
33
+
34
+ def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
35
+ self._visit_function(node)
36
+
37
+ def _visit_function(
38
+ self,
39
+ node: Union[ast.FunctionDef, ast.AsyncFunctionDef],
40
+ ) -> None:
41
+ if self.current_class:
42
+ qualified_name = f"{self.current_class}.{node.name}"
43
+ else:
44
+ qualified_name = node.name
45
+ self.qualified_names[node] = qualified_name
46
+ self.generic_visit(node)
47
+
48
+
49
+ def generate_fully_qualified_names_dict(
50
+ tree: ast.AST,
51
+ ) -> Dict[ast.FunctionDef | ast.AsyncFunctionDef, str]:
52
+ collector = FullyQualifiedNameCollector()
53
+ collector.visit(tree)
54
+ return collector.qualified_names
55
+
56
+
57
+ def parse_python_file(
58
+ file_path: str,
59
+ ) -> List[int]:
60
+ fully_annotated_count = 0
61
+ partially_annotated_count = 0
62
+ not_annotated_count = 0
63
+
64
+ with open(file_path, "r") as file:
65
+ tree = ast.parse(file.read(), filename=file_path)
66
+
67
+ qualified_names = generate_fully_qualified_names_dict(tree)
68
+
69
+ # Function to check if a node has annotations
70
+ def has_annotations(node: ast.arg) -> bool:
71
+ return hasattr(node, "annotation") and node.annotation is not None
72
+
73
+ # Function to recursively search for functions in the AST
74
+ def search_functions(node: ast.AST) -> None:
75
+ nonlocal fully_annotated_count, partially_annotated_count, not_annotated_count
76
+
77
+ if isinstance(node, ast.FunctionDef) or isinstance(
78
+ node, ast.AsyncFunctionDef
79
+ ):
80
+ annotations_count = 0
81
+ total_to_annotate = 1 # Count the return here, then the args
82
+
83
+ if node.args.args:
84
+ for index, arg in enumerate(node.args.args):
85
+ # Modest unsoundness:
86
+ # We assume that if the first argument is self or cls,
87
+ # it is actually in a method / classmethod
88
+ if index == 0 and arg.arg in ["self", "cls"]:
89
+ continue
90
+ total_to_annotate += 1
91
+ annotations_count += has_annotations(arg)
92
+
93
+ # Check if there is a return type annotation
94
+ if node.returns:
95
+ annotations_count += 1
96
+
97
+ entry = (
98
+ file.name,
99
+ qualified_names[node],
100
+ node.lineno,
101
+ )
102
+ if annotations_count == total_to_annotate:
103
+ fully_annotated_count += 1
104
+ elif annotations_count > 0:
105
+ partially_annotated.append(entry)
106
+ partially_annotated_count += 1
107
+ else:
108
+ not_annotated.append(entry)
109
+ not_annotated_count += 1
110
+
111
+ for child_node in ast.iter_child_nodes(node):
112
+ search_functions(child_node)
113
+
114
+ search_functions(tree)
115
+
116
+ return [
117
+ fully_annotated_count,
118
+ partially_annotated_count,
119
+ not_annotated_count,
120
+ ]
121
+
122
+
123
+ def analyze_directory(
124
+ directory: str, cache: Dict[str, List[int]]
125
+ ) -> List[int]:
126
+ if directory in cache:
127
+ return cache[directory]
128
+
129
+ directory_summary: List[int] = [
130
+ 0,
131
+ 0,
132
+ 0,
133
+ ] # [fully annotated, partially annotated, not annotated]
134
+
135
+ for root, dirs, files in os.walk(directory):
136
+ if root != directory:
137
+ # Skip processing subdirectories separately in this loop
138
+ continue
139
+
140
+ for file_name in files:
141
+ if file_name.endswith(".py"):
142
+ file_path = os.path.join(root, file_name)
143
+ (
144
+ fully_annotated_count,
145
+ partially_annotated_count,
146
+ not_annotated_count,
147
+ ) = parse_python_file(file_path)
148
+ directory_summary[0] += fully_annotated_count
149
+ directory_summary[1] += partially_annotated_count
150
+ directory_summary[2] += not_annotated_count
151
+ # Update file-level summary in cache
152
+ cache[file_path] = [
153
+ fully_annotated_count,
154
+ partially_annotated_count,
155
+ not_annotated_count,
156
+ ]
157
+
158
+ for dir_name in dirs:
159
+ dir_path = os.path.join(root, dir_name)
160
+ subdir_summary = analyze_directory(dir_path, cache)
161
+ directory_summary[0] += subdir_summary[0] # fully annotated
162
+ directory_summary[1] += subdir_summary[1] # partially annotated
163
+ directory_summary[2] += subdir_summary[2] # not annotated
164
+
165
+ cache[directory] = directory_summary
166
+ return directory_summary
167
+
168
+
169
+ def analyze_all_directories(
170
+ directory: str,
171
+ ) -> Dict[str, List[int]]:
172
+ cache: Dict[str, List[int]] = {}
173
+ analyze_directory(directory, cache)
174
+ return cache
175
+
176
+
177
+ def print_directory_summary(summary: Dict[str, List[int]]) -> None:
178
+ from rich.console import Console
179
+ from rich.table import Table
180
+
181
+ headers = [
182
+ "Directory",
183
+ "Fully\nannotated",
184
+ "Partially\nannotated",
185
+ "Unannotated",
186
+ "% Fully\n annotated",
187
+ ]
188
+ data = []
189
+ for (
190
+ directory,
191
+ counts,
192
+ ) in (
193
+ summary.items()
194
+ ): # sorted(summary.items(), key=lambda x: x[1][2]): # Sort by number of not annotated functions
195
+ if os.path.isfile(directory): # Check if the path is a file
196
+ continue
197
+ total_functions = sum(counts)
198
+ if total_functions > 0:
199
+ percentage_annotated = (counts[0] / total_functions) * 100
200
+ else:
201
+ continue
202
+ data.append(
203
+ [
204
+ directory,
205
+ f"{counts[0]}",
206
+ f"{counts[1]}",
207
+ f"{counts[2]}",
208
+ f"{percentage_annotated:6.2f}%",
209
+ ]
210
+ )
211
+
212
+ summary_row = data[-1]
213
+ print(
214
+ f"{summary_row[3]} unannotated, {summary_row[2]} partially annotated, {summary_row[1]} fully annotated ({summary_row[4]})"
215
+ )
216
+
217
+ table = Table(
218
+ show_header=True,
219
+ header_style="bold magenta",
220
+ )
221
+ for header in headers:
222
+ table.add_column(header)
223
+
224
+ for row in data:
225
+ table.add_row(*row)
226
+
227
+ console = Console(width=255, record=True)
228
+ with console.capture() as _:
229
+ console.print(table)
230
+ html_output = console.export_html()
231
+
232
+ # Save the HTML output to a file
233
+ with open(f"{TOOL_NAME}-coverage.html", "w") as file:
234
+ file.write(html_output)
235
+ print(f"Report saved in {TOOL_NAME}-coverage.html")
236
+
237
+ # console.print(table)
238
+
239
+
240
+ def print_file_summary(summary: Dict[str, List[int]]) -> None:
241
+ from rich.console import Console
242
+ from rich.table import Table
243
+
244
+ headers = [
245
+ "File",
246
+ "Fully\nannotated",
247
+ "Partially\nannotated",
248
+ "Unannotated",
249
+ "% Fully\nannotated",
250
+ ]
251
+ data = []
252
+ # print(summary.items())
253
+ for file_path, counts in sorted(
254
+ summary.items()
255
+ ): # , key=lambda x: sum(x[1:])): # x[1][2]): # Sort by number of not annotated functions
256
+ if os.path.isfile(file_path): # Check if the path is a file
257
+ total_functions = sum(counts)
258
+ total_annotated_functions = total_functions - counts[2] - counts[1]
259
+ if total_functions > 0:
260
+ percentage_annotated = (
261
+ total_annotated_functions / total_functions
262
+ ) * 100
263
+ else:
264
+ continue
265
+ data.append(
266
+ [
267
+ file_path,
268
+ f"{counts[0]}",
269
+ f"{counts[1]}",
270
+ f"{counts[2]}",
271
+ f"{percentage_annotated:6.2f}%",
272
+ ]
273
+ )
274
+
275
+ table = Table(
276
+ show_header=True,
277
+ header_style="bold magenta",
278
+ )
279
+ for header in headers:
280
+ table.add_column(header)
281
+
282
+ for row in data:
283
+ table.add_row(*row)
284
+
285
+ table.add_section()
286
+ c = [0, 0, 0]
287
+ for file_path, counts in summary.items():
288
+ if not os.path.isfile(file_path): # Check if the path is a file
289
+ continue
290
+ # print(f"DIRECTORY {file_path} COUNTS {counts}")
291
+ for i, value in enumerate(counts):
292
+ c[i] += value
293
+ total_functions = sum(c)
294
+ if total_functions > 0:
295
+ percentage_annotated = (c[0] / total_functions) * 100
296
+ else:
297
+ percentage_annotated = 0
298
+ summary_row = [
299
+ "Total:",
300
+ f"{c[0]}",
301
+ f"{c[1]}",
302
+ f"{c[2]}",
303
+ f"{percentage_annotated:6.2f}%",
304
+ ]
305
+ table.add_row(*summary_row)
306
+
307
+ print(
308
+ f"{summary_row[3]} unannotated, {summary_row[2]} partially annotated, {summary_row[1]} fully annotated ({summary_row[4]})"
309
+ )
310
+
311
+ console = Console(width=255, record=True)
312
+ with console.capture() as _:
313
+ console.print(table)
314
+ html_output = console.export_html()
315
+
316
+ # Save the HTML output to a file
317
+ with open(f"{TOOL_NAME}-coverage.html", "w") as file:
318
+ file.write(html_output)
319
+ print(f"Report saved in {TOOL_NAME}-coverage.html")
320
+
321
+
322
+ def print_annotation_summary() -> None:
323
+ items = [partially_annotated, not_annotated]
324
+ item_titles = [
325
+ "Partially annotated",
326
+ "Not annotated",
327
+ ]
328
+ for i in range(len(items)):
329
+ if items[i]:
330
+ items[i] = sorted(
331
+ items[i],
332
+ key=lambda x: (x[0], x[2]),
333
+ )
334
+ print(f"{item_titles[i]}:")
335
+ print("-" * (len(item_titles[i]) + 1))
336
+ while items[i]:
337
+ fname = items[i][0][0]
338
+ print(f"{fname}:")
339
+ while items[i] and fname == items[i][0][0]:
340
+ rec = items[i].pop(0)
341
+ print(f" {rec[2]:5}:{rec[1]}")
342
+ print()
343
+
344
+
345
+ @click.command()
346
+ @click.argument("directory", type=click.Path(exists=True))
347
+ @click.option(
348
+ "-l",
349
+ "--level",
350
+ type=click.Choice(["directory", "file"]),
351
+ default="directory",
352
+ help="Level of statistics to report: directory or file.",
353
+ )
354
+ def main(directory: str, level: str) -> None:
355
+ if level == "directory":
356
+ directory_summary = analyze_all_directories(directory)
357
+ print_directory_summary(directory_summary)
358
+ elif level == "file":
359
+ file_summary = analyze_all_directories(directory)
360
+ print_file_summary(file_summary)
361
+ print_annotation_summary()
362
+
363
+
364
+ if __name__ == "__main__":
365
+ main()
@@ -0,0 +1,43 @@
1
+ from typing import Set
2
+
3
+ import libcst as cst
4
+
5
+ from righttyper.get_import_details import (
6
+ generate_import_nodes,
7
+ )
8
+ from righttyper.righttyper_types import ImportInfo
9
+
10
+
11
+ class ConstructImportTransformer(cst.CSTTransformer):
12
+ def __init__(
13
+ self,
14
+ imports: Set[ImportInfo],
15
+ root_path: str,
16
+ ) -> None:
17
+ self.imports = imports
18
+ self.root_path = root_path
19
+
20
+ def leave_Module(
21
+ self,
22
+ original_node: cst.Module,
23
+ updated_node: cst.Module,
24
+ ) -> cst.Module:
25
+ new_imports = [] # set()
26
+
27
+ # import site
28
+ # import sysconfig
29
+
30
+ # Paths to the main and user libraries, with an os separator added
31
+ # Used to generate import statements later.
32
+ # purelib = sysconfig.get_paths()["purelib"]
33
+ # platstdlib = sysconfig.get_paths()["platstdlib"]
34
+ # userlib = site.getusersitepackages()
35
+
36
+ for imp in self.imports:
37
+ q = generate_import_nodes(imp.import_details)
38
+ new_imports.extend(q)
39
+
40
+ # Add all import statements at the beginning of the module
41
+ return updated_node.with_changes(
42
+ body=list(new_imports) + list(updated_node.body)
43
+ )
@@ -0,0 +1,94 @@
1
+ import ast
2
+ from typing import TextIO
3
+
4
+ from righttyper.righttyper_types import (
5
+ FunctionName,
6
+ )
7
+
8
+
9
+ def generate_stub(
10
+ input_file: str,
11
+ output_file: str,
12
+ exclude_functions: list[FunctionName],
13
+ ) -> None:
14
+ if exclude_functions is None:
15
+ exclude_functions = []
16
+
17
+ with open(input_file, "r") as file:
18
+ tree = ast.parse(file.read())
19
+
20
+ with open(output_file, "a") as stub_file:
21
+ for node in tree.body:
22
+ if isinstance(node, ast.FunctionDef) or isinstance(
23
+ node, ast.AsyncFunctionDef
24
+ ):
25
+ if node.name not in exclude_functions:
26
+ generate_function_stub(node, stub_file)
27
+ elif isinstance(node, ast.ClassDef):
28
+ generate_class_stub(node, stub_file)
29
+ elif isinstance(node, ast.Assign):
30
+ generate_variable_stub(node, stub_file)
31
+
32
+
33
+ def generate_function_stub(
34
+ node: ast.FunctionDef | ast.AsyncFunctionDef,
35
+ stub_file: TextIO,
36
+ ) -> None:
37
+ args = []
38
+ for arg in node.args.args:
39
+ arg_str = arg.arg
40
+ if arg.annotation:
41
+ arg_str += f": {ast.unparse(arg.annotation).strip()}"
42
+ args.append(arg_str)
43
+ if node.args.vararg:
44
+ vararg_str = f"*{node.args.vararg.arg}"
45
+ if node.args.vararg.annotation:
46
+ vararg_str += (
47
+ f": {ast.unparse(node.args.vararg.annotation).strip()}"
48
+ )
49
+ args.append(vararg_str)
50
+ if node.args.kwarg:
51
+ kwarg_str = f"**{node.args.kwarg.arg}"
52
+ if node.args.kwarg.annotation:
53
+ kwarg_str += f": {ast.unparse(node.args.kwarg.annotation).strip()}"
54
+ args.append(kwarg_str)
55
+ args_str = ", ".join(args)
56
+ if node.returns:
57
+ returns = ast.unparse(node.returns).strip()
58
+ else:
59
+ returns = "None"
60
+ stub_file.write(f"def {node.name}({args_str}) -> {returns}: ...\n")
61
+
62
+
63
+ def generate_class_stub(node: ast.ClassDef, stub_file: TextIO) -> None:
64
+ stub_file.write(f"class {node.name}:\n")
65
+ empty_body = True
66
+ for elem in node.body:
67
+ if isinstance(elem, ast.FunctionDef):
68
+ if elem.name not in [
69
+ "__init__",
70
+ "__new__",
71
+ ]:
72
+ stub_file.write(" ")
73
+ generate_function_stub(elem, stub_file)
74
+ empty_body = False
75
+ elif isinstance(elem, ast.Assign):
76
+ stub_file.write(" ")
77
+ generate_variable_stub(elem, stub_file)
78
+ empty_body = False
79
+ # FIXME TODO what about members?
80
+ if empty_body:
81
+ stub_file.write(" pass")
82
+ stub_file.write("\n")
83
+
84
+
85
+ def generate_variable_stub(node: ast.Assign, stub_file: TextIO) -> None:
86
+ for target in node.targets:
87
+ if isinstance(target, ast.Name):
88
+ if node.value:
89
+ annotation = "Any"
90
+ if isinstance(node.value, ast.Constant):
91
+ annotation = type(node.value.value).__name__
92
+ stub_file.write(f"{target.id}: {annotation}\n")
93
+ else:
94
+ stub_file.write(f"{target.id}: Any\n")