Backtestspydanticrun-20261007-130130#13857
_FieldNameStack/_ModelTypeStack lose their pop() on exception, corrupting later unrelated schema builds
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
The core fix is identical in both patches. Each wraps the `yield` in `try`/`finally` in both `_FieldNameStack.push()` and `_ModelTypeStack.push()`, which addresses the root cause the issue describes. The difference is in the extras and the tests. Patch A applies the same fix to `generic_recursion_self_type` in `_generics.py`, which had the same shape and the same bug. This is low-risk hardening. It earns no direct credit, but it is clearly correct, it is not risky, and it comes with its own regression test. On tests, Patch A is stronger. It adds direct unit tests on a real `GenerateSchema` instance that reproduce the issue's scenario exactly: raise inside `push`, check that `.get()` is `None`, then do an unrelated push. These unambiguously fail before the fix and pass after. It also adds an end-to-end test where `InstanceOf` swallows a schema error and a later `Self` reference must resolve to the right model. Finally, it adds a nested-exception test for the generics recursion contextvar. Patch B relies only on indirect integration tests. Each uses a custom `__get_pydantic_core_schema__` that swallows a TypedDict schema error and then observes either `Self` resolution or `handler.field_name`. These plausibly fail before the fix as well, but they depend on internal behavior (that TypedDict schema generation pushes onto these stacks) rather than testing the stacks directly. B's tests also use `Self | None` syntax, which may cause problems on older Python versions, and they carry placeholder `"""<link>"""` docstrings. Overall, A's tests are more direct and robust, and its extra fix is a sound, consistent extension.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
7m 54s
Wall-clock time in the container
Full test suite
No new failures
12,851 passed · 526 skipped · 13,377 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
105vs 62 merged
Changed lines, ours vs merged
The issue
View on GitHubDescription
_FieldNameStack.push() and _ModelTypeStack.push() in pydantic/_internal/_generate_schema.py (around lines 2910/2929) push onto their internal list, yield, then pop:
@contextmanager
def push(self, field_name: str) -> Iterator[None]:
self._stack.append(field_name)
yield
self._stack.pop()
If the code inside the with self.field_name_stack.push(...): block raises (schema generation is deeply recursive and can legitimately raise, e.g. PydanticUserError for a circular reference a few lines above this class in the same file), the yield's continuation - the .pop() - never runs. The stale entry stays on the stack forever, and corrupts every later, otherwise-unrelated push()/get() on that same GenerateSchema instance, not just the one that failed.
Reproduction
Isolated (no pydantic-core needed to demonstrate - this is pure Python control flow, verified against the actual class body copied verbatim):
from contextlib import contextmanager
class _FieldNameStack:
__slots__ = ('_stack',)
def __init__(self):
self._stack = []
@contextmanager
def push(self, field_name):
self._stack.append(field_name)
yield
self._stack.pop()
def get(self):
return self._stack[-1] if self._stack else None
stack = _FieldNameStack()
try:
with stack.push('some_field'):
raise ValueError('boom') # simulates any exception during schema generation
except ValueError:
pass
print(stack.get()) # 'some_field' - already wrong, nothing is being processed
# The real damage: this leaks into a completely unrelated, later, successful push.
with stack.push('other_field'):
pass
print(stack.get()) # 'some_field' again - should be None, 'other_field' was popped
# correctly but the stale entry underneath it was never cleared
Output:
some_field
some_field
Both lines should print None.
Fix
Wrap the yield in try/finally so the pop runs unconditionally, the same guarantee a class-based context manager's __exit__ gets for free:
@contextmanager
def push(self, field_name: str) -> Iterator[None]:
self._stack.append(field_name)
try:
yield
finally:
self._stack.pop()
Same fix applies identically to _ModelTypeStack.push() a few lines below - same shape, same bug.
I have a fix + regression test ready (asserts .get() returns None after a push whose body raised) and I'm happy to open a PR, per the note in the PR template about discussing non-trivial changes here first.
The two patches
pydantic/_internal/_generate_schema.pyindex b458013a..a446559b 100644@@ -2910,8 +2910,10 @@ class _FieldNameStack: @contextmanager def push(self, field_name: str) -> Iterator[None]: self._stack.append(field_name)- yield- self._stack.pop()+ try:+ yield+ finally:+ self._stack.pop() def get(self) -> str | None: if self._stack:@@ -2929,8 +2931,10 @@ class _ModelTypeStack: @contextmanager def push(self, type_obj: type) -> Iterator[None]: self._stack.append(type_obj)- yield- self._stack.pop()+ try:+ yield+ finally:+ self._stack.pop() def get(self) -> type | None: if self._stack:pydantic/_internal/_generics.pyindex 15c4c56d..45f9aeff 100644@@ -432,8 +432,10 @@ def generic_recursion_self_type( yield self_type else: previously_seen_type_refs.add(type_ref)- yield- previously_seen_type_refs.remove(type_ref)+ try:+ yield+ finally:+ previously_seen_type_refs.remove(type_ref) finally: if token: _generic_recursion_cache.reset(token)tests/test_generics.pyindex f8c396cf..a343a48d 100644@@ -2473,6 +2473,34 @@ def test_generic_recursion_contextvar(): assert not recursively_defined_type_refs() +def test_generic_recursion_contextvar_nested_exception():+ T = TypeVar('T')++ class TestingException(Exception):+ pass++ class Model(BaseModel, Generic[T]):+ pass++ with generic_recursion_self_type(Model, (int,)):+ outer_refs = recursively_defined_type_refs()+ try:+ with generic_recursion_self_type(Model, (str,)):+ assert len(recursively_defined_type_refs()) == len(outer_refs) + 1+ raise TestingException+ except TestingException:+ pass++ # The inner type ref must be removed even though the outer recursion (and thus the cache) is still active:+ assert recursively_defined_type_refs() == outer_refs++ # Otherwise, a later, unrelated parametrization would wrongly be treated as a recursive self-reference:+ with generic_recursion_self_type(Model, (str,)) as maybe_self_type:+ assert maybe_self_type is None++ assert not recursively_defined_type_refs()++ def test_limited_dict(): d = LimitedDict(10) d[1] = '1'tests/test_internal.pyindex 3bfc123f..842942c4 100644@@ -6,14 +6,15 @@ import sys from copy import deepcopy from dataclasses import dataclass from decimal import Decimal-from typing import Any+from typing import Any, Optional import pytest from dirty_equals import Contains, IsPartialDict from pydantic_core import CoreSchema, PydanticUndefined from pydantic_core import core_schema as cs+from typing_extensions import Self -from pydantic import BaseModel, TypeAdapter+from pydantic import BaseModel, InstanceOf, TypeAdapter from pydantic._internal._config import ConfigWrapper from pydantic._internal._core_metadata import update_core_metadata from pydantic._internal._core_utils import as_ser_schema@@ -356,3 +357,57 @@ def _wrap_identity(v: Any, handler: Any) -> Any: ) def test_as_ser_schema(schema: CoreSchema, expected: cs.SerSchema) -> None: assert as_ser_schema(schema) == expected+++def test_field_name_stack_pops_on_exception() -> None:+ gen = GenerateSchema(ConfigWrapper(None))++ with pytest.raises(ValueError):+ with gen.field_name_stack.push('failing_field'):+ raise ValueError('boom')++ assert gen.field_name_stack.get() is None++ # A later, unrelated push must not see the stale entry once it is popped:+ with gen.field_name_stack.push('other_field'):+ assert gen.field_name_stack.get() == 'other_field'+ assert gen.field_name_stack.get() is None+++def test_model_type_stack_pops_on_exception() -> None:+ class FailingModel:+ pass++ class OtherModel:+ pass++ gen = GenerateSchema(ConfigWrapper(None))++ with pytest.raises(ValueError):+ with gen.model_type_stack.push(FailingModel):+ raise ValueError('boom')++ assert gen.model_type_stack.get() is None++ with gen.model_type_stack.push(OtherModel):+ assert gen.model_type_stack.get() is OtherModel+ assert gen.model_type_stack.get() is None+++def test_swallowed_schema_error_does_not_corrupt_model_type_stack() -> None:+ """`InstanceOf` swallows schema generation errors of its argument while the same `GenerateSchema`+ instance keeps building the enclosing model, so the stacks must be cleaned up on exception."""++ class Arbitrary:+ pass++ @dataclass+ class DC:+ x: Arbitrary # Fails schema generation (arbitrary types not allowed), swallowed by `InstanceOf`.++ class Model(BaseModel):+ a: InstanceOf[DC]+ b: Optional[Self] = None++ m = Model(a=DC(Arbitrary()), b=Model(a=DC(Arbitrary())))+ assert type(m.b) is Model
pydantic/_internal/_generate_schema.pyindex b458013ae..a446559b8 100644@@ -2910,8 +2910,10 @@ class _FieldNameStack: @contextmanager def push(self, field_name: str) -> Iterator[None]: self._stack.append(field_name)- yield- self._stack.pop()+ try:+ yield+ finally:+ self._stack.pop() def get(self) -> str | None: if self._stack:@@ -2929,8 +2931,10 @@ class _ModelTypeStack: @contextmanager def push(self, type_obj: type) -> Iterator[None]: self._stack.append(type_obj)- yield- self._stack.pop()+ try:+ yield+ finally:+ self._stack.pop() def get(self) -> type | None: if self._stack:tests/test_edge_cases.pyindex f5e0de1a1..981d787c0 100644@@ -3218,3 +3218,53 @@ def test_get_pydantic_core_schema_instance() -> None: ta = TypeAdapter(SchemaProvider()) assert ta.validate_python('1') == 1+++def test_model_type_stack_popped_after_schema_generation_error() -> None:+ """https://github.com/pydantic/pydantic/issues/13857"""++ class Unsupported:+ pass++ class Bad(TypedDict):+ x: Unsupported++ class Fallback:+ @classmethod+ def __get_pydantic_core_schema__(cls, source: Any, handler: GetCoreSchemaHandler) -> CoreSchema:+ try:+ return handler.generate_schema(Bad)+ except PydanticSchemaGenerationError:+ return core_schema.any_schema()++ class Model(BaseModel):+ a: Fallback+ child: Self | None = None++ m = Model.model_validate({'a': 1, 'child': {'a': 2}})+ assert isinstance(m.child, Model)+++def test_field_name_stack_popped_after_schema_generation_error() -> None:+ """https://github.com/pydantic/pydantic/issues/13857"""++ class Unsupported:+ pass++ class Bad(TypedDict):+ x: Unsupported++ field_names: list[str | None] = []++ class Fallback:+ @classmethod+ def __get_pydantic_core_schema__(cls, source: Any, handler: GetCoreSchemaHandler) -> CoreSchema:+ try:+ handler.generate_schema(Bad)+ except PydanticSchemaGenerationError:+ pass+ field_names.append(handler.field_name)+ return core_schema.any_schema()++ TypeAdapter(Fallback)+ assert field_names == [None]