Backtestspydanticrun-20260929-204949#13376
PrivateAttr(default_factory=...): takes_validated_data_argument re-runs inspect.signature on every instantiation (uncached) → CPU + cyclic-GC memory churn
Our patch was judged better than the merged fix.
In this judgment, Patch A = our agent, Patch B = the merged fix.
Both patches resolve the reported issue for the concrete scenarios (default_factory=list, Lock): A memoizes the pure function with functools.cache (the issue's primary recommended fix), so after the first call per factory both per-instance sites become cache hits and no new cycle garbage accumulates; B precomputes the flag once, caches it on the ModelPrivateAttr instance, and threads it into resolve_default_value so neither site re-inspects. On correctness both hold up for the reported cases; A carries a minor risk (a global, non-evicting @cache retains references to every distinct factory, including per-class lambdas from regular fields, which could grow unboundedly in dynamic-model workloads), while B avoids that by scoping the cache to the instance and mirrors how regular fields already compute the flag once at build time. That architectural cleanliness is B's main edge. However, tests (weighted above scope) favor A: it adds a focused regression test that counts signature inspections across repeated instantiation — the exact symptom the issue describes, failing before and passing after (assuming, as is plausible, takes_validated_data_argument routes through _typing_extra.signature_no_eval that the test monkeypatches). B adds no test for the actual perf/memory regression; its new tests only exercise the refactored resolve_default_value signature and an unrelated __repr_args__/repr feature. That __repr_args__ addition plus the docstring edit are scope creep that changes public repr behavior — extra risk unrelated to the fix. Net: A is the minimal, root-cause fix matching the issue's own suggestion with a genuine before/after regression test and no unrelated churn, edging out B despite B's cleaner caching model.
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
4m 38s9 turns
Wall-clock time in the container
Full test suite
No new failures
12,251 passed · 500 skipped · 12,751 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
27vs 83 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial Checks
- I confirm that I'm using Pydantic V2
Description
For a private attribute with a default_factory, every model instantiation re-derives whether the factory takes the validated data by calling takes_validated_data_argument(default_factory=...), which runs inspect.signature() on the factory (pydantic/_internal/_fields.py:694) — not cached, even though it's a pure function of the factory object. It runs twice per instantiation, from two separate sites in init_private_attributes() (pydantic/_internal/_model_construction.py:377): once via the ModelPrivateAttr.default_factory_takes_validated_data property, and once via get_default() → resolve_default_value() (_fields.py:718). Both are uncached.
Regular Field(default_factory=...) is not affected — it computes this once at schema-build time via FieldInfo.default_factory_takes_validated_data and bakes it into the core validator. Only the PrivateAttr path recomputes per instance.
For a factory whose inspect.signature() succeeds via CPython's inspect._signature_fromstr (list, tuple, set, threading.Lock, …), that call does sys.modules.copy() and builds closures that capture the copy — reference cycles the plain refcounter can't free, only the cyclic GC can. Instantiating such a model at a high rate in a long-running process accumulates this garbage faster than the collector reclaims it (and pymalloc keeps the freed arenas), so RSS climbs steadily.
Concretely: Sculptor has a BaseModel with _wait_lock: Lock = PrivateAttr(default_factory=Lock) that is instantiated constantly. It was harmless on Python ≤3.12 — there inspect.signature(Lock) raises ValueError: no signature found, so pydantic falls back cheaply — but began leaking multi-GB of RSS on 3.13+, because CPython 3.13 gave threading.Lock an inferrable signature and flipped every instantiation onto the _signature_fromstr path. We mitigated it by memoizing this check: imbue-ai/sculptor#205.
With default_factory=list it reproduces on every CPython tested (3.12.11, 3.13.5, 3.14.6) and on pydantic 2.13.4 through main (2.14.0a1) — ~52k objects of cycle-only garbage per 1000 instantiations. list/tuple have always had signatures, so the bug isn't tied to a Python version; the set of other builtins that trigger it just grows over time (set, threading.Lock at 3.13).
Example Code
import gc
from pydantic import BaseModel, PrivateAttr
from pydantic._internal import _fields
# (1) It's recomputed on every instantiation, uncached:
calls = 0
_orig = _fields.takes_validated_data_argument
def _counting(*args, **kwargs):
global calls
calls += 1
return _orig(*args, **kwargs)
_fields.takes_validated_data_argument = _counting
class M(BaseModel):
_items: list = PrivateAttr(default_factory=list)
M() # warm
calls = 0
for _ in range(1000):
M()
print("takes_validated_data_argument calls:", calls) # -> 2000
# (2) Each call leaves cycle-only garbage (default_factory=list):
gc.collect(); gc.disable()
before = len(gc.get_objects())
for _ in range(1000):
M()
after = len(gc.get_objects())
freed = gc.collect(); gc.enable()
print("objects grew:", after - before, "| freed only by cyclic gc:", freed)
# -> objects grew: ~54000 | freed only by cyclic gc: ~52000
Suggested fix
The simplest fix is to memoize takes_validated_data_argument (a pure function of the factory, e.g. functools.cache) — after the first call per factory, both per-instance calls become cache hits.
Alternatively, do what regular fields do and compute the flag once at class-construction time, storing it so both sites can read it. Note this is more than "reuse the property": ModelPrivateAttr.default_factory_takes_validated_data (pydantic/fields.py:1451) is itself an uncached @property that re-runs inspect.signature on every access — it's one of the two per-instance calls, so it has to be precomputed too, not just read from resolve_default_value.
Happy to open a PR if you'd like.
Python, Pydantic & OS Version
pydantic version: 2.13.4
pydantic-core version: 2.46.4
pydantic-core build: profile=release pgo=false
python version: 3.14.6 (main, Jun 11 2026, 03:55:33) [Clang 22.1.3 ]
platform: macOS-26.5.1-arm64-arm-64bit-Mach-O
related packages: typing_extensions-4.15.0 fastapi-0.137.2 pydantic-settings-2.14.2
commit: unknown
Also reproduced on main (pydantic 2.14.0a1 / pydantic-core 2.47.0) and on CPython 3.12.11 / 3.13.5.
Repro verified by hand by the maintainer of the affected service.
(prepared & edited with heavy use of Claude)
The two patches
pydantic/_internal/_fields.pyindex 9c2483fc..9a2b3d10 100644@@ -696,6 +696,7 @@ def is_valid_privateattr_name(name: str) -> bool: return name.startswith('_') and not name.startswith('__') +@cache def takes_validated_data_argument( default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any], ) -> TypeIs[Callable[[dict[str, Any]], Any]]:tests/test_private_attributes.pyindex cefe4c6d..cd973a1f 100644@@ -130,6 +130,32 @@ def test_private_attribute_factory_unset_private(): Model(foo='foo', bar='bar') +def test_private_attribute_factory_signature_inspection_not_repeated(monkeypatch):+ from pydantic._internal import _typing_extra++ calls = 0+ orig_signature = _typing_extra.signature_no_eval++ def counting_signature(obj):+ nonlocal calls+ calls += 1+ return orig_signature(obj)++ monkeypatch.setattr(_typing_extra, 'signature_no_eval', counting_signature)++ class Model(BaseModel):+ _items: list = PrivateAttr(default_factory=list)++ Model() # warm any per-factory caching+ calls_after_warm = calls++ for _ in range(100):+ Model()++ # After warming, repeated instantiation must not re-inspect the factory signature.+ assert calls == calls_after_warm++ def test_private_attribute_annotation(): class Model(BaseModel): """The best model"""
pydantic/_internal/_fields.pyindex 9c2483fce..59c7f8d79 100644@@ -713,9 +713,10 @@ def takes_validated_data_argument( def resolve_default_value(+ *, default: Any, default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any] | None,- *,+ default_factory_takes_validated_data_argument: bool | None, validated_data: dict[str, Any] | None = None, call_default_factory: bool = False, ) -> Any:@@ -725,7 +726,7 @@ def resolve_default_value( if default_factory is None: return smart_deepcopy(default) if call_default_factory:- if takes_validated_data_argument(default_factory=default_factory):+ if default_factory_takes_validated_data_argument: fac = cast('Callable[[dict[str, Any]], Any]', default_factory) if validated_data is None: raise ValueError(pydantic/fields.pyindex cd74d7926..b54e5d052 100644@@ -740,6 +740,7 @@ class FieldInfo(_repr.Representation): return _fields.resolve_default_value( default=self.default, default_factory=self.default_factory,+ default_factory_takes_validated_data_argument=self.default_factory_takes_validated_data, validated_data=validated_data, call_default_factory=call_default_factory, )@@ -1404,7 +1405,7 @@ class ModelPrivateAttr(_repr.Representation): !!! warning You generally shouldn't be creating `ModelPrivateAttr` instances directly, instead use- `pydantic.fields.PrivateAttr`. (This is similar to `FieldInfo` vs. `Field`.)+ the [`PrivateAttr()`][pydantic.fields.PrivateAttr] function. Attributes: default: The default value of the attribute if not provided.@@ -1413,7 +1414,7 @@ class ModelPrivateAttr(_repr.Representation): [`__dict__`][object.__dict__]) and the already initialized private attributes. """ - __slots__ = ('default', 'default_factory')+ __slots__ = ('default', 'default_factory', '_default_factory_takes_validated_data') def __init__( self,@@ -1426,6 +1427,7 @@ class ModelPrivateAttr(_repr.Representation): else: self.default = default self.default_factory = default_factory+ self._default_factory_takes_validated_data: bool | None = _Unset if not TYPE_CHECKING: # We put `__getattr__` in a non-TYPE_CHECKING block because otherwise, mypy allows arbitrary attribute access@@ -1454,8 +1456,15 @@ class ModelPrivateAttr(_repr.Representation): Returns `None` if no default factory is set. """+ if self._default_factory_takes_validated_data is not _Unset:+ return self._default_factory_takes_validated_data++ value: bool | None = None if self.default_factory is not None:- return _fields.takes_validated_data_argument(self.default_factory)+ value = _fields.takes_validated_data_argument(self.default_factory)++ self._default_factory_takes_validated_data = value+ return value @overload def get_default(@@ -1482,6 +1491,7 @@ class ModelPrivateAttr(_repr.Representation): return _fields.resolve_default_value( default=self.default, default_factory=self.default_factory,+ default_factory_takes_validated_data_argument=self.default_factory_takes_validated_data, validated_data=validated_data, call_default_factory=call_default_factory, )@@ -1492,6 +1502,12 @@ class ModelPrivateAttr(_repr.Representation): other.default_factory, ) + def __repr_args__(self) -> ReprArgs:+ if self.default is not PydanticUndefined:+ yield 'default', self.default+ if self.default_factory is not None:+ yield 'default_factory', self.default_factory+ # NOTE: Actual return type is 'ModelPrivateAttr', but we want to help type checkers # to understand the magic that happens at runtime.tests/test_fields.pyindex 104a0f700..372584d7a 100644@@ -12,6 +12,7 @@ from pydantic import ( BaseModel, ConfigDict, Field,+ PrivateAttr, PydanticUserError, RootModel, TypeAdapter,@@ -20,7 +21,7 @@ from pydantic import ( create_model, validate_call, )-from pydantic.fields import FieldInfo+from pydantic.fields import FieldInfo, ModelPrivateAttr from pydantic.warnings import UnsupportedFieldAttributeWarning @@ -459,3 +460,15 @@ def test_default_factory_without_validated_data_unsupported() -> None: with pytest.raises(ValueError): FooBar.model_fields['a'].get_default(call_default_factory=True)+++@pytest.mark.parametrize(+ ['private_attr', 'result'],+ [+ (PrivateAttr(), 'ModelPrivateAttr()'),+ (PrivateAttr(default=1), 'ModelPrivateAttr(default=1)'),+ (PrivateAttr(default_factory=int), "ModelPrivateAttr(default_factory=<class 'int'>)"),+ ],+)+def test_privateattr_repr(private_attr: ModelPrivateAttr, result: str) -> None:+ assert repr(private_attr) == resulttests/test_internal.pyindex b94c05b42..acd0b46e7 100644@@ -244,19 +244,34 @@ def test_pydantic_js_functions(): @pytest.mark.parametrize(- 'default, default_factory, validated_data, call_default_factory, expected', [- ('foo', None, None, True, 'foo'),- ('foo', None, None, False, 'foo'),- ('foo-unused', lambda: 'foo', None, False, PydanticUndefined),- ('foo-unused', lambda: 'foo', None, True, 'foo'),- ('foo-unused', lambda data: data['foo'], {'foo': 'bar'}, True, 'bar'),+ 'default',+ 'default_factory',+ 'default_factory_takes_validated_data_argument',+ 'validated_data',+ 'call_default_factory',+ 'expected',+ ],+ [+ ('foo', None, False, None, True, 'foo'),+ ('foo', None, False, None, False, 'foo'),+ ('foo-unused', lambda: 'foo', False, None, False, PydanticUndefined),+ ('foo-unused', lambda: 'foo', False, None, True, 'foo'),+ ('foo-unused', lambda data: data['foo'], True, {'foo': 'bar'}, True, 'bar'), ], )-def test_resolve_default_value(default, default_factory, validated_data, call_default_factory, expected):+def test_resolve_default_value(+ default,+ default_factory,+ default_factory_takes_validated_data_argument,+ validated_data,+ call_default_factory,+ expected,+): result = resolve_default_value(- default,- default_factory,+ default=default,+ default_factory=default_factory,+ default_factory_takes_validated_data_argument=default_factory_takes_validated_data_argument, validated_data=validated_data, call_default_factory=call_default_factory, )@@ -266,4 +281,10 @@ def test_resolve_default_value(default, default_factory, validated_data, call_de def test_resolve_default_value_missing_validated_data(): # When factory requires validated_data but none is provided, a ValueError should be raised. with pytest.raises(ValueError):- resolve_default_value('foo', lambda data: data['foo'], validated_data=None, call_default_factory=True)+ resolve_default_value(+ default='foo',+ default_factory=lambda data: data['foo'],+ default_factory_takes_validated_data_argument=True,+ validated_data=None,+ call_default_factory=True,+ )
What the agent reported
The fix is complete and verified. Here is my final report.
What was wrong
For a private attribute declared with PrivateAttr(default_factory=...), every model instantiation re-derived whether the factory takes validated data by calling takes_validated_data_argument() in pydantic/_internal/_fields.py, which runs inspect.signature (via _typing_extra.signature_no_eval) on the factory. This was uncached and ran twice per instantiation — once through the ModelPrivateAttr.default_factory_takes_validated_data property and once through resolve_default_value(). On CPython 3.13+, factories like threading.Lock/list take the inspect._signature_fromstr path, which builds reference cycles (sys.modules.copy() + closures) that only the cyclic GC can reclaim — so high-rate instantiation leaked multi-GB RSS. Regular Field(default_factory=...) was unaffected because it bakes the flag in once at schema-build time.
What I changed
pydantic/_internal/_fields.py: added@cachetotakes_validated_data_argument(thecachesymbol was already imported fromfunctools). It's a pure function of the factory object, so after the first call per factory both per-instance call sites become cache hits — eliminating the per-instantiationinspect.signaturecalls and the resulting CPU + cyclic-GC churn.tests/test_private_attributes.py: addedtest_private_attribute_factory_signature_inspection_not_repeated, which countssignature_no_evalinvocations and asserts that repeated instantiation adds zero further signature inspections.
Test evidence
- Regression test failed before the fix:
assert 202 == 2(2 signature inspections per instantiation × 100 loops). - After the fix: full gate passed — 104 passed across
tests/test_private_attributes.pyandtests/test_fields.py(the latter covers the sharedtakes_validated_data_argumentschema-build call site for regular fields, so blast radius is checked).
Requirements disposition
- Fix uncached per-instantiation recomputation (both call sites) — ✅ Implemented via
@cache; reviewer confirmed all callers (both properties,resolve_default_value, and the schema-build site) route through the now-cached function. Test proves no per-instantiation re-inspection. - Report's suggested "simplest fix" — memoize with
functools.cache— ✅ This exact approach was used. - Alternative (precompute at class-construction, including the property) — ⏭️ Skipped in favor of the report's endorsed simplest fix; the
@cacheapproach covers the property call site too (it delegates to the cached function), so no separate precomputation is needed.
Note on one theoretical edge
functools.cache raises TypeError on an unhashable argument, so a default_factory that is a callable object whose class defines __eq__ without __hash__ would newly fail. This is exotic (factories are near-universally functions, lambdas, or types, all hashable), and it is the approach the report explicitly endorses, so I kept the minimal change. Flagging it for awareness. The cache also retains references to factory objects for the process lifetime, which is acceptable since factories are typically module-level and long-lived.
All changes are left uncommitted in the working tree.