Backtestspydanticrun-20260923-170312#13376

Issue #13376 Merged fix PR # Run 23 Sep 2026 · run-20260923-170312 pydantic/pydantic

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

Worse

The merged fix was judged better than our patch.

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

Both patches eliminate the per-instantiation recomputation, but they differ meaningfully on the highest-weighted criterion, correctness/root-cause. Patch A precomputes the flag once and stores it on the shared `ModelPrivateAttr` instance (mirroring how regular `FieldInfo` fields bake this in), fixing BOTH per-instance call sites the issue names: the previously-uncached `default_factory_takes_validated_data` property now memoizes into a new slot, and `resolve_default_value` no longer calls `takes_validated_data_argument` at all (it receives the flag as a parameter). Because the cache lives on the class-scoped attribute, it is freed when the class is, with no global retention. Patch B just decorates `takes_validated_data_argument` with `@cache`. That is the issue's 'simplest' suggestion and it does stop the repeated `inspect.signature` work, but it installs an unbounded, process-lifetime module-level cache keyed on arbitrary user-supplied callables — holding strong references to every distinct factory (and its enclosing closure) forever. For a library that routinely builds models dynamically (`create_model`, generics, per-call lambda factories) this reintroduces exactly the class of unbounded RSS growth the issue is about; it trades one leak for a milder one, i.e. it masks rather than cleanly resolves. On tests, Patch B is stronger: it adds a focused regression test that asserts signature inspection is not re-run per instantiation (fails before, passes after) — directly targeting the bug. Patch A's test changes are largely adaptive (updating `resolve_default_value`'s new keyword-only signature) plus an unrelated `__repr_args__` repr test; it notably lacks a test proving the recomputation is gone. On scope, Patch B is far tighter (one line), whereas Patch A carries drive-by churn (a new `__repr_args__` method and a docstring rewording unrelated to the perf fix) and a somewhat invasive internal signature change. Net: B wins tests and scope, but A wins the top-priority correctness criterion decisively by fixing the root cause at both sites without introducing a new unbounded-cache retention hazard on a bug that is fundamentally about long-running memory growth. That edge on the highest-weighted axis outweighs B's advantages, so A is the better fix — though it is close and A's scope creep is a real mark against it.

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

8m 19s6 turns

Wall-clock time in the container

Full test suite

No new failures

6,412 passed · 367 skipped · 6,779 total

Contamination audit

Unknown

See the audit notes for this run

Patch size

26vs 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+26 −0
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..acc70e68 100644@@ -88,6 +88,31 @@ def test_private_attribute_factory():     assert m.__dict__ == {}  +def test_private_attribute_factory_signature_inspection_cached(monkeypatch):+    from pydantic._internal import _typing_extra++    class Model(BaseModel):+        _items: list = PrivateAttr(default_factory=list)++    # Warm any cache with a first instantiation:+    Model()++    call_count = 0+    original = _typing_extra.signature_no_eval++    def counting_signature_no_eval(*args, **kwargs):+        nonlocal call_count+        call_count += 1+        return original(*args, **kwargs)++    monkeypatch.setattr(_typing_extra, 'signature_no_eval', counting_signature_no_eval)++    for _ in range(50):+        Model()++    assert call_count == 0++ def test_private_attribute_factory_uses_validated_data():     def factory(data: dict[str, str]) -> str:         return data['foo'] + data['bar'] 
Merged fix · PR #+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,+        ) 

What the agent reported

The fix is verified and correct. The reviewer flagged that @cache narrows accepted factories to hashable ones — but this is exactly the fix the report proposed as simplest, and unhashable callable factories are a niche case. I'll keep the change minimal per the report's own suggestion and note the tradeoff.

Summary

What was wrong: For a private attribute with a 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() on the factory. It was uncached and ran twice per instantiation (via the ModelPrivateAttr.default_factory_takes_validated_data property in init_private_attributes, and again via get_default → resolve_default_value). For factories like list/Lock whose signature is inferred through CPython's _signature_fromstr, each call leaves cycle-only garbage, causing RSS to climb in long-running processes. Regular Field(default_factory=...) was unaffected because it bakes the flag into the core schema once at build time.

What I changed (both left as uncommitted working-tree edits):

  • pydantic/_internal/_fields.py: added the @cache decorator to takes_validated_data_argument. from functools import cache was already imported, so this is a one-line change. After the first call per factory, both per-instance call sites become cache hits — the expensive signature inspection no longer runs per instantiation. This is the exact "simplest fix" the report proposed.
  • tests/test_private_attributes.py: added test_private_attribute_factory_signature_inspection_cached, which warms a PrivateAttr(default_factory=list) model once, patches _typing_extra.signature_no_eval with a counter, instantiates 50 times, and asserts it is never called again (0 calls).

Test evidence:

  • Regression test failed before the fix: assert 100 == 0 (signature inspected twice per instantiation × 50).
  • After the fix, the full tests/test_private_attributes.py suite passes: 36 passed, including the new test. No previously passing tests regressed.

Reviewer note (not blocking): @cache now requires factories to be hashable and retains them for the process lifetime. All common factories (list, dict, set, functions, lambdas, classes, partial) are hashable, and this matches the report's explicitly suggested approach and the reporter's own upstream mitigation, so I kept it minimal rather than switching to the more invasive precompute-at-class-construction alternative.

Requirements disposition

  1. Memoize takes_validated_data_argument so both per-instance calls become cache hits (primary suggested fix) — ✅ Implemented (@cache) with a regression test that fails before and passes after.
  2. Alternative: precompute the flag once at class-construction time (incl. the uncached property at fields.py) — ⏭️ Skipped as the explicitly-offered alternative to the primary fix. Evidence it's unnecessary: the @cache fix drives the underlying signature_no_eval calls to 0 across repeated instantiations (regression test asserts this), fully resolving the reported CPU/GC churn without touching the property or __slots__. The report presents these as either/or; the simpler one is sufficient.
  3. Fix must not affect the regular Field(default_factory=...) path — ✅ Confirmed: that path calls the same now-cached function once at schema-build time (_generate_schema.py:2654); caching only makes it a hit if reused. All 36 private-attribute tests pass.