Repository navigation
Generate JSON wire schemas for Python runtime types supported by Pydantic #466
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,10 +2,13 @@ | |
|
|
||
| import collections.abc | ||
| from collections import OrderedDict | ||
| import datetime | ||
| import decimal | ||
| import inspect | ||
| import sys | ||
| import typing | ||
| import typing_extensions | ||
| import uuid | ||
| from dataclasses import MISSING, Field, dataclass | ||
| from types import NoneType, UnionType | ||
| from typing_extensions import ( | ||
|
|
@@ -236,6 +239,31 @@ def convert_to_type_node(py_type: object) -> TypeNode: | |
| if py_type is Self: | ||
| return ThisTypeReferenceNode | ||
|
|
||
| # Standard library types supported by Pydantic | ||
| if py_type is datetime.datetime: | ||
| return Annotated( | ||
| StringTypeReferenceNode, | ||
| "ISO 8601 date-time string (e.g., '2022-08-20T08:30:00.000Z')" | ||
| ) | ||
| if py_type is datetime.date: | ||
| return Annotated(StringTypeReferenceNode, "ISO 8601 date string (e.g., '2022-08-20')") | ||
| if py_type is datetime.time: | ||
| return Annotated(StringTypeReferenceNode, "ISO 8601 time string (e.g., '08:30:00')") | ||
| if py_type is datetime.timedelta: | ||
| return Annotated(StringTypeReferenceNode, "ISO 8601 duration string (e.g., 'PT1H')") | ||
| if py_type is uuid.UUID: | ||
| return Annotated( | ||
| StringTypeReferenceNode, | ||
| "UUID string (e.g., '12345678-1234-5678-1234-567812345678')" | ||
| ) | ||
| if py_type is decimal.Decimal: | ||
| return Annotated(StringTypeReferenceNode, "Decimal number string (e.g., '12.50')") | ||
| if py_type is bytes: | ||
| return Annotated(StringTypeReferenceNode, "Base64 encoded bytes or UTF-8 string") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Advertise only the byte encoding the validator actually decodes Even after fixing the AST construction, "Base64 encoded bytes or UTF-8 string" does not match the default validator's decoding behavior. For example: from typing_extensions import TypedDict
from typechat import Success, TypeChatValidator
class Blob(TypedDict):
value: bytes
result = TypeChatValidator(Blob).validate_object({"value": "aGVsbG8="})
assert isinstance(result, Success)
assert result.value["value"] == b"aGVsbG8=" # Not b"hello".The default configuration treats the input as UTF-8 text; a Base64-looking string can validate successfully while producing the wrong intended bytes. Please describe UTF-8 for the default configuration, and advertise Base64 only when the validator is configured to decode it. If configuration-aware schema guidance is not supported, document that limitation rather than presenting both formats as interchangeable. Include tests that assert the actual resulting bytes, not just validation success. |
||
|
|
||
| if isinstance(py_type, Annotatedish): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P1] Remove the runtime check against a non-runtime-checkable protocol
For example, this previously supported schema now fails: from typing_extensions import TypedDict
from typechat import python_type_to_typescript_schema
class Example(TypedDict):
values: list[str]
python_type_to_typescript_schema(Example)The existing collection, generic, tuple, and other schema tests reproduce the regression. |
||
| return convert_to_type_node(py_type.__origin__) | ||
|
|
||
| # TODO: consider handling bare 'tuple' (and list, etc.) | ||
| # https://docs.python.org/3/library/typing.html#annotating-tuples | ||
| # Using plain tuple as an annotation is equivalent to using tuple[Any, ...]: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Return schema AST nodes rather than constructing
typing.AnnotatedAnnotatedhere comes fromtyping_extensions; it is not a schema-node constructor. Each of the seven new mappings raises:A minimal reproduction of the datetime case is:
The normal translator constructor consequently fails before any model request, so the primary acceptance criterion in #454 remains unmet. The same error was reproduced for date, time, timedelta, UUID, Decimal, and bytes.
Please emit valid converter AST nodes, such as the existing string type-reference node, and attach format guidance through the schema's comment/documentation machinery. Merely changing this to
Annotated[...]would still not produce a supported AST node. Add offline tests for successful schema generation, translator construction, and typed translation results.