🐛 fix(code_parser.py): handle case when code parameter is a class by getting its source code using inspect module
✨ feat(code_parser.py): add support for parsing class source code in addition to string source code to improve flexibility and usability
This commit is contained in:
parent
d668640791
commit
4827c99368
1 changed files with 8 additions and 2 deletions
|
|
@ -1,7 +1,8 @@
|
||||||
import ast
|
import ast
|
||||||
|
import inspect
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from typing import Dict, Any, Union
|
from typing import Dict, Any, Type, Union
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -14,10 +15,15 @@ class CodeParser:
|
||||||
A parser for Python source code, extracting code details.
|
A parser for Python source code, extracting code details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, code: str) -> None:
|
def __init__(self, code: Union[str, Type]) -> None:
|
||||||
"""
|
"""
|
||||||
Initializes the parser with the provided code.
|
Initializes the parser with the provided code.
|
||||||
"""
|
"""
|
||||||
|
if isinstance(code, type):
|
||||||
|
if not inspect.isclass(code):
|
||||||
|
raise ValueError("The provided code must be a class.")
|
||||||
|
# If the code is a class, get its source code
|
||||||
|
code = inspect.getsource(code)
|
||||||
self.code = code
|
self.code = code
|
||||||
self.data: Dict[str, Any] = {
|
self.data: Dict[str, Any] = {
|
||||||
"imports": [],
|
"imports": [],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue