Skip to content

Commit 0d5a7a4

Browse files
committed
Separated source code transformation from TypeChat src and created separate CLI utility for schema pythonic comment handling
1 parent bb87a9d commit 0d5a7a4

File tree

11 files changed

+492
-253
lines changed

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

0 commit comments

Comments
 (0)