Skip to content

Commit a3c818c

Browse files
committed
feat: adopt Python 3.13 type system features
decorators.py: - Use generic function syntax [T: Callable] (PEP 695) so type checkers preserve the decorated callable's type through Requirements/SVCs decorator_processor.py: - Add DecoratorInfo and ElementResult TypedDict classes with ReadOnly fields (PEP 705, typing.ReadOnly new in 3.13) to express that result dicts are built once and never mutated - Add `type Results = list[ElementResult]` type alias (PEP 695) - Annotate method signatures and internal variables with these types
1 parent 6f211f7 commit a3c818c

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# Copyright © LFV
22

3+
from collections.abc import Callable
34

4-
def Requirements(*requirements):
5-
def decorator(func):
6-
func.requirements = requirements
5+
6+
def Requirements[T: Callable](*requirements: str) -> Callable[[T], T]:
7+
def decorator(func: T) -> T:
8+
func.requirements = requirements # type: ignore[attr-defined]
79
return func
810

911
return decorator
1012

1113

12-
def SVCs(*svc_ids):
13-
def decorator(func):
14-
func.svc_ids = svc_ids
14+
def SVCs[T: Callable](*svc_ids: str) -> Callable[[T], T]:
15+
def decorator(func: T) -> T:
16+
func.svc_ids = svc_ids # type: ignore[attr-defined]
1517
return func
1618

1719
return decorator

‎src/reqstool_python_decorators/processors/decorator_processor.py‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from enum import Enum, unique
44
import os
5+
from typing import ReadOnly, TypedDict
56
from ruamel.yaml import YAML
67
import ast
78

@@ -19,6 +20,21 @@ 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+
type Results = list[ElementResult]
36+
37+
2238
class DecoratorProcessor:
2339
"""
2440
A class for collecting and processing Requirements and SVCs annotations on functions and classes in a directory.
@@ -47,9 +63,9 @@ def __init__(self, *args, **kwargs):
4763
4864
"""
4965
super().__init__(*args, **kwargs)
50-
self.req_svc_results = []
66+
self.req_svc_results: Results = []
5167

52-
def find_python_files(self, directory):
68+
def find_python_files(self, directory) -> list[str]:
5369
"""
5470
Find Python files in the given directory.
5571
@@ -66,7 +82,7 @@ def find_python_files(self, directory):
6682
python_files.append(os.path.join(root, file))
6783
return python_files
6884

69-
def get_functions_and_classes(self, file_path, decorator_names):
85+
def get_functions_and_classes(self, file_path, decorator_names) -> None:
7086
"""
7187
Get information about functions and classes, if annotated with "Requirements" or "SVCs":
7288
decorator filepath, elementKind, name and decorators is saved to list that is returned.
@@ -89,7 +105,7 @@ def get_functions_and_classes(self, file_path, decorator_names):
89105

90106
for node in ast.walk(tree):
91107
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
92-
decorators_info = []
108+
decorators_info: list[DecoratorInfo] = []
93109
for decorator_node in getattr(node, "decorator_list", []):
94110
if isinstance(decorator_node, ast.Call) and isinstance(decorator_node.func, ast.Name):
95111
decorator_name = decorator_node.func.id
@@ -106,7 +122,7 @@ def get_functions_and_classes(self, file_path, decorator_names):
106122
}
107123
)
108124

109-
def write_to_yaml(self, output_file, formatted_data):
125+
def write_to_yaml(self, output_file, formatted_data) -> None:
110126
"""
111127
Write formatted data to a YAML file.
112128
@@ -135,7 +151,7 @@ def map_type(self, input_str) -> str:
135151
mapping = {item.from_value: item.to_value for item in DECORATOR_TYPES}
136152
return mapping.get(input_str, input_str)
137153

138-
def format_results(self, results):
154+
def format_results(self, results: Results) -> dict:
139155
"""
140156
Format the collected results into a structured data format for YAML.
141157

0 commit comments

Comments
 (0)