Backtestspydanticrun-20261003-150343#13376

Issue #13376 Merged fix PR #13390 Run 3 Oct 2026 · run-20261003-150343 pydantic/pydantic

PrivateAttr(default_factory=...): takes_validated_data_argument re-runs inspect.signature on every instantiation (uncached) → CPU + cyclic-GC memory churn

Better

Our patch was judged better than the merged fix.

In this judgment, Patch B = our agent, Patch A = the merged fix.

Both patches stop the per-instantiation `inspect.signature` calls for `PrivateAttr(default_factory=...)`, the root cause the issue reports.

**Patch A** caches the flag on each `ModelPrivateAttr` instance in a new slot. `resolve_default_value` now takes the precomputed flag instead of recomputing it. This matches the issue's alternative suggestion and covers every factory type. Because `__private_attributes__` holds the same objects across instantiations, the cache persists.

Its costs:
- It changes `resolve_default_value`'s signature: it becomes keyword-only and gains a required new parameter. Any other internal callers that pass positional args would break.
- It has to add a custom `__repr_args__` so the new private slot doesn't leak into the repr, which is extra churn.
- Most importantly, it adds no regression test for the reported bug. Its test changes only adapt existing `resolve_default_value` tests to the new signature and add a repr test. Nothing would fail before the fix and pass after it with respect to call counts or garbage.

**Patch B** memoizes `takes_validated_data_argument` itself, which is the issue's primary suggested fix. It uses an id-keyed dict with weakref validation and cleanup callbacks, so entries are not leaked and `id()` reuse cannot return a stale result. Every caller benefits. The reported factories (`list`, `threading.Lock`, plain functions) are all weakref-able types, so they are cached.

The gap is non-weakrefable callables, such as `__slots__` instances and some builtin functions. These fall back to uncached behaviour, which is a correctness-safe degradation that never breaks anything.

Patch B's tests are meaningful:
- They count `signature_no_eval` calls across 100 instantiations, asserting exactly one call per factory. This would fail before the fix, when 200 calls were made.
- They cover the builtin `list` factory.
- They verify the cache entry is released when the factory is garbage-collected.
- They check the non-weakrefable fallback.

Patch B is somewhat more intricate, but it is correct, avoids changing internal signatures, and has much stronger regression coverage. On the tests criterion it clearly wins, and correctness is comparable for the scenarios in the issue.

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

11m 26s

Wall-clock time in the container

Full test suite

No new failures

12,255 passed · 500 skipped · 12,755 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

148vs 83 merged

Changed lines, ours vs merged

Initial 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

Our patch+147 −1
pydantic/_internal/_fields.pyindex 9c2483fc..136ef1f5 100644@@ -4,6 +4,7 @@ from __future__ import annotations as _annotations  import dataclasses import warnings+import weakref from collections.abc import Callable, Mapping from functools import cache from inspect import Parameter, ismethoddescriptor@@ -696,10 +697,44 @@ def is_valid_privateattr_name(name: str) -> bool:     return name.startswith('_') and not name.startswith('__')  +# Maps the `id()` of a default factory to a weak reference to it and the result of+# `_takes_validated_data_argument()`. Entries are removed when the factory is garbage collected.+_takes_validated_data_cache: dict[int, tuple[weakref.ReferenceType[Any], bool]] = {}++ def takes_validated_data_argument(     default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any], ) -> TypeIs[Callable[[dict[str, Any]], Any]]:-    """Whether the provided default factory callable has a validated data parameter."""+    """Whether the provided default factory callable has a validated data parameter.++    The result is cached per factory object, as this can be called on every model instantiation+    (e.g. for private attributes with a default factory) and `inspect.signature()` is expensive.+    """+    key = id(default_factory)+    entry = _takes_validated_data_cache.get(key)+    if entry is not None and entry[0]() is default_factory:+        return entry[1]++    result = _takes_validated_data_argument(default_factory)++    cache = _takes_validated_data_cache++    def remove_entry(ref: weakref.ReferenceType[Any]) -> None:+        # Only remove the entry if it wasn't replaced in the meantime (`id()` values can be reused):+        entry = cache.get(key)+        if entry is not None and entry[0] is ref:+            cache.pop(key, None)++    try:+        ref = weakref.ref(default_factory, remove_entry)+    except TypeError:+        # The factory doesn't support weak references, so we can't safely cache the result:+        return result+    _takes_validated_data_cache[key] = (ref, result)+    return result+++def _takes_validated_data_argument(default_factory: Callable[..., Any]) -> bool:     try:         sig = _typing_extra.signature_no_eval(default_factory)     except (ValueError, TypeError):tests/test_construction.pyindex 1f42457e..0689bde9 100644@@ -669,3 +669,37 @@ def test_model_construct_with_alias_choices_and_path() -> None:     assert MyModel.model_construct(a='a_value').a == 'a_value'     assert MyModel.model_construct(aaa='a_value').a == 'a_value'     assert MyModel.model_construct(AAA={'aaa': 'a_value'}).a == 'a_value'+++def test_construct_default_factory_signature_inspected_once(monkeypatch: pytest.MonkeyPatch) -> None:+    """https://github.com/pydantic/pydantic/issues/13376"""+    from pydantic._internal import _typing_extra++    def no_data_factory() -> list[int]:+        return []++    def data_factory(data: dict[str, Any]) -> int:+        return data['a'] * 2++    class Model(BaseModel):+        a: int+        b: list[int] = Field(default_factory=no_data_factory)+        c: int = Field(default_factory=data_factory)++    calls: list[object] = []+    orig = _typing_extra.signature_no_eval++    def counting(f, *args, **kwargs):+        calls.append(f)+        return orig(f, *args, **kwargs)++    monkeypatch.setattr(_typing_extra, 'signature_no_eval', counting)++    for i in range(100):+        m = Model.model_construct(a=i)+        assert m.b == []+        assert m.c == i * 2++    # Already computed when building the schema, so no inspection should happen at all:+    assert calls.count(no_data_factory) == 0+    assert calls.count(data_factory) == 0tests/test_private_attributes.pyindex cefe4c6d..37b336f1 100644@@ -1,4 +1,5 @@ import functools+import gc from enum import Enum from typing import ClassVar, Generic, TypeVar @@ -6,6 +7,7 @@ import pytest from pydantic_core import PydanticUndefined  from pydantic import BaseModel, ConfigDict, PrivateAttr, computed_field+from pydantic._internal import _fields, _typing_extra from pydantic.fields import ModelPrivateAttr  @@ -101,6 +103,81 @@ def test_private_attribute_factory_uses_validated_data():     assert m._foobaz == 'foobar'  +@pytest.fixture+def count_signature_calls(monkeypatch: pytest.MonkeyPatch) -> list[object]:+    calls: list[object] = []+    orig = _typing_extra.signature_no_eval++    def counting(f, *args, **kwargs):+        calls.append(f)+        return orig(f, *args, **kwargs)++    monkeypatch.setattr(_typing_extra, 'signature_no_eval', counting)+    return calls+++def test_private_attribute_factory_signature_inspected_once(count_signature_calls: list[object]) -> None:+    """https://github.com/pydantic/pydantic/issues/13376"""++    def no_data_factory() -> list[int]:+        return []++    def data_factory(data: dict[str, int]) -> int:+        return data['a'] * 2++    class Model(BaseModel):+        a: int+        _items: list[int] = PrivateAttr(default_factory=no_data_factory)+        _double: int = PrivateAttr(default_factory=data_factory)++    for i in range(100):+        m = Model(a=i)+        assert m._items == []+        assert m._double == i * 2++    assert count_signature_calls.count(no_data_factory) == 1+    assert count_signature_calls.count(data_factory) == 1+++def test_private_attribute_builtin_factory_signature_inspected_once(count_signature_calls: list[object]) -> None:+    class Model(BaseModel):+        _items: list = PrivateAttr(default_factory=list)++    for _ in range(100):+        assert Model()._items == []++    # `list` may already be cached by previous tests:+    assert count_signature_calls.count(list) <= 1+++def test_takes_validated_data_argument_cache_entry_released() -> None:+    def factory(data):+        return data++    key = id(factory)+    assert _fields.takes_validated_data_argument(factory) is True+    assert key in _fields._takes_validated_data_cache++    del factory+    gc.collect()+    assert key not in _fields._takes_validated_data_cache+++def test_private_attribute_non_weakrefable_factory() -> None:+    class Factory:+        __slots__ = ()++        def __call__(self, data: dict[str, int]) -> int:+            return data['a'] + 1++    class Model(BaseModel):+        a: int+        _b: int = PrivateAttr(default_factory=Factory())++    assert Model(a=1)._b == 2+    assert Model(a=2)._b == 3++ def test_private_attribute_factory_uses_other_private_attribute():     def factory(data: dict[str, str]) -> str:         return data['foo'] + data['bar'] + data['_foobaz'] 
Merged fix · PR #13390+67 −16
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,+        )