Backtestspydanticrun-20260930-144322#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 store the takes-validated-data flag on ModelPrivateAttr, so after the first call neither re-runs inspect.signature per private-attribute instantiation. Patch A is the better fix. Correctness and risk: - Patch A keys its cache on the factory's identity, so reassigning `default_factory` still gives a correct result. - Patch A adds an optional `default_factory_takes_validated_data=None` keyword to `resolve_default_value` and falls back to computing it when absent. The internal helper stays backward compatible. - Patch A only passes the precomputed flag when it will actually be used. - Patch B makes `resolve_default_value` fully keyword-only and adds a new required argument. Any caller not shown in the diff would break. - Patch B changes `FieldInfo.get_default` to always evaluate `FieldInfo.default_factory_takes_validated_data`, even when `call_default_factory=False`. That property is uncached, so Patch B adds inspect.signature calls to a path that previously had none. - Patch B's sentinel cache never invalidates if `default_factory` is reassigned, and it annotates an `_Unset` sentinel as `bool | None`. Tests: - Patch A adds a targeted regression test. It monkeypatches `takes_validated_data_argument`, asserts zero calls across repeated instantiations for both a plain factory and a data-taking factory, and checks that each instance gets a fresh object. This test would fail before the fix. - Patch B's tests only cover repr output and the new call signature. Nothing verifies the per-instantiation recomputation is gone. Scope: - Both add `__repr_args__` to keep the new slot out of the repr. - Patch B also changes repr semantics (hiding PydanticUndefined), edits a docstring, and rewrites existing tests. That is extra churn.
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
3m 58s10 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
68vs 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..58e27076 100644@@ -718,6 +718,7 @@ def resolve_default_value( *, validated_data: dict[str, Any] | None = None, call_default_factory: bool = False,+ default_factory_takes_validated_data: bool | None = None, ) -> Any: """Resolve the default value using either a static default or a default_factory.""" from ._utils import smart_deepcopy@@ -725,7 +726,9 @@ 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 is None:+ default_factory_takes_validated_data = takes_validated_data_argument(default_factory=default_factory)+ if default_factory_takes_validated_data: fac = cast('Callable[[dict[str, Any]], Any]', default_factory) if validated_data is None: raise ValueError(pydantic/fields.pyindex cd74d792..f92588d2 100644@@ -1413,7 +1413,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_cache') def __init__( self,@@ -1426,6 +1426,8 @@ class ModelPrivateAttr(_repr.Representation): else: self.default = default self.default_factory = default_factory+ # Cache of `(default_factory, takes_validated_data)`, keyed on the factory identity:+ self._default_factory_takes_validated_data_cache: tuple[Any, bool] | None = None 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 is not None:- return _fields.takes_validated_data_argument(self.default_factory)+ default_factory = self.default_factory+ if default_factory is None:+ return None+ cache = getattr(self, '_default_factory_takes_validated_data_cache', None)+ if cache is not None and cache[0] is default_factory:+ return cache[1]+ result = _fields.takes_validated_data_argument(default_factory)+ self._default_factory_takes_validated_data_cache = (default_factory, result)+ return result @overload def get_default(@@ -1484,8 +1493,19 @@ class ModelPrivateAttr(_repr.Representation): default_factory=self.default_factory, validated_data=validated_data, call_default_factory=call_default_factory,+ default_factory_takes_validated_data=(+ self.default_factory_takes_validated_data+ if call_default_factory and self.default_factory is not None+ else None+ ), ) + def __repr_args__(self) -> ReprArgs:+ for name in ('default', 'default_factory'):+ value = getattr(self, name)+ if value is not None:+ yield name, value+ def __eq__(self, other: Any) -> bool: return isinstance(other, self.__class__) and (self.default, self.default_factory) == ( other.default,tests/test_private_attributes.pyindex cefe4c6d..400fae79 100644@@ -632,3 +632,40 @@ def test_private_attribute_not_skipped_during_ns_inspection() -> None: _priv: object = Fullname assert isinstance(Full._priv, ModelPrivateAttr)+++def test_private_attr_default_factory_takes_validated_data_computed_once(monkeypatch: pytest.MonkeyPatch) -> None:+ from pydantic._internal import _fields++ calls = 0+ original = _fields.takes_validated_data_argument++ def counting_takes_validated_data_argument(*args, **kwargs):+ nonlocal calls+ calls += 1+ return original(*args, **kwargs)++ monkeypatch.setattr(_fields, 'takes_validated_data_argument', counting_takes_validated_data_argument)++ class Model(BaseModel):+ _items: list = PrivateAttr(default_factory=list)++ first = Model()+ assert first._items == []++ calls = 0+ instances = [Model() for _ in range(10)]+ assert calls == 0+ assert all(m._items == [] for m in instances)+ assert len({id(m._items) for m in [first, *instances]}) == 11++ class DataModel(BaseModel):+ a: int+ _double: int = PrivateAttr(default_factory=lambda data: data['a'] * 2)++ assert DataModel(a=1)._double == 2++ calls = 0+ data_instances = [DataModel(a=i) for i in range(10)]+ assert calls == 0+ assert [m._double for m in data_instances] == [i * 2 for i in range(10)]
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
I fixed it: PrivateAttr(default_factory=...) now checks the factory's signature once instead of twice on every model instantiation. The new regression test failed before the change and passes after, and the surrounding test files pass.
What was wrong: When a model was created, init_private_attributes called _fields.takes_validated_data_argument twice per private attribute. That function runs inspect.signature, and nothing cached the result. One call came from the ModelPrivateAttr.default_factory_takes_validated_data property. The other came from get_default(), which calls resolve_default_value(). For factories like list and, on Python 3.13+, threading.Lock, each call leaves reference cycles that only the cyclic garbage collector can free.
What I changed:
pydantic/fields.py,ModelPrivateAttr:- There is a new slot,
_default_factory_takes_validated_data_cache, which stores the factory and the result together. - The property computes the result on first access and reuses it after that. The cache is keyed on the factory object itself, so if someone reassigns
default_factory, it is recomputed. get_default()passes the cached value down toresolve_default_valuewhen it actually calls the factory.- I added a small
__repr_args__override. The base class builds the repr from__slots__, so without it the new slot would have shown up in the repr. The output is unchanged. pydantic/_internal/_fields.py,resolve_default_value: it takes a new optional keyword,default_factory_takes_validated_data. If it isn't passed, the function computes the value as before, so other callers such asFieldInfo.get_defaultbehave exactly as they did.
Tests:
- New test:
test_private_attr_default_factory_takes_validated_data_computed_onceintests/test_private_attributes.py. It counts calls totakes_validated_data_argument(by wrapping the function, not mocking it) and checks that the defaults are still correct. - Before the fix it failed with
assert 20 == 0: 10 instantiations made 20 calls. - After the fix it passes. It covers
default_factory=listand a factory that takes the validated data. - Gate:
./bt-testontests/test_private_attributes.py,test_main.py,test_fields.py,test_construction.py,test_pickle.py,test_dataclasses.pyandtest_edge_cases.pygave 893 passed, 28 skipped, 2 xfailed, 0 failed. - Not tested: I didn't re-run the report's garbage-collection measurement (the ~52k objects), because
pydantic_coreisn't installed outside the test lane. The test shows the root cause is gone: after the first instance,inspect.signatureisn't called again. - Review: the reviewer found no blocking issues. One side effect: a
ModelPrivateAttrpickled with this version can't be unpickled by an older pydantic. Old pickles still load in this version.
Scope of the change: only ModelPrivateAttr and an optional new keyword on resolve_default_value. FieldInfo is unchanged, as is ModelPrivateAttr's equality check, and no existing tests were modified.
Requirements:
- Stop the check running on every instantiation: done. The new test shows 0 calls after the first instance.
- Both call sites (the property and
get_default→resolve_default_value): done. Both now use one cached value, and the test counts calls from both. - Suggested fix A,
functools.cacheontakes_validated_data_argument: not used, on purpose. A global cache would hold strong references to every factory forever, including dynamically created lambdas. It would also raiseTypeErrorfor callables that can't be hashed. Caching on theModelPrivateAttravoids both problems. - Suggested fix B, compute once and make the property cached too: done, but on first use rather than when the class is built. The property itself is now cached. It is computed on the first instantiation, which the test allows for by resetting its counter after the first instance.
- Regular
Field(default_factory=...)is not affected: left unchanged, as the report says.FieldInfois untouched andtests/test_fields.pypasses. - The
default_factory=listandthreading.Lockexamples:listis covered directly by the test.Lockgoes through the same code path, so the same caching applies; I didn't write a separate test for it.
The changes are uncommitted in the working tree, with no scratch files left behind.