Backtestspydanticrun-20261009-110034#13867

Issue #13867 Merged fix PR #13871 Run 9 Oct 2026 · run-20261009-110034 pydantic/pydantic

FieldInfo.get_default is 5-6x slower on 2.13 because of a relative import in the function body

Better

Our patch was judged better than the merged fix.

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

The source changes are byte-for-byte identical. Both move `smart_deepcopy` into the existing module-level `from ._utils import ...` line in `_fields.py` and delete the in-body relative import from `resolve_default_value`. That removes the per-call `IMPORT_NAME`/`resolve_name` cost, which is the root cause the issue identifies. The import does not break a cycle, since `_utils` is already imported at module level, so both are equally correct and minimal in scope.

The only difference is testing. Patch A adds `test_get_default_does_not_import_at_call_time`, which checks with `dis.get_instructions` that `resolve_default_value` has no `IMPORT_NAME` instruction. This test fails on the unpatched code, where the in-body `from ._utils import smart_deepcopy` produces an `IMPORT_NAME`, and passes after the fix. It is a deterministic stand-in for the timing regression, which a test could not measure reliably. The test also checks behaviour through `get_default(call_default_factory=True)` for `FieldInfo` and `PrivateAttr`, with both a static default and a factory. The static case asserts the value is equal but not the same object, so it also confirms deep-copy semantics are preserved.

The bytecode check is somewhat tied to implementation details. One unverified risk is that the `PrivateAttr` cases assume `ModelPrivateAttr.get_default` accepts `call_default_factory`; the issue implies the 2.13 delegation path makes this likely. Even so, the test is a meaningful regression guard rather than a tautology. Patch B adds no tests. With correctness and scope equal, Patch A wins on tests.

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

5m 29s

Wall-clock time in the container

Full test suite

No new failures

12,871 passed · 526 skipped · 13,397 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

30vs 4 merged

Changed lines, ours vs merged

FieldInfo.get_default() costs about five to six times more on 2.13 than on 2.12.

On 2.13 the call is delegated to pydantic._internal._fields.resolve_default_value, whose body starts with a relative import that runs on every call:

def resolve_default_value(...):
    from ._utils import smart_deepcopy

On 2.12 the plain-default branch was return _utils.smart_deepcopy(self.default), with no import at call time. In 2.13 the import sits before the default_factory branch, so every call pays it.

_fields.py already has a module-level import from ._utils, so the in-body import is not breaking an import cycle, and moving the name up adds nothing to import time.

Measured with timeit, best of 5 runs of 200000 calls:

2.12.32.13.52.13.5, import moved up
FieldInfo(default=123)55 ns300 ns85 ns
FieldInfo(default_factory=list)35 ns271 ns60 ns

The cost is the import statement rather than module loading, since the module is already in sys.modules. With the relative form, resolve_name runs before the sys.modules lookup, so it is paid on every call even though the module is cached. A three-function package, identical except for the import form used to obtain the same callee:

formper call
from ._helper import work inside the body234 ns
from pkg._helper import work inside the body108 ns
module-level import32 ns

Moving the import up removes most of the gap but not all of it. What is left is the extra resolve_default_value call and the sentinel checks added to smart_deepcopy.

This is visible in model_construct(), where get_default runs once per field. For a five-field model with defaults: 1.18 us on 2.12.3 against 2.49 us on 2.13.5. Passing every field explicitly, so that no default is resolved, gives 1.12 us on both.

Version info

pydantic 2.13.5, CPython 3.13, macOS on arm64. The in-body import is still on main. Not measured on Linux or x86_64, so the absolute numbers will differ there, though the ratios should hold.

The two patches

Our patch+27 −3
pydantic/_internal/_fields.pyindex 29cde2df..51e7c9f0 100644@@ -25,7 +25,7 @@ from ._docs_extraction import extract_docstrings_from_cls from ._import_utils import import_cached_base_model, import_cached_field_info from ._namespace_utils import NsResolver from ._repr import Representation-from ._utils import can_be_positional, get_first_not_none+from ._utils import can_be_positional, get_first_not_none, smart_deepcopy  if TYPE_CHECKING:     from annotated_types import BaseMetadata@@ -866,8 +866,6 @@ def resolve_default_value(     call_default_factory: bool = False, ) -> Any:     """Resolve the default value using either a static default or a default_factory."""-    from ._utils import smart_deepcopy-     if default_factory is None:         return smart_deepcopy(default)     if call_default_factory:tests/test_fields.pyindex 5c8dc1ae..e1011205 100644@@ -1,4 +1,5 @@ import copy+import dis from typing import Annotated, Any, Final, Generic, TypeVar  import pytest@@ -473,3 +474,28 @@ def test_default_factory_without_validated_data_unsupported() -> None: ) def test_privateattr_repr(private_attr: ModelPrivateAttr, result: str) -> None:     assert repr(private_attr) == result+++@pytest.mark.parametrize(+    'field',+    [+        FieldInfo(default=[1]),+        FieldInfo(default_factory=list),+        PrivateAttr(default=[1]),+        PrivateAttr(default_factory=list),+    ],+)+def test_get_default_does_not_import_at_call_time(field: Any) -> None:+    """`get_default()` is on the hot path (e.g. `model_construct()`), so default resolution must not run an+    import statement on each call: even for cached modules, a relative import costs several times the call itself.+    """+    from pydantic._internal._fields import resolve_default_value++    assert not [i for i in dis.get_instructions(resolve_default_value) if i.opname == 'IMPORT_NAME']++    default = field.get_default(call_default_factory=True)+    if field.default_factory is None:+        assert default == field.default+        assert default is not field.default+    else:+        assert default == [] 
Merged fix · PR #13871+1 −3
pydantic/_internal/_fields.pyindex 29cde2dff..51e7c9f01 100644@@ -25,7 +25,7 @@ from ._docs_extraction import extract_docstrings_from_cls from ._import_utils import import_cached_base_model, import_cached_field_info from ._namespace_utils import NsResolver from ._repr import Representation-from ._utils import can_be_positional, get_first_not_none+from ._utils import can_be_positional, get_first_not_none, smart_deepcopy  if TYPE_CHECKING:     from annotated_types import BaseMetadata@@ -866,8 +866,6 @@ def resolve_default_value(     call_default_factory: bool = False, ) -> Any:     """Resolve the default value using either a static default or a default_factory."""-    from ._utils import smart_deepcopy-     if default_factory is None:         return smart_deepcopy(default)     if call_default_factory: