You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python: runtime validator accepts a string for an inherited dataclass field specialized to int #550
#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.
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.
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.
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.
fromdataclassesimportdataclassfromtypingimportGeneric, TypeVarfromtypechatimportFailure, Success, TypeChatValidatorT=TypeVar("T")
@dataclassclassBox(Generic[T]):
item: T@dataclassclassIntBox(Box[int]):
item: int# Redeclare the field on the concrete subclass.validator=TypeChatValidator(IntBox)
assertvalidator.validate_object({"item": 2}) ==Success(IntBox(2))
result=validator.validate_object({"item": "bad"})
assertisinstance(result, Failure)
assert"Validation path `item`"inresult.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.
Activity
robgruen commentedon Nov 30, 2026
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"}, butTypeAdapter(IntBox)accepts it whenIntBoxinherits fromBox[int]. Explicitly redeclaringitem: intonIntBoxrestores 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.
robgruen commentedon Nov 30, 2026
Workaround: redeclare the inherited field on the concrete dataclass
For the
IntBox(Box[int])example, additem: inttoIntBox. This gives Pydantic the concrete annotation directly. No change to the generic base, TypeChat validator, or installed dependencies is needed.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.