Backtestspydanticrun-20261002-175417#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 stop the PrivateAttr path from calling `inspect.signature` on every instantiation. Patch A is better on tests and has fewer correctness risks. **Correctness** - Patch A caches the result on both `ModelPrivateAttr` and `FieldInfo`. - The cache is keyed by the identity of the factory, so reassigning `default_factory` gives a fresh result. Its test confirms this. - The cached value is passed to `resolve_default_value` only when `call_default_factory` is true. So `get_default(call_default_factory=False)` still never inspects the signature. - It reads the cache with `getattr(..., None)`, which handles objects that are missing the new slot. - It also fixes the same uncached work in the `model_construct` path for regular fields. - Patch B caches only on `ModelPrivateAttr`, using an `_Unset` sentinel. - The cache goes stale if `default_factory` is reassigned. - `FieldInfo.get_default` now always evaluates the uncached `default_factory_takes_validated_data`, even when `call_default_factory=False`. Before, it inspected the signature only when calling the factory, so this is a small new cost. - It turns `resolve_default_value`'s new argument into a required keyword-only parameter, which changes the call signature. **Tests** - Patch A adds tests that count calls to `takes_validated_data_argument`. They cover private attributes (`list`, a lambda, and a data-taking lambda, through both `Model()` and `model_construct`) and regular fields through `model_construct`. These tests would fail before the fix and pass after it. It also tests factory reassignment and repr. - Patch B adds no test that checks the caching behavior at all. It only adds repr tests and rewrites the existing `resolve_default_value` tests to the new signature. **Scope and craft** - Both patches add `__repr_args__` so the new slot does not leak into the repr. - Patch B also makes an unrelated docstring edit. Overall, Patch A is more correct, covers the issue more completely, and is properly tested.
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
12m 16s
Wall-clock time in the container
Full test suite
No new failures
12,256 passed · 500 skipped · 12,756 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
140vs 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..b0eb603c 100644@@ -718,14 +718,21 @@ 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."""+ """Resolve the default value using either a static default or a default_factory.++ `default_factory_takes_validated_data` can be provided to avoid inspecting the signature+ of the default factory (which is costly) on each call.+ """ from ._utils import smart_deepcopy 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..ba2c2747 100644@@ -204,6 +204,7 @@ class FieldInfo(_repr.Representation): '_original_assignment', '_original_annotation', '_final',+ '_default_factory_takes_data_cache', ) # used to convert kwargs to metadata/constraints,@@ -287,6 +288,8 @@ class FieldInfo(_repr.Representation): # or if it is the result of the `Field()` function being used as metadata in an `Annotated` type/as an assignment # (not an ideal pattern, see https://github.com/pydantic/pydantic/issues/11122): self._final = False+ # Cache for `default_factory_takes_validated_data`, as (default_factory, result):+ self._default_factory_takes_data_cache: tuple[Callable[..., Any], bool] | None = None @staticmethod def from_field(default: Any = PydanticUndefined, **kwargs: Unpack[_FromFieldInfoInputs]) -> FieldInfo:@@ -712,8 +715,18 @@ class FieldInfo(_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+ # Inspecting the signature is costly, and this can be called on each model instantiation+ # (e.g. for private attributes or with `model_construct()`), so the result is cached+ # (keyed by the identity of the default factory, which may be reassigned):+ cache = getattr(self, '_default_factory_takes_data_cache', None)+ if cache is not None and cache[0] is default_factory:+ return cache[1]+ takes_validated_data = _fields.takes_validated_data_argument(default_factory)+ self._default_factory_takes_data_cache = (default_factory, takes_validated_data)+ return takes_validated_data @overload def get_default(@@ -742,6 +755,9 @@ class FieldInfo(_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 else None+ ), ) def is_required(self) -> bool:@@ -848,6 +864,7 @@ class FieldInfo(_repr.Representation): '_original_assignment', '_original_annotation', '_final',+ '_default_factory_takes_data_cache', ): continue elif s == 'metadata' and not self.metadata:@@ -1413,7 +1430,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_data_cache') def __init__( self,@@ -1426,6 +1443,8 @@ class ModelPrivateAttr(_repr.Representation): else: self.default = default self.default_factory = default_factory+ # Cache for `default_factory_takes_validated_data`, as (default_factory, result):+ self._default_factory_takes_data_cache: tuple[Callable[..., 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 +1473,18 @@ 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+ # Inspecting the signature is costly, and this can be called on each model instantiation+ # (e.g. for private attributes or with `model_construct()`), so the result is cached+ # (keyed by the identity of the default factory, which may be reassigned):+ cache = getattr(self, '_default_factory_takes_data_cache', None)+ if cache is not None and cache[0] is default_factory:+ return cache[1]+ takes_validated_data = _fields.takes_validated_data_argument(default_factory)+ self._default_factory_takes_data_cache = (default_factory, takes_validated_data)+ return takes_validated_data @overload def get_default(@@ -1484,8 +1513,14 @@ 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 else None+ ), ) + def __repr_args__(self) -> ReprArgs:+ return [(a, v) for a in ('default', 'default_factory') if (v := getattr(self, a)) is not None]+ def __eq__(self, other: Any) -> bool: return isinstance(other, self.__class__) and (self.default, self.default_factory) == ( other.default,tests/test_main.pyindex c87dae79..87ce33d7 100644@@ -3747,6 +3747,34 @@ def test_validation_works_for_cyclical_forward_refs() -> None: assert Y(x={'y': None}).x.y is None +def test_model_construct_default_factory_signature_not_inspected_on_each_call(monkeypatch: pytest.MonkeyPatch) -> None:+ """https://github.com/pydantic/pydantic/issues/13376"""+ 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)++ class Model(BaseModel):+ a: int = 1+ b: list[int] = Field(default_factory=list)+ c: list[int] = Field(default_factory=lambda data: [data['a']])++ monkeypatch.setattr(_fields, 'takes_validated_data_argument', counting_takes_validated_data_argument)++ for _ in range(10):+ m = Model.model_construct()+ assert m.b == []+ assert m.c == [1]++ # Only once per field with a default factory:+ assert calls == 2++ def test_model_construct_with_model_post_init_and_model_copy() -> None: class Model(BaseModel): id: inttests/test_private_attributes.pyindex cefe4c6d..ba0bd248 100644@@ -6,6 +6,7 @@ import pytest from pydantic_core import PydanticUndefined from pydantic import BaseModel, ConfigDict, PrivateAttr, computed_field+from pydantic._internal import _fields from pydantic.fields import ModelPrivateAttr @@ -130,6 +131,61 @@ def test_private_attribute_factory_unset_private(): Model(foo='foo', bar='bar') +@pytest.mark.parametrize(+ ['factory', 'expected'],+ [+ (list, []),+ (lambda: ['x'], ['x']),+ (lambda data: [data['foo']], [1]),+ ],+)+def test_private_attribute_factory_signature_not_inspected_on_each_instantiation(+ factory, expected, monkeypatch: pytest.MonkeyPatch+) -> None:+ """https://github.com/pydantic/pydantic/issues/13376"""+ 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):+ foo: int = 1+ _items: list = PrivateAttr(default_factory=factory)++ for _ in range(10):+ m = Model()+ assert m._items == expected+ m = Model.model_construct()+ assert m._items == expected++ assert calls == 1+++def test_private_attribute_factory_reassigned() -> None:+ private_attr = PrivateAttr(default_factory=lambda: 1)+ assert private_attr.default_factory_takes_validated_data is False+ assert private_attr.get_default(call_default_factory=True) == 1++ private_attr.default_factory = lambda data: data['a']+ assert private_attr.default_factory_takes_validated_data is True+ assert private_attr.get_default(call_default_factory=True, validated_data={'a': 2}) == 2+++def test_private_attribute_repr() -> None:+ def factory() -> int:+ return 1++ private_attr = PrivateAttr(default_factory=factory)+ private_attr.get_default(call_default_factory=True)+ assert repr(private_attr) == f'ModelPrivateAttr(default=PydanticUndefined, default_factory={factory!r})'+ assert repr(PrivateAttr(1)) == 'ModelPrivateAttr(default=1)'++ 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,+ )