Skip to content

Commit 84e649e

Browse files
committed
Separated source code transformation from TypeChat src and created separate CLI utility for schema pythonic comment handling
1 parent 6ec8dd3 commit 84e649e

11 files changed

+492
-253
lines changed

python/examples/math/ast_comment_handling.py

Lines changed: 0 additions & 105 deletions
This file was deleted.

python/examples/math/demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
from typing import cast
66
from dotenv import dotenv_values
7-
import schema_with_comments as math
7+
import schema as math
88
from typechat import Failure, create_language_model, process_requests
99
from program import TypeChatProgramTranslator, TypeChatProgramValidator, evaluate_json_program
1010

python/examples/math/program.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ class TypeChatProgramTranslator(TypeChatJsonTranslator[JsonProgram]):
121121
_api_declaration_str: str
122122

123123
def __init__(self, model: TypeChatLanguageModel, validator: TypeChatProgramValidator, api_type: type):
124-
api_type = self._convert_pythonic_comments_to_annotated_docs(api_type)
125124
super().__init__(model=model, validator=validator, target_type=api_type, _raise_on_schema_errors = False)
126125
# TODO: the conversion result here has errors!
127126
conversion_result = python_type_to_typescript_schema(api_type)

python/examples/math/pythonic_comment_handling.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

python/examples/math/schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing_extensions import TypedDict, Annotated, Callable, Doc
22

3+
34
class MathAPI(TypedDict):
45
"""
56
This is API for a simple calculator

python/src/typechat/_internal/translator.py

Lines changed: 1 addition & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
from typing_extensions import Generic, TypeVar
22

33
import pydantic_core
4-
import ast
5-
import io
6-
import tokenize
7-
import inspect
84

95
from typechat._internal.model import PromptSection, TypeChatLanguageModel
106
from typechat._internal.result import Failure, Result, Success
@@ -123,99 +119,4 @@ def _create_repair_prompt(self, validation_error: str) -> str:
123119
'''
124120
The following is a revised JSON object:
125121
"""
126-
return prompt
127-
128-
def _convert_pythonic_comments_to_annotated_docs(schema_class, debug=False):
129-
130-
def _extract_tokens_between_line_numbers(gen, start_lineno, end_lineno):
131-
# Extract tokens between start_lineno and end_lineno obtained from the tokenize generator
132-
tokens = []
133-
for tok in gen:
134-
if tok.start[0] < start_lineno: # Skip tokens before start_lineno
135-
continue
136-
if tok.start[0] >= start_lineno and tok.end[0] <= end_lineno:
137-
# Add token if it is within the range
138-
tokens.append((tok.type, tok.string))
139-
elif tok.start[0] > end_lineno: # Stop if token is beyond end_lineno
140-
break
141-
142-
return tokens
143-
144-
schema_path = inspect.getfile(schema_class)
145-
146-
with open(schema_path, 'r') as f:
147-
schema_class_source = f.read()
148-
gen = tokenize.tokenize(io.BytesIO(
149-
schema_class_source.encode('utf-8')).readline)
150-
151-
tree = ast.parse(schema_class_source)
152-
153-
if debug:
154-
print("Source code before transformation:")
155-
print("--"*50)
156-
print(schema_class_source)
157-
print("--"*50)
158-
159-
has_comments = False # Flag later used to perform imports of Annotated and Doc if needed
160-
161-
for node in tree.body:
162-
if isinstance(node, ast.ClassDef):
163-
for n in node.body:
164-
if isinstance(n, ast.AnnAssign): # Check if the node is an annotated assignment
165-
assgn_comment = None
166-
tokens = _extract_tokens_between_line_numbers(
167-
# Extract tokens between the line numbers of the annotated assignment
168-
gen, n.lineno, n.end_lineno
169-
)
170-
for toknum, tokval in tokens:
171-
if toknum == tokenize.COMMENT:
172-
# Extract the comment
173-
assgn_comment = tokval
174-
break
175-
176-
if assgn_comment:
177-
# If a comment is found, transform the annotation to include the comment
178-
assgn_subscript = n.annotation
179-
has_comments = True
180-
n.annotation = ast.Subscript(
181-
value=ast.Name(id="Annotated", ctx=ast.Load()),
182-
slice=ast.Tuple(
183-
elts=[
184-
assgn_subscript,
185-
ast.Call(
186-
func=ast.Name(
187-
id="Doc", ctx=ast.Load()
188-
),
189-
args=[
190-
ast.Constant(
191-
value=assgn_comment.strip("#").strip()
192-
)
193-
],
194-
keywords=[]
195-
)
196-
],
197-
ctx=ast.Load()
198-
),
199-
ctx=ast.Load()
200-
)
201-
202-
if has_comments:
203-
for node in tree.body:
204-
if isinstance(node, ast.ImportFrom):
205-
if node.module == "typing_extensions":
206-
if ast.alias(name="Annotated") not in node.names:
207-
node.names.append(ast.alias(name="Annotated"))
208-
if ast.alias(name="Doc") not in node.names:
209-
node.names.append(ast.alias(name="Doc"))
210-
211-
transformed_schema_source = ast.unparse(tree)
212-
213-
if debug:
214-
print("Source code after transformation:")
215-
print("--"*50)
216-
print(transformed_schema_source)
217-
print("--"*50)
218-
219-
namespace = {}
220-
exec(transformed_schema_source, namespace)
221-
return namespace[schema_class.__name__]
122+
return prompt

0 commit comments

Comments
 (0)