22
33from enum import Enum , unique
44import os
5+ from typing import ReadOnly , TypedDict
56from ruamel .yaml import YAML
67import ast
78
@@ -19,6 +20,35 @@ def get_from_to(self):
1920 return f"from: { self .from_value } , to: { self .to_value } "
2021
2122
23+ class DecoratorInfo (TypedDict ):
24+ name : ReadOnly [str ]
25+ args : ReadOnly [list [str ]]
26+
27+
28+ class ElementResult (TypedDict ):
29+ fullyQualifiedName : ReadOnly [str ]
30+ elementKind : ReadOnly [str ]
31+ name : ReadOnly [str ]
32+ decorators : ReadOnly [list [DecoratorInfo ]]
33+
34+
35+ class FormattedEntry (TypedDict ):
36+ elementKind : str
37+ fullyQualifiedName : str
38+
39+
40+ class RequirementAnnotations (TypedDict ):
41+ implementations : dict [str , list [FormattedEntry ]]
42+ tests : dict [str , list [FormattedEntry ]]
43+
44+
45+ class FormattedData (TypedDict ):
46+ requirement_annotations : RequirementAnnotations
47+
48+
49+ type Results = list [ElementResult ]
50+
51+
2252class DecoratorProcessor :
2353 """
2454 A class for collecting and processing Requirements and SVCs annotations on functions and classes in a directory.
@@ -47,7 +77,7 @@ def __init__(self, *args, **kwargs):
4777
4878 """
4979 super ().__init__ (* args , ** kwargs )
50- self .req_svc_results = []
80+ self .req_svc_results : Results = []
5181
5282 def find_python_files (self , directory : str | os .PathLike ) -> list [str ]:
5383 """
@@ -86,7 +116,7 @@ def get_functions_and_classes(self, file_path: str | os.PathLike, decorator_name
86116
87117 for node in ast .walk (tree ):
88118 if isinstance (node , (ast .FunctionDef , ast .AsyncFunctionDef , ast .ClassDef )):
89- decorators_info = []
119+ decorators_info : list [ DecoratorInfo ] = []
90120 for decorator_node in getattr (node , "decorator_list" , []):
91121 if isinstance (decorator_node , ast .Call ) and isinstance (decorator_node .func , ast .Name ):
92122 decorator_name = decorator_node .func .id
@@ -132,25 +162,25 @@ def map_type(self, input_str) -> str:
132162 mapping = {item .from_value : item .to_value for item in DECORATOR_TYPES }
133163 return mapping .get (input_str , input_str )
134164
135- def format_results (self , results ) -> dict :
165+ def format_results (self , results : Results ) -> FormattedData :
136166 """
137167 Format the collected results into a structured data format for YAML.
138168
139169 Parameters:
140170 - `results` (list): List of dictionaries containing information about functions and classes.
141171
142172 Returns:
143- - `formatted_data` (dict ): Formatted data in a structured `yaml_language_server` compatible format.
173+ - `formatted_data` (FormattedData ): Formatted data in a structured `yaml_language_server` compatible format.
144174
145175 This function formats a list of decorated data into the structure required by the `yaml_language_server`.
146176 It includes version information, requirement annotations, and relevant element information.
147177 """
148178
149- formatted_data = {}
150- implementations = {}
151- tests = {}
152- requirement_annotations = {"implementations" : implementations , "tests" : tests }
153- formatted_data [ "requirement_annotations" ] = requirement_annotations
179+ implementations : dict [ str , list [ FormattedEntry ]] = {}
180+ tests : dict [ str , list [ FormattedEntry ]] = {}
181+ formatted_data : FormattedData = {
182+ "requirement_annotations" : {"implementations" : implementations , "tests" : tests }
183+ }
154184
155185 for result in results :
156186 for decorator_info in result ["decorators" ]:
0 commit comments