Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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(

Copy link
Copy Markdown
Contributor

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.Annotated

Annotated here comes from typing_extensions; it is not a schema-node constructor. Each of the seven new mappings raises:

TypeError: Type Annotated cannot be instantiated.

A minimal reproduction of the datetime case is:

from datetime import datetime
from typing_extensions import TypedDict
from typechat import python_type_to_typescript_schema

class Event(TypedDict):
    timestamp: datetime

python_type_to_typescript_schema(Event)

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.

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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Remove the runtime check against a non-runtime-checkable protocol

Annotatedish is declared as a Protocol without @runtime_checkable. This new branch is reached for previously supported non-primitive annotations, before the existing collection/union/reference handling, and raises:

TypeError: Instance and class checks can only be used with @runtime_checkable protocols

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. convert_to_type_node() already calls skip_annotations() at entry, and that helper unwraps Annotated, so this additional branch appears unnecessary. Please remove it and preserve the existing conversion paths rather than adding a broad structural runtime check.

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, ...]:
Expand Down