Skip to content

Commit 0e2adcb

Browse files
committed
refactor(schema): stop duplicating field descriptions
1 parent f937229 commit 0e2adcb

2 files changed

Lines changed: 0 additions & 1832 deletions

File tree

scripts/gen_schema.py

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@
2020
VERSION_FILE = SCHEMA_DIR / "VERSION"
2121
SCHEMA_OUT = ROOT / "src" / "acp" / "schema.py"
2222

23-
# Pattern caches used when post-processing generated schema.
24-
FIELD_DECLARATION_PATTERN = re.compile(r"[A-Za-z_][A-Za-z0-9_]*\s*:")
25-
DESCRIPTION_PATTERN = re.compile(
26-
r"description\s*=\s*(?P<prefix>[rRbBuU]*)?(?P<quote>'''|\"\"\"|'|\")(?P<value>.*?)(?P=quote)",
27-
re.DOTALL,
28-
)
29-
3023
STDIO_TYPE_LITERAL = 'Literal["2#-datamodel-code-generator-#-object-#-special-#"]'
3124
MODELS_TO_REMOVE = [
3225
"AgentClientProtocol",
@@ -517,7 +510,6 @@ def postprocess_generated_schema(output_path: Path) -> list[str]:
517510
_ProcessingStep("apply field overrides", _apply_field_overrides),
518511
_ProcessingStep("apply default overrides", _apply_default_overrides),
519512
_ProcessingStep("restore required nullable fields", _restore_required_nullable_fields),
520-
_ProcessingStep("attach description comments", _add_description_comments),
521513
_ProcessingStep("ensure custom BaseModel", _ensure_custom_base_model),
522514
_ProcessingStep("inject field validators", _inject_field_validators),
523515
_ProcessingStep("inject deserialize defaults", _inject_deserialize_defaults),
@@ -1032,77 +1024,6 @@ def replace_block(
10321024
return content
10331025

10341026

1035-
def _add_description_comments(content: str) -> str:
1036-
lines = content.splitlines()
1037-
new_lines: list[str] = []
1038-
index = 0
1039-
1040-
while index < len(lines):
1041-
line = lines[index]
1042-
stripped = line.lstrip()
1043-
indent = len(line) - len(stripped)
1044-
1045-
if indent == 4 and FIELD_DECLARATION_PATTERN.match(stripped or ""):
1046-
block_lines, next_index = _collect_field_block(lines, index, indent)
1047-
block_text = "\n".join(block_lines)
1048-
description = _extract_description(block_text)
1049-
1050-
if description:
1051-
indent_str = " " * indent
1052-
comment_lines = [
1053-
f"{indent_str}# {comment_line}" if comment_line else f"{indent_str}#"
1054-
for comment_line in description.splitlines()
1055-
]
1056-
if comment_lines:
1057-
new_lines.extend(comment_lines)
1058-
1059-
new_lines.extend(block_lines)
1060-
index = next_index
1061-
continue
1062-
1063-
new_lines.append(line)
1064-
index += 1
1065-
1066-
return "\n".join(new_lines)
1067-
1068-
1069-
def _collect_field_block(lines: list[str], start: int, indent: int) -> tuple[list[str], int]:
1070-
block: list[str] = []
1071-
index = start
1072-
1073-
while index < len(lines):
1074-
current_line = lines[index]
1075-
current_indent = len(current_line) - len(current_line.lstrip())
1076-
if index != start and current_line.strip() and current_indent <= indent:
1077-
break
1078-
1079-
block.append(current_line)
1080-
index += 1
1081-
1082-
return block, index
1083-
1084-
1085-
def _extract_description(block_text: str) -> str | None:
1086-
match = DESCRIPTION_PATTERN.search(block_text)
1087-
if not match:
1088-
return None
1089-
1090-
prefix = match.group("prefix") or ""
1091-
quote = match.group("quote")
1092-
value = match.group("value")
1093-
literal = f"{prefix}{quote}{value}{quote}"
1094-
1095-
# datamodel-code-generator emits standard string literals, but fall back to raw text on parse errors.
1096-
try:
1097-
parsed = ast.literal_eval(literal)
1098-
except (SyntaxError, ValueError):
1099-
return value.replace("\\n", "\n")
1100-
1101-
if isinstance(parsed, str):
1102-
return parsed
1103-
return str(parsed)
1104-
1105-
11061027
def _inject_enum_aliases(content: str) -> str:
11071028
enum_lines = [
11081029
f"{name} = Literal[{', '.join(repr(value) for value in values)}]" for name, values in ENUM_LITERAL_MAP.items()

0 commit comments

Comments
 (0)