Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that the skeleton code generation does not include function implementations #176

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion faststream_gen/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.7rc1"
__version__ = "0.1.7rc2"
40 changes: 34 additions & 6 deletions faststream_gen/_code_generator/app_skeleton_generator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/App_Skeleton_Generator.ipynb.

# %% auto 0
__all__ = ['logger', 'generate_app_skeleton']
__all__ = ['logger', 'CODE_CONTAINS_IMPLEMENTATION_ERROR', 'generate_app_skeleton']

# %% ../../nbs/App_Skeleton_Generator.ipynb 1
from typing import *
import time
import json
import ast
from pathlib import Path
from collections import defaultdict

Expand All @@ -30,16 +31,43 @@
logger = get_logger(__name__)

# %% ../../nbs/App_Skeleton_Generator.ipynb 5
CODE_CONTAINS_IMPLEMENTATION_ERROR = "Error: The response contains code implementation. Rewrite the skeleton code without implementing the business logic for the functions. Ensure the new code has only google styled docstring describing the business logic step by step and raise NotImplementedError()"

# %% ../../nbs/App_Skeleton_Generator.ipynb 6
def _has_function_implementation(
node: Union[ast.AsyncFunctionDef, ast.FunctionDef]
) -> bool:
return len(node.body) == 2 and isinstance(node.body[-1], ast.Raise)


def _check_response_for_implementation(response: str) -> List[str]:
parsed = ast.parse(response)
function_nodes = [
node
for node in ast.walk(parsed)
if isinstance(node, ast.AsyncFunctionDef) or isinstance(node, ast.FunctionDef)
]
ret_val = (
[]
if all(_has_function_implementation(node) for node in function_nodes)
else [CODE_CONTAINS_IMPLEMENTATION_ERROR]
)
return ret_val

# %% ../../nbs/App_Skeleton_Generator.ipynb 9
def _validate_response(
response: str, output_directory: str, **kwargs: Dict[str, Any]
) -> List[str]:
target_file_name = Path(output_directory) / APPLICATION_FILE_PATH
write_file_contents(str(target_file_name), response)
ret_val = validate_python_code(str(target_file_name))

return ret_val
code_import_errors = validate_python_code(str(target_file_name))

if len(code_import_errors) != 0:
return code_import_errors

return _check_response_for_implementation(response)

# %% ../../nbs/App_Skeleton_Generator.ipynb 8
# %% ../../nbs/App_Skeleton_Generator.ipynb 15
@retry_on_error() # type: ignore
def _generate(
model: str,
Expand Down Expand Up @@ -72,7 +100,7 @@ def _generate(
else validator_result
)

# %% ../../nbs/App_Skeleton_Generator.ipynb 11
# %% ../../nbs/App_Skeleton_Generator.ipynb 18
def generate_app_skeleton(
validated_description: str,
output_directory: str,
Expand Down
6 changes: 5 additions & 1 deletion faststream_gen/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
'faststream_gen/_code_generator/app_and_test_generator.py')},
'faststream_gen._code_generator.app_description_validator': { 'faststream_gen._code_generator.app_description_validator.validate_app_description': ( 'app_description_validator.html#validate_app_description',
'faststream_gen/_code_generator/app_description_validator.py')},
'faststream_gen._code_generator.app_skeleton_generator': { 'faststream_gen._code_generator.app_skeleton_generator._generate': ( 'app_skeleton_generator.html#_generate',
'faststream_gen._code_generator.app_skeleton_generator': { 'faststream_gen._code_generator.app_skeleton_generator._check_response_for_implementation': ( 'app_skeleton_generator.html#_check_response_for_implementation',
'faststream_gen/_code_generator/app_skeleton_generator.py'),
'faststream_gen._code_generator.app_skeleton_generator._generate': ( 'app_skeleton_generator.html#_generate',
'faststream_gen/_code_generator/app_skeleton_generator.py'),
'faststream_gen._code_generator.app_skeleton_generator._has_function_implementation': ( 'app_skeleton_generator.html#_has_function_implementation',
'faststream_gen/_code_generator/app_skeleton_generator.py'),
'faststream_gen._code_generator.app_skeleton_generator._validate_response': ( 'app_skeleton_generator.html#_validate_response',
'faststream_gen/_code_generator/app_skeleton_generator.py'),
'faststream_gen._code_generator.app_skeleton_generator.generate_app_skeleton': ( 'app_skeleton_generator.html#generate_app_skeleton',
Expand Down
Loading