1212import importlib .util
1313import inspect
1414import os
15- from typing import Any , Optional , Type
15+ from typing import Any
1616
1717from pydantic import BaseModel
1818
@@ -23,7 +23,7 @@ def __init__(self, name: str, lineno: int, col_offset: int, original_text: str):
2323 self .lineno = lineno
2424 self .col_offset = col_offset
2525 self .original_text = original_text
26- self .docstring : Optional [ str ] = None
26+ self .docstring : str | None = None
2727
2828 def add_docstring (self , docstring : str ) -> None :
2929 """Add a docstring to the class."""
@@ -49,23 +49,23 @@ def format_docstring(docstring: str, indent: str) -> str:
4949 return '\n ' .join (indented_lines )
5050
5151
52- def get_docstring_from_parent (cls : type ) -> Optional [ str ] :
52+ def get_docstring_from_parent (cls : type ) -> str | None :
5353 """Get the docstring from the parent class."""
5454 for base in cls .__bases__ :
5555 if base .__doc__ :
5656 return base .__doc__
5757 return None
5858
5959
60- def get_field_docstring (cls : Type [BaseModel ], field_name : str ) -> str :
60+ def get_field_docstring (cls : type [BaseModel ], field_name : str ) -> str :
6161 """Get the docstring of a field from the class."""
6262 for name , obj in inspect .getmembers (cls ):
6363 if name == field_name :
6464 return obj .__doc__ or "No docstring provided."
6565 return "No docstring found."
6666
6767
68- def format_type (annotation : Type [Any ] | None ) -> str :
68+ def format_type (annotation : type [Any ] | None ) -> str :
6969 """Format the type to show module and class name."""
7070 if annotation is None :
7171 raise ValueError ("Annotation cannot be None" )
@@ -79,7 +79,7 @@ def format_type(annotation: Type[Any] | None) -> str:
7979 return str (annotation ) # Fallback for other types
8080
8181
82- def get_pydantic_fields (cls : Type [BaseModel ]) -> str :
82+ def get_pydantic_fields (cls : type [BaseModel ]) -> str :
8383 """Get the fields of a Pydantic model and format them as Google-style Args."""
8484 if not issubclass (cls , BaseModel ):
8585 return ""
@@ -109,7 +109,7 @@ def get_pydantic_fields(cls: Type[BaseModel]) -> str:
109109
110110def parse_file (file_path : str ) -> list [ClassInfo ]:
111111 """Parse the file and extract class definitions."""
112- with open (file_path , 'r' ) as file :
112+ with open (file_path ) as file :
113113 lines = file .readlines ()
114114 tree = ast .parse ('' .join (lines ))
115115
@@ -146,7 +146,7 @@ def update_class_docstrings(file_path: str) -> None:
146146 full_docstring = parent_docstring + args_section
147147 class_info .add_docstring (full_docstring )
148148
149- with open (file_path , 'r' , encoding = "utf-8" ) as file :
149+ with open (file_path , encoding = "utf-8" ) as file :
150150 lines = file .readlines ()
151151
152152 updated_lines = []
0 commit comments