Skip to content

Python: runtime validator accepts a string for an inherited dataclass field specialized to int #550

Todo

Description

@robgruen

#549 fixed the prompt schema for the generic dataclass example from #548: IntBox(Box[int]) now emits item: number with no schema errors. The Python runtime validator still accepts a string for that field and returns a successfully constructed IntBox. This behavior was also present before #549; this is a runtime-validation follow-up, not a regression in the schema fix.

Reproduction

Tested on main at 7118017 with Python 3.14.10 and Pydantic 2.14.2. No model or credentials are needed.

from dataclasses import dataclass
from typing import Generic, TypeVar
from typechat import TypeChatValidator, python_type_to_typescript_schema

T = TypeVar("T")

@dataclass
class Box(Generic[T]):
    item: T

@dataclass
class IntBox(Box[int]):
    pass

schema = python_type_to_typescript_schema(IntBox)
print(schema.typescript_schema_str)  # IntBox declares item: number;
print(schema.errors)                # []
validator = TypeChatValidator(IntBox)
print(validator.validate_object({"item": 2}))      # Success(value=IntBox(item=2))
print(validator.validate_object({"item": "bad"}))  # Success(value=IntBox(item='bad'))

Expected: the integer succeeds, but {"item": "bad"} returns Failure identifying item. Callers should not receive a successful result containing a string where the target dataclass specifies an integer.

Diagnostic clue: in the same environment, TypeAdapter(IntBox).json_schema()["properties"]["item"] returns only {'title': 'Item'}, with no type constraint.

Suggested regression coverage: exercise both inputs through the public validator and preserve the specialized prompt schema from #549. No change to the TypeScript implementation or the separate InitVar work in #547 is needed.

Activity

  1. added
    Bug
    Language: Python
    on Nov 30, 2026
  2. robgruen commented on Nov 30, 2026

    @robgruen
    ContributorAuthor

    Upstream tracking: pydantic/pydantic#11363. A Pydantic maintainer confirmed that type-variable substitution is lost on inherited dataclass fields.

    This reproduces without TypeChat on Pydantic 2.14.2: in strict JSON mode TypeAdapter(Box[int]) rejects {"item":"bad"}, but TypeAdapter(IntBox) accepts it when IntBox inherits from Box[int]. Explicitly redeclaring item: int on IntBox restores validation and is a workaround for this example. #549 already corrected the prompt schema; this issue tracks runtime behavior only.

    An upstream contributor proposed carrying type arguments through the generic bases and requested assignment. As of November 30, 2026, the upstream issue is open and unassigned; I found no public Pydantic implementation PR to link yet.

    For anyone considering submitting a patch: Pydantic's current contribution policy does not accept external nontrivial bug-fix PRs or requests for assignment.

  3. robgruen commented on Nov 30, 2026

    @robgruen
    ContributorAuthor

    Workaround: redeclare the inherited field on the concrete dataclass

    For the IntBox(Box[int]) example, add item: int to IntBox. This gives Pydantic the concrete annotation directly. No change to the generic base, TypeChat validator, or installed dependencies is needed.

    from dataclasses import dataclass
    from typing import Generic, TypeVar
    from typechat import Failure, Success, TypeChatValidator
    
    T = TypeVar("T")
    
    @dataclass
    class Box(Generic[T]):
        item: T
    
    @dataclass
    class IntBox(Box[int]):
        item: int  # Redeclare the field on the concrete subclass.
    
    validator = TypeChatValidator(IntBox)
    assert validator.validate_object({"item": 2}) == Success(IntBox(2))
    result = validator.validate_object({"item": "bad"})
    assert isinstance(result, Failure)
    assert "Validation path `item`" in result.message

    Verified with unpatched Pydantic 2.14.2. This works around the runtime issue for this field; the general fix is tracked in pydantic/pydantic#11363.

  4. self-assigned this
    on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugLanguage: Python

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions