Skip to content

Python schema conversion crashes on inherited Annotated fields with unhashable metadata #543

Backlog

Description

Bug

Inherited TypedDict fields using valid but unhashable Annotated metadata cause the Python-to-TypeScript schema converter to raise TypeError.

Reproduction

from typing_extensions import Annotated, TypedDict
from typechat import python_type_to_typescript_schema, TypeChatValidator

class Base(TypedDict):
    name: Annotated[str, {"description": "display name"}]

class Child(Base):
    count: int

print(TypeChatValidator(Child).validate_object({"name": "demo", "count": 1}))
print(python_type_to_typescript_schema(Child))

Expected: successful validation and schema generation.
Current source behavior: TypeError: unhashable type: 'dict'.

Root cause

The converter stores inherited annotations in a set, although Annotated metadata need not be hashable.

base_attributes: OrderedDict[str, set[object]] = OrderedDict()
for base in raw_but_filtered_bases:
for prop, type_hint in get_type_hints(get_origin(base) or base, include_extras=True).items():
base_attributes.setdefault(prop, set()).add(type_hint)

Suggested fix

Change base_attributes to OrderedDict[str, list[object]], replace setdefault(prop, set()).add(type_hint) with setdefault(prop, []).append(type_hint), and update attribute_identical_in_all_bases to use:

return attr_name in base_attributes and all(
    base_hint == type_hint for base_hint in base_attributes[attr_name]
)

Update the helper's parameter type accordingly.

Regression tests

Cover inherited dictionary metadata, identical base annotations, and conflicting base annotations.

Verification

The upstream inheritance statements reproduced the exception on Python 3.13.5 with Pydantic 2.13.5. Proposed comparison logic passed isolated tests. Full integration tests were not run. No matching open or closed issue or PR was found. Unlike #454, this involves an already-supported string field.

AI-assisted investigation and report.

Activity

  1. shubhambhar007 commented on Nov 25, 2026

    @shubhambhar007

    Matthew Wesney (@dovvnloading) can i please try this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions