Backtestspydanticrun-20261007-130130#13621
Memory leak: transient models using Enum methods or Field(exclude_if=...) survive GC
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches make the three traversal changes the issue identifies: `serialization_exclude_if` on `SerField`, `class` on `EnumSerializer`, and `lookup` on every `EnumValidator<T>`. So both address the root cause of the reported leak. Patch A's problems: - It rewrites existing GC tests in ways unrelated to the fix. It renames `__schema__`/`__validator__` to `__pydantic_core_schema__`/`__pydantic_validator__` and turns comments into docstrings. - More seriously, it removes the `xfail(is_free_threaded and sys.version_info < (3, 14))` markers from the existing tests and does not add them to its new tests. That can cause failures on free-threaded Python builds before 3.14. This is risky churn that obscures the fix. - Its new enum tests only cover the `str` subtype, not all four `EnumValidator` variants. Patch B's strengths: - It also fixes nearby missing traversal edges of the same kind: `ComputedField.serialization_exclude_if`, `GeneralFieldsSerializer.extra_serializer`, `extras_keys_validator`, the dataclass `extras_validator`, `LiteralLookup`'s unhashable values and primitives, and custom error contexts. These are additive traversal edges with low risk. The computed-field `exclude_if` case is directly analogous to the reported bug. - Its tests are much stronger. They are parametrized over the plain, int, str and float enum validators. Each test builds a real reference cycle through an enum method that closes over the owner, matching the issue's mechanism. - It adds pydantic-level regression tests that reproduce the issue's transient-module scenario end to end. - It keeps the existing free-threaded and PyPy markers. The one risk for B is that its larger Rust surface (the `ErrorType` macro impl and `LiteralLookup` field types) has to compile. It looks consistent with the existing types. Overall, B has better tests, less harmful churn, and equal or better correctness.
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
25m 22s
Wall-clock time in the container
Full test suite
No new failures
12,344 passed · 500 skipped · 12,844 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
422vs 139 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial Checks
- I confirm that I'm using Pydantic V2
Description
Transient BaseModel classes are not collected when either:
- A model field references an
Enumwhose class defines a Python method. - A model field uses
Field(exclude_if=<Python callable>).
After the defining module is removed from sys.modules and every ordinary strong reference is dropped, weak references to the model and enum classes remain alive after repeated full gc.collect() calls. Repeating this construction in disposable module namespaces causes linear memory growth.
Controls using an enum without methods or a field without exclude_if are collected normally.
I encountered this in combination with a Temporal workflow sandbox. With a large interconnected model graph and high volume Temporal workflows this can result in multi-GiB process growth relatively quickly.
The likely cause is missing PyGcTraverse edges for Python objects held by pydantic-core:
SerFieldownsserialization_exclude_if, but traverses onlyserializer: https://github.com/pydantic/pydantic/blob/c67a1397327c6abc1a2e2e921af01aebbbeedf1c/pydantic-core/src/serializers/fields.rs#L26-L38EnumSerializerownsclass, but traverses onlyserializer: https://github.com/pydantic/pydantic/blob/c67a1397327c6abc1a2e2e921af01aebbbeedf1c/pydantic-core/src/serializers/type_serializers/enum_.rs- Each
EnumValidator<T>ownslookup, but traverses onlyclassandmissing: https://github.com/pydantic/pydantic/blob/c67a1397327c6abc1a2e2e921af01aebbbeedf1c/pydantic-core/src/validators/enum_.rs
Traversing py_schema does not compensate for another unreported direct reference: CPython’s collector accounts for every actual strong edge individually.
I tested a temporary build from Pydantic main at c67a1397327c6abc1a2e2e921af01aebbbeedf1c. Adding only these traversal edges made all objects in the reproduction collectible:
- Add
serialization_exclude_iftoSerFieldtraversal. - Add
classtoEnumSerializertraversal. - Add
lookupto everyEnumValidator<T>traversal.
For the enum case, changing only the serializer or only the validator was insufficient; both missing edges were required.
This appears related to earlier missing-traversal fixes such as:
- https://github.com/pydantic/pydantic/issues/5136
- https://github.com/pydantic/pydantic-core/pull/787
- https://github.com/pydantic/pydantic-core/pull/1100
I would be happy to submit a focused patch.
(please note that this analysis was LLM assisted)
Example Code
import gc
import sys
import types
import weakref
def transient_module(name, source, class_names):
module = types.ModuleType(name)
sys.modules[name] = module
exec(compile(source, f"<{name}>", "exec"), module.__dict__)
refs = tuple(
weakref.ref(module.__dict__[class_name])
for class_name in class_names
)
del sys.modules[name]
del module
return refs
enum_ref, enum_model_ref = transient_module(
"_pydantic_enum_gc_repro",
"""
from enum import Enum
from pydantic import BaseModel
class Membership(str, Enum):
FULL = "full"
def label(self) -> str:
return self.value
class Model(BaseModel):
membership: Membership
""",
("Membership", "Model"),
)
(exclude_if_model_ref,) = transient_module(
"_pydantic_exclude_if_gc_repro",
"""
from pydantic import BaseModel, Field
class Model(BaseModel):
value: int = Field(exclude_if=lambda value: False)
""",
("Model",),
)
for _ in range(5):
gc.collect()
# All three should be False because no intentional strong references remain.
print("enum alive:", enum_ref() is not None)
print("enum model alive:", enum_model_ref() is not None)
print("exclude_if model alive:", exclude_if_model_ref() is not None)
Python, Pydantic & OS Version
pydantic version: 2.12.5
pydantic-core version: 2.41.5
pydantic-core build: profile=release pgo=false
python version: 3.11.15 (main, Aug 5 2026, 03:58:57) [GCC 12.2.0]
platform: Linux-6.5.0-1018-oem-x86_64-with-glibc2.36
related packages: typing_extensions-4.15.0 pydantic-extra-types-2.10.5 mypy-1.16.0
commit: unknownThe two patches
pydantic-core/src/errors/types.rsindex 748d2d5b..884b8dea 100644@@ -140,6 +140,16 @@ macro_rules! error_types { } } + impl crate::py_gc::PyGcTraverse for ErrorType {+ fn py_gc_traverse(&self, visit: &pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> {+ match self {+ $(+ Self::$item { context, .. } => context.py_gc_traverse(visit),+ )++ }+ }+ }+ pub struct ErrorTypeDefaults {} // Allow unused default constants as they are generated by macro. // Also allow camel case as constants so we dont need to do case conversion of macropydantic-core/src/errors/value_exception.rsindex e648e0ac..be3bca51 100644@@ -105,6 +105,8 @@ impl PydanticCustomError { } } +impl_py_gc_traverse!(PydanticCustomError { context });+ impl PydanticCustomError { pub fn into_val_error(self, input: impl ToErrorValue) -> ValError { let error_type = ErrorType::CustomError {@@ -181,6 +183,8 @@ impl PydanticKnownError { } } +impl_py_gc_traverse!(PydanticKnownError { error_type });+ impl PydanticKnownError { pub fn into_val_error(self, input: impl ToErrorValue) -> ValError { ValError::new(self.error_type, input)pydantic-core/src/serializers/computed_fields.rsindex 5bb3841b..4e1946dd 100644@@ -110,7 +110,10 @@ impl ComputedField { } } -impl_py_gc_traverse!(ComputedField { serializer });+impl_py_gc_traverse!(ComputedField {+ serializer,+ serialization_exclude_if+}); impl PyGcTraverse for ComputedFields { fn py_gc_traverse(&self, visit: &PyVisit<'_>) -> Result<(), PyTraverseError> {pydantic-core/src/serializers/fields.rsindex ba18ee4f..d0905aac 100644@@ -35,7 +35,10 @@ pub(super) struct SerField { pub serialization_exclude_if: Option<Py<PyAny>>, } -impl_py_gc_traverse!(SerField { serializer });+impl_py_gc_traverse!(SerField {+ serializer,+ serialization_exclude_if+}); impl SerField { pub fn new(@@ -337,6 +340,7 @@ pub fn exclude_field_by_value<'py>( impl_py_gc_traverse!(GeneralFieldsSerializer { fields,+ extra_serializer, computed_fields }); pydantic-core/src/serializers/type_serializers/enum_.rsindex 0d2819ac..1ad123e9 100644@@ -48,7 +48,7 @@ impl BuildSerializer for EnumSerializer { } } -impl_py_gc_traverse!(EnumSerializer { serializer });+impl_py_gc_traverse!(EnumSerializer { class, serializer }); impl TypeSerializer for EnumSerializer { fn to_python<'py>(&self, value: &Bound<'py, PyAny>, state: &mut SerializationState<'py>) -> PyResult<Py<PyAny>> {pydantic-core/src/validators/custom_error.rsindex 08c0e942..0f9c8df4 100644@@ -3,11 +3,13 @@ use std::sync::Arc; use pyo3::intern; use pyo3::prelude::*; use pyo3::types::PyDict;+use pyo3::{PyTraverseError, PyVisit}; use crate::build_tools::py_schema_err; use crate::errors::ToErrorValue; use crate::errors::{ErrorType, PydanticCustomError, PydanticKnownError, ValError, ValResult}; use crate::input::Input;+use crate::py_gc::PyGcTraverse; use crate::tools::SchemaDict; use super::validation_state::ValidationState;@@ -19,6 +21,15 @@ pub enum CustomError { KnownError(PydanticKnownError), } +impl PyGcTraverse for CustomError {+ fn py_gc_traverse(&self, visit: &PyVisit<'_>) -> Result<(), PyTraverseError> {+ match self {+ CustomError::Custom(custom_error) => custom_error.py_gc_traverse(visit),+ CustomError::KnownError(known_error) => known_error.py_gc_traverse(visit),+ }+ }+}+ impl CustomError { pub fn build( schema: &Bound<'_, PyDict>,@@ -87,7 +98,7 @@ impl BuildValidator for CustomErrorValidator { } } -impl_py_gc_traverse!(CustomErrorValidator { validator });+impl_py_gc_traverse!(CustomErrorValidator { validator, custom_error }); impl Validator for CustomErrorValidator { fn validate<'py>(pydantic-core/src/validators/dataclass.rsindex 93f9c56c..87c9d836 100644@@ -136,7 +136,10 @@ impl BuildValidator for DataclassArgsValidator { impl_py_gc_traverse!(Field { validator }); -impl_py_gc_traverse!(DataclassArgsValidator { fields });+impl_py_gc_traverse!(DataclassArgsValidator {+ fields,+ extras_validator+}); impl Validator for DataclassArgsValidator { fn validate<'py>(pydantic-core/src/validators/enum_.rsindex 6ef685da..28a32a87 100644@@ -168,7 +168,7 @@ impl<T: EnumValidateValue> Validator for EnumValidator<T> { #[derive(Debug, Clone)] pub struct PlainEnumValidator; -impl_py_gc_traverse!(EnumValidator<PlainEnumValidator> { class, missing });+impl_py_gc_traverse!(EnumValidator<PlainEnumValidator> { class, lookup, missing }); impl EnumValidateValue for PlainEnumValidator { fn validate_value<'py, I: Input<'py> + ?Sized>(@@ -200,7 +200,7 @@ impl EnumValidateValue for PlainEnumValidator { #[derive(Debug, Clone)] pub struct IntEnumValidator; -impl_py_gc_traverse!(EnumValidator<IntEnumValidator> { class, missing });+impl_py_gc_traverse!(EnumValidator<IntEnumValidator> { class, lookup, missing }); impl EnumValidateValue for IntEnumValidator { fn validate_value<'py, I: Input<'py> + ?Sized>(@@ -216,7 +216,7 @@ impl EnumValidateValue for IntEnumValidator { #[derive(Debug, Clone)] pub struct StrEnumValidator; -impl_py_gc_traverse!(EnumValidator<StrEnumValidator> { class, missing });+impl_py_gc_traverse!(EnumValidator<StrEnumValidator> { class, lookup, missing }); impl EnumValidateValue for StrEnumValidator { fn validate_value<'py, I: Input<'py> + ?Sized>(@@ -232,7 +232,7 @@ impl EnumValidateValue for StrEnumValidator { #[derive(Debug, Clone)] pub struct FloatEnumValidator; -impl_py_gc_traverse!(EnumValidator<FloatEnumValidator> { class, missing });+impl_py_gc_traverse!(EnumValidator<FloatEnumValidator> { class, lookup, missing }); impl EnumValidateValue for FloatEnumValidator { fn validate_value<'py, I: Input<'py> + ?Sized>(pydantic-core/src/validators/literal.rsindex c9195a78..681137fc 100644@@ -230,6 +230,12 @@ impl<T: Debug> LiteralLookup<T> { impl<T: PyGcTraverse + Debug> PyGcTraverse for LiteralLookup<T> { fn py_gc_traverse(&self, visit: &PyVisit<'_>) -> Result<(), PyTraverseError> { self.expected_py_dict.py_gc_traverse(visit)?;+ if let Some(expected_py_values) = &self.expected_py_values {+ for (value, _) in expected_py_values {+ value.py_gc_traverse(visit)?;+ }+ }+ self.expected_py_primitives.py_gc_traverse(visit)?; self.values.py_gc_traverse(visit)?; Ok(()) }pydantic-core/src/validators/model_fields.rsindex 5e8fe034..d9078f13 100644@@ -126,7 +126,8 @@ impl BuildValidator for ModelFieldsValidator { impl_py_gc_traverse!(ModelFieldsValidator { fields,- extras_validator+ extras_validator,+ extras_keys_validator }); impl Validator for ModelFieldsValidator {pydantic-core/src/validators/union.rsindex b21e54e7..265a1a99 100644@@ -213,6 +213,7 @@ impl UnionValidator { impl PyGcTraverse for UnionValidator { fn py_gc_traverse(&self, visit: &PyVisit<'_>) -> Result<(), PyTraverseError> { self.choices.iter().try_for_each(|(v, _)| v.py_gc_traverse(visit))?;+ self.custom_error.py_gc_traverse(visit)?; Ok(()) } }@@ -351,7 +352,7 @@ impl BuildValidator for TaggedUnionValidator { } } -impl_py_gc_traverse!(TaggedUnionValidator { discriminator, lookup });+impl_py_gc_traverse!(TaggedUnionValidator { discriminator, lookup, custom_error }); impl Validator for TaggedUnionValidator { fn validate<'py>(pydantic-core/tests/test_garbage_collection.pyindex e53cdc56..749e4151 100644@@ -1,6 +1,7 @@ import platform import sys-from collections.abc import Iterable+from collections.abc import Callable, Iterable+from enum import Enum from typing import Any from weakref import WeakValueDictionary @@ -114,3 +115,217 @@ def test_gc_validator_iterator() -> None: del iterable assert_gc(lambda: len(cache) == 0)+++def _make_enum(owner: type, base: type) -> type[Enum]:+ values = {int: 1, str: 'a', float: 1.5, object: object()}++ bases = (Enum,) if base is object else (base, Enum)++ class E(*bases):+ A = values[base]++ def owner(self) -> type:+ # closes over `owner`, creating a cycle `owner -> schema -> E -> owner`+ return owner++ return E+++def _assert_owners_collected(build: Callable[[type], Any]) -> None:+ """Build a schema validator / serializer holding a reference back to its owner class+ and check the owner is collected once it goes out of scope."""+ cache: WeakValueDictionary[int, Any] = WeakValueDictionary()++ for _ in range(100):++ class Owner:+ pass++ Owner.__schema__ = build(Owner)+ cache[id(Owner)] = Owner++ del Owner++ assert_gc(lambda: len(cache) == 0)+++_gc_marks = [+ pytest.mark.xfail(is_free_threaded and sys.version_info < (3, 14), reason='GC leaks on free-threaded (<3.14)'),+ pytest.mark.xfail(+ condition=platform.python_implementation() == 'PyPy',+ reason='https://foss.heptapod.net/pypy/pypy/-/issues/3899',+ ),+ pytest.mark.skipif(platform.python_implementation() == 'GraalVM', reason='Cannot reliably trigger GC on GraalPy'),+]+++def _apply_gc_marks(func: Callable[..., Any]) -> Callable[..., Any]:+ for mark in _gc_marks:+ func = mark(func)+ return func+++@_apply_gc_marks+@pytest.mark.parametrize(+ 'base,sub_type',+ [(object, None), (int, 'int'), (str, 'str'), (float, 'float')],+ ids=['plain', 'int', 'str', 'float'],+)+def test_gc_enum_validator(base: type, sub_type: Any) -> None:+ def build(owner: type) -> SchemaValidator:+ enum_cls = _make_enum(owner, base)+ members = list(enum_cls.__members__.values())+ return SchemaValidator(core_schema.enum_schema(enum_cls, members, sub_type=sub_type))++ _assert_owners_collected(build)+++@_apply_gc_marks+def test_gc_enum_serializer() -> None:+ def build(owner: type) -> SchemaSerializer:+ enum_cls = _make_enum(owner, object)+ return SchemaSerializer(core_schema.enum_schema(enum_cls, list(enum_cls.__members__.values())))++ _assert_owners_collected(build)+++@_apply_gc_marks+def test_gc_literal_unhashable_values() -> None:+ def build(owner: type) -> SchemaValidator:+ return SchemaValidator(core_schema.literal_schema([[owner]]))++ _assert_owners_collected(build)+++@_apply_gc_marks+def test_gc_field_serialization_exclude_if() -> None:+ def build(owner: type) -> SchemaSerializer:+ return SchemaSerializer(+ core_schema.typed_dict_schema(+ {+ 'x': core_schema.typed_dict_field(+ core_schema.int_schema(), serialization_exclude_if=lambda v: v is owner+ )+ }+ )+ )++ _assert_owners_collected(build)+++@_apply_gc_marks+def test_gc_computed_field_serialization_exclude_if() -> None:+ def build(owner: type) -> SchemaSerializer:+ return SchemaSerializer(+ core_schema.typed_dict_schema(+ {},+ computed_fields=[+ core_schema.computed_field(+ 'x', core_schema.int_schema(), serialization_exclude_if=lambda v: v is owner+ )+ ],+ )+ )++ _assert_owners_collected(build)+++def _owner_function_schema(owner: type) -> core_schema.CoreSchema:+ return core_schema.no_info_plain_validator_function(+ lambda v: (owner, v)[1],+ serialization=core_schema.plain_serializer_function_ser_schema(lambda v: (owner, v)[1]),+ )+++@_apply_gc_marks+def test_gc_extras_serializer() -> None:+ def build(owner: type) -> SchemaSerializer:+ return SchemaSerializer(+ core_schema.typed_dict_schema({}, extra_behavior='allow', extras_schema=_owner_function_schema(owner))+ )++ _assert_owners_collected(build)+++@_apply_gc_marks+def test_gc_model_fields_extras_keys_validator() -> None:+ def build(owner: type) -> SchemaValidator:+ return SchemaValidator(+ core_schema.model_fields_schema(+ {}, extra_behavior='allow', extras_keys_schema=_owner_function_schema(owner)+ )+ )++ _assert_owners_collected(build)+++@_apply_gc_marks+def test_gc_dataclass_args_extras_validator() -> None:+ def build(owner: type) -> SchemaValidator:+ schema = core_schema.dataclass_args_schema('Foo', [], extra_behavior='allow')+ schema['extras_schema'] = _owner_function_schema(owner)+ return SchemaValidator(schema)++ _assert_owners_collected(build)+++def _owner_context(owner: type) -> dict[str, str]:+ class OwnerStr(str):+ pass++ value = OwnerStr('owner')+ # holds `owner`, creating a cycle `owner -> schema -> context -> value -> owner`+ value.owner = owner # type: ignore[attr-defined]+ return {'owner': value}+++@_apply_gc_marks+@pytest.mark.parametrize(+ 'custom_error_type,custom_error_message',+ [('my_error', 'my message'), ('int_parsing', None)],+ ids=['custom', 'known'],+)+def test_gc_custom_error_context(custom_error_type: str, custom_error_message: Any) -> None:+ def build(owner: type) -> SchemaValidator:+ return SchemaValidator(+ core_schema.custom_error_schema(+ core_schema.int_schema(),+ custom_error_type=custom_error_type,+ custom_error_message=custom_error_message,+ custom_error_context=_owner_context(owner),+ )+ )++ _assert_owners_collected(build)+++@_apply_gc_marks+def test_gc_union_custom_error_context() -> None:+ def build(owner: type) -> SchemaValidator:+ return SchemaValidator(+ core_schema.union_schema(+ [core_schema.int_schema(), core_schema.str_schema()],+ custom_error_type='my_error',+ custom_error_message='my message',+ custom_error_context=_owner_context(owner),+ )+ )++ _assert_owners_collected(build)+++@_apply_gc_marks+def test_gc_tagged_union_custom_error_context() -> None:+ def build(owner: type) -> SchemaValidator:+ return SchemaValidator(+ core_schema.tagged_union_schema(+ {'a': core_schema.typed_dict_schema({'kind': core_schema.typed_dict_field(core_schema.str_schema())})},+ discriminator='kind',+ custom_error_type='my_error',+ custom_error_message='my message',+ custom_error_context=_owner_context(owner),+ )+ )++ _assert_owners_collected(build)tests/test_garbage_collection.pynew file mode 100644index 00000000..1e0051cb@@ -0,0 +1,140 @@+"""Regression tests ensuring transient models are collectable by the garbage collector.++See https://github.com/pydantic/pydantic/issues/13621: pydantic-core validators and serializers+must report every Python object they hold to the GC, otherwise reference cycles going through them+(e.g. model class -> validator -> enum class -> method -> module globals -> model class) can never+be collected.+"""++import gc+import platform+import sys+import types+import weakref+from time import sleep, time++import pytest++pytestmark = [+ pytest.mark.skipif(platform.python_implementation() == 'PyPy', reason='PyPy does not play nice with PyO3 gc'),+ pytest.mark.skipif(platform.python_implementation() == 'GraalVM', reason='Cannot reliably trigger GC on GraalPy'),+ pytest.mark.xfail(+ not getattr(sys, '_is_gil_enabled', lambda: True)() and sys.version_info < (3, 14),+ reason='GC leaks on free-threaded (<3.14)',+ ),+]+++def _transient_module_refs(source: str, class_names: tuple[str, ...]) -> tuple[weakref.ref, ...]:+ name = '_pydantic_gc_regression_module'+ module = types.ModuleType(name)+ sys.modules[name] = module+ try:+ exec(compile(source, f'<{name}>', 'exec'), module.__dict__)+ refs = tuple(weakref.ref(module.__dict__[class_name]) for class_name in class_names)+ finally:+ del sys.modules[name]+ del module+ return refs+++def _assert_collected(refs: tuple[weakref.ref, ...], timeout: float = 10) -> None:+ start = time()+ while time() - start < timeout:+ gc.collect()+ if all(ref() is None for ref in refs):+ return+ sleep(0.1)+ alive = [ref() for ref in refs if ref() is not None]+ raise AssertionError(f'Objects were not garbage collected: {alive}')+++ENUM_WITH_METHOD = """+from enum import Enum++class Membership(str, Enum):+ FULL = 'full'++ def label(self) -> str:+ return self.value+"""+++def test_model_with_enum_with_method_is_collected() -> None:+ refs = _transient_module_refs(+ ENUM_WITH_METHOD+ + """+from pydantic import BaseModel++class Model(BaseModel):+ membership: Membership++assert Model(membership='full').model_dump() == {'membership': Membership.FULL}+""",+ ('Membership', 'Model'),+ )+ _assert_collected(refs)+++@pytest.mark.parametrize('base', ['int', 'float', ''])+def test_model_with_other_enum_kinds_is_collected(base: str) -> None:+ bases = f'{base}, Enum' if base else 'Enum'+ value = {'int': '1', 'float': '1.5', '': 'object()'}[base]+ refs = _transient_module_refs(+ f"""+from enum import Enum+from pydantic import BaseModel++class E({bases}):+ A = {value}++ def method(self):+ return self++class Model(BaseModel):+ e: E++Model(e=E.A).model_dump()+""",+ ('E', 'Model'),+ )+ _assert_collected(refs)+++def test_model_with_field_exclude_if_is_collected() -> None:+ refs = _transient_module_refs(+ """+from pydantic import BaseModel, Field++class Model(BaseModel):+ value: int = Field(exclude_if=lambda value: False)++assert Model(value=1).model_dump() == {'value': 1}+""",+ ('Model',),+ )+ _assert_collected(refs)+++@pytest.mark.parametrize(+ 'extra_annotation,expected',+ [+ ('dict[str, Membership]', {'full': 'full'}),+ ('dict[Membership, str]', {'full': 'full'}),+ ],+)+def test_model_with_typed_extra_is_collected(extra_annotation: str, expected: dict[str, str]) -> None:+ refs = _transient_module_refs(+ ENUM_WITH_METHOD+ + f"""+from pydantic import BaseModel, ConfigDict++class Model(BaseModel):+ model_config = ConfigDict(extra='allow')+ __pydantic_extra__: {extra_annotation}++assert Model(full='full').model_dump() == {expected!r}+""",+ ('Membership', 'Model'),+ )+ _assert_collected(refs)
pydantic-core/src/serializers/fields.rsindex ba18ee4f3..8631d0b09 100644@@ -35,7 +35,10 @@ pub(super) struct SerField { pub serialization_exclude_if: Option<Py<PyAny>>, } -impl_py_gc_traverse!(SerField { serializer });+impl_py_gc_traverse!(SerField {+ serializer,+ serialization_exclude_if+}); impl SerField { pub fn new(pydantic-core/src/serializers/type_serializers/enum_.rsindex 0d2819ac2..b4e76133c 100644@@ -23,6 +23,8 @@ pub struct EnumSerializer { serializer: Option<Arc<CombinedSerializer>>, } +impl_py_gc_traverse!(EnumSerializer { class, serializer });+ impl BuildSerializer for EnumSerializer { const EXPECTED_TYPE: &'static str = "enum"; @@ -48,8 +50,6 @@ impl BuildSerializer for EnumSerializer { } } -impl_py_gc_traverse!(EnumSerializer { serializer });- impl TypeSerializer for EnumSerializer { fn to_python<'py>(&self, value: &Bound<'py, PyAny>, state: &mut SerializationState<'py>) -> PyResult<Py<PyAny>> { let py = value.py();pydantic-core/src/validators/enum_.rsindex 6ef685daf..28a32a874 100644@@ -168,7 +168,7 @@ impl<T: EnumValidateValue> Validator for EnumValidator<T> { #[derive(Debug, Clone)] pub struct PlainEnumValidator; -impl_py_gc_traverse!(EnumValidator<PlainEnumValidator> { class, missing });+impl_py_gc_traverse!(EnumValidator<PlainEnumValidator> { class, lookup, missing }); impl EnumValidateValue for PlainEnumValidator { fn validate_value<'py, I: Input<'py> + ?Sized>(@@ -200,7 +200,7 @@ impl EnumValidateValue for PlainEnumValidator { #[derive(Debug, Clone)] pub struct IntEnumValidator; -impl_py_gc_traverse!(EnumValidator<IntEnumValidator> { class, missing });+impl_py_gc_traverse!(EnumValidator<IntEnumValidator> { class, lookup, missing }); impl EnumValidateValue for IntEnumValidator { fn validate_value<'py, I: Input<'py> + ?Sized>(@@ -216,7 +216,7 @@ impl EnumValidateValue for IntEnumValidator { #[derive(Debug, Clone)] pub struct StrEnumValidator; -impl_py_gc_traverse!(EnumValidator<StrEnumValidator> { class, missing });+impl_py_gc_traverse!(EnumValidator<StrEnumValidator> { class, lookup, missing }); impl EnumValidateValue for StrEnumValidator { fn validate_value<'py, I: Input<'py> + ?Sized>(@@ -232,7 +232,7 @@ impl EnumValidateValue for StrEnumValidator { #[derive(Debug, Clone)] pub struct FloatEnumValidator; -impl_py_gc_traverse!(EnumValidator<FloatEnumValidator> { class, missing });+impl_py_gc_traverse!(EnumValidator<FloatEnumValidator> { class, lookup, missing }); impl EnumValidateValue for FloatEnumValidator { fn validate_value<'py, I: Input<'py> + ?Sized>(pydantic-core/tests/test_garbage_collection.pyindex e53cdc564..7ac338f77 100644@@ -1,6 +1,6 @@ import platform-import sys from collections.abc import Iterable+from enum import Enum from typing import Any from weakref import WeakValueDictionary @@ -8,7 +8,7 @@ import pytest from pydantic_core import SchemaSerializer, SchemaValidator, core_schema -from .conftest import assert_gc, is_free_threaded+from .conftest import assert_gc GC_TEST_SCHEMA_INNER = core_schema.definitions_schema( core_schema.definition_reference_schema(schema_ref='model'),@@ -21,18 +21,18 @@ GC_TEST_SCHEMA_INNER = core_schema.definitions_schema( ) -@pytest.mark.xfail(is_free_threaded and sys.version_info < (3, 14), reason='GC leaks on free-threaded (<3.14)') @pytest.mark.xfail(- condition=platform.python_implementation() == 'PyPy', reason='https://foss.heptapod.net/pypy/pypy/-/issues/3899'+ condition=platform.python_implementation() == 'PyPy', reason='https://github.com/pypy/pypy/issues/3898' ) @pytest.mark.skipif(platform.python_implementation() == 'GraalVM', reason='Cannot reliably trigger GC on GraalPy') def test_gc_schema_serializer() -> None:- # test for https://github.com/pydantic/pydantic/issues/5136+ """https://github.com/pydantic/pydantic/issues/5136"""+ class BaseModel:- __schema__: SchemaSerializer+ __pydantic_core_schema__: SchemaSerializer def __init_subclass__(cls) -> None:- cls.__schema__ = SchemaSerializer(+ cls.__pydantic_core_schema__ = SchemaSerializer( core_schema.model_schema(cls, GC_TEST_SCHEMA_INNER), config={'ser_json_timedelta': 'float'} ) @@ -50,18 +50,18 @@ def test_gc_schema_serializer() -> None: assert_gc(lambda: len(cache) == 0) -@pytest.mark.xfail(is_free_threaded and sys.version_info < (3, 14), reason='GC leaks on free-threaded (<3.14)') @pytest.mark.xfail(- condition=platform.python_implementation() == 'PyPy', reason='https://foss.heptapod.net/pypy/pypy/-/issues/3899'+ condition=platform.python_implementation() == 'PyPy', reason='https://github.com/pypy/pypy/issues/3898' ) @pytest.mark.skipif(platform.python_implementation() == 'GraalVM', reason='Cannot reliably trigger GC on GraalPy') def test_gc_schema_validator() -> None:- # test for https://github.com/pydantic/pydantic/issues/5136+ """https://github.com/pydantic/pydantic/issues/5136"""+ class BaseModel:- __validator__: SchemaValidator+ __pydantic_validator__: SchemaValidator def __init_subclass__(cls) -> None:- cls.__validator__ = SchemaValidator(+ cls.__pydantic_validator__ = SchemaValidator( schema=core_schema.model_schema(cls, GC_TEST_SCHEMA_INNER), config=core_schema.CoreConfig(extra_fields_behavior='allow'), )@@ -81,11 +81,12 @@ def test_gc_schema_validator() -> None: @pytest.mark.xfail(- condition=platform.python_implementation() == 'PyPy', reason='https://foss.heptapod.net/pypy/pypy/-/issues/3899'+ condition=platform.python_implementation() == 'PyPy', reason='https://github.com/pypy/pypy/issues/3898' ) @pytest.mark.skipif(platform.python_implementation() == 'GraalVM', reason='Cannot reliably trigger GC on GraalPy') def test_gc_validator_iterator() -> None:- # test for https://github.com/pydantic/pydantic/issues/9243+ """https://github.com/pydantic/pydantic/issues/9243"""+ class MyModel: iter: Iterable[int] @@ -114,3 +115,96 @@ def test_gc_validator_iterator() -> None: del iterable assert_gc(lambda: len(cache) == 0)+++@pytest.mark.xfail(+ condition=platform.python_implementation() == 'PyPy', reason='https://github.com/pypy/pypy/issues/3898'+)+@pytest.mark.skipif(platform.python_implementation() == 'GraalVM', reason='Cannot reliably trigger GC on GraalPy')+def test_gc_enum_serializer_class() -> None:+ """https://github.com/pydantic/pydantic/issues/13621"""++ class EnumBase:+ __pydantic_serializer__: SchemaSerializer++ cache: WeakValueDictionary[int, Any] = WeakValueDictionary()++ for _ in range(10_000):++ class MyEnum(EnumBase, str, Enum):+ FULL = 'full'++ MyEnum.__pydantic_serializer__ = SchemaSerializer(core_schema.enum_schema(MyEnum, list(MyEnum), sub_type='str'))++ cache[id(MyEnum)] = MyEnum++ del MyEnum++ assert_gc(lambda: len(cache) == 0)+++@pytest.mark.xfail(+ condition=platform.python_implementation() == 'PyPy', reason='https://github.com/pypy/pypy/issues/3898'+)+@pytest.mark.skipif(platform.python_implementation() == 'GraalVM', reason='Cannot reliably trigger GC on GraalPy')+def test_gc_enum_validator_lookup() -> None:+ """https://github.com/pydantic/pydantic/issues/13621"""++ class EnumBase:+ __pydantic_validator__: SchemaValidator++ cache: WeakValueDictionary[int, Any] = WeakValueDictionary()++ for _ in range(10_000):++ class MyEnum(EnumBase, str, Enum):+ FULL = 'full'++ MyEnum.__pydantic_validator__ = SchemaValidator(core_schema.enum_schema(MyEnum, list(MyEnum), sub_type='str'))++ cache[id(MyEnum)] = MyEnum++ del MyEnum++ assert_gc(lambda: len(cache) == 0)+++@pytest.mark.xfail(+ condition=platform.python_implementation() == 'PyPy', reason='https://github.com/pypy/pypy/issues/3898'+)+@pytest.mark.skipif(platform.python_implementation() == 'GraalVM', reason='Cannot reliably trigger GC on GraalPy')+def test_gc_field_exclude_if() -> None:+ """https://github.com/pydantic/pydantic/issues/13621"""++ class Dummy:+ pass++ class BaseModel:+ __pydantic_serializer__: SchemaSerializer++ def __init_subclass__(cls) -> None:+ cls.__pydantic_serializer__ = SchemaSerializer(+ core_schema.model_schema(+ Dummy,+ core_schema.model_fields_schema(+ {+ 'value': core_schema.model_field(+ core_schema.int_schema(), serialization_exclude_if=lambda value, _cls=cls: False+ )+ }+ ),+ )+ )++ cache: WeakValueDictionary[int, Any] = WeakValueDictionary()++ for _ in range(10_000):++ class MyModel(BaseModel):+ pass++ cache[id(MyModel)] = MyModel++ del MyModel++ assert_gc(lambda: len(cache) == 0)