Skip to content

Commit

Permalink
Merge pull request #176 from airtai/175-ensure-that-the-skeleton-code…
Browse files Browse the repository at this point in the history
…-generation-does-not-include-function-implementations

Ensure that the skeleton code generation does not include function implementations
  • Loading branch information
rjambrecic authored Oct 11, 2023
2 parents b9d690c + 9f1ba5e commit 0561bb3
Show file tree
Hide file tree
Showing 5 changed files with 824 additions and 44 deletions.
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

0 comments on commit 0561bb3

Please sign in to comment.