Backtestspydanticrun-20260930-183947#13771

Issue #13771 Merged fix PR #13773 Run 30 Sep 2026 · run-20260930-183947 pydantic/pydantic

validate_call cannot call a function with a keyword parameter named self

Better

Our patch was judged better than the merged fix.

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

Both patches make the same core fix: `ValidateCallWrapper.__call__` becomes `def __call__(self, /, *args, **kwargs)`. That removes the root-cause name collision the issue describes, so they are equal on correctness for the reported bug.

The difference is in testing and craft. Patch A's tests cover:
- the exact keyword-only case from the issue (`def f(*, self: int)`);
- a positional-or-keyword `self` parameter, called both by keyword and positionally;
- both eager build and `defer_build=True`, which exercises the `__pydantic_complete__` path inside `__call__`;
- an async function;
- validation errors, including that the error `loc` is `('self',)`.

All of these fail before the change and pass after it. Patch B adds one minimal test: a positional-or-keyword `self`, called only as `func(self=1)`. It would also fail before the fix, but it covers far less. It never exercises the keyword-only form from the issue, never checks that coercion happens or that validation errors are raised, and its docstring is a leftover `"""<link>"""` placeholder.

Patch A also applies the same positional-only fix to the deprecated `ValidatedFunction.call` and `init_model_instance`, which have the identical collision for `validate_arguments`. This is adjacent to the issue's ask, so it earns no credit on its own. It carries little risk, though: the change is mechanical, consistent with the main fix, and has its own tests.

With correctness equal, Patch A's substantially stronger tests make it the better patch.

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 46s

Wall-clock time in the container

Full test suite

No new failures

12,501 passed · 509 skipped · 13,010 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

47vs 12 merged

Changed lines, ours vs merged

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

ValidateCallWrapper.__call__ is declared as def __call__(self, *args, **kwargs), so the instance is bound to the name self. A decorated function with a keyword parameter literally named self can then never be called with it: f(self=1) raises TypeError: ValidateCallWrapper.__call__() got multiple values for argument 'self' before validation runs. The wrapped function itself is fine with that name; it is only the wrapper's signature that collides. Same in 2.12.5, 2.13.5 and pydantic/_internal/_validate_call.py on main.

Making self positional-only in __call__ (def __call__(self, /, *args, **kwargs)) removes the collision without changing anything else. Can send the PR.

Example Code

import pydantic.version
from pydantic import validate_call


@validate_call
def f(*, self: int) -> int:
    return self


try:
    print("f(self=1) ->", f(self=1))
except TypeError as e:
    print("f(self=1) -> TypeError:", e)
print(pydantic.version.version_info())
f(self=1) -> TypeError: ValidateCallWrapper.__call__() got multiple values for argument 'self'

Python, Pydantic & OS Version

             pydantic version: 2.13.5
        pydantic-core version: 2.46.5
          pydantic-core build: profile=release pgo=false
               python version: 3.12.13 (main, May 10 2026, 19:20:41) [Clang 22.1.3 ]
                     platform: macOS-26.5.2-arm64-arm-64bit
             related packages: typing_extensions-4.16.0
                       commit: unknown

The two patches

Our patch+44 −3
pydantic/_internal/_validate_call.pyindex 1559d070..10f12cc8 100644@@ -130,7 +130,7 @@ class ValidateCallWrapper:          self.__pydantic_complete__ = True -    def __call__(self, *args: Any, **kwargs: Any) -> Any:+    def __call__(self, /, *args: Any, **kwargs: Any) -> Any:         if not self.__pydantic_complete__:             self._create_validators() pydantic/deprecated/decorator.pyindex b8dc224f..7c2e17f8 100644@@ -142,11 +142,11 @@ class ValidatedFunction:          self.create_model(fields, takes_args, takes_kwargs, config) -    def init_model_instance(self, *args: Any, **kwargs: Any) -> BaseModel:+    def init_model_instance(self, /, *args: Any, **kwargs: Any) -> BaseModel:         values = self.build_values(args, kwargs)         return self.model(**values) -    def call(self, *args: Any, **kwargs: Any) -> Any:+    def call(self, /, *args: Any, **kwargs: Any) -> Any:         m = self.init_model_instance(*args, **kwargs)         return self.execute(m) tests/test_deprecated_validate_arguments.pyindex 4fa70b37..db503920 100644@@ -389,3 +389,15 @@ def test_validate_by_name():     assert foo(a=10, d=1) == 11     assert foo(b=10, c=1) == 11     assert foo(a=10, c=1) == 11+++def test_keyword_parameter_named_self():+    @validate_arguments+    def foo(self: int, *, other: int = 0):+        return self + other++    assert foo(self='1', other='2') == 3+    assert foo('1') == 1+    assert isinstance(foo.validate(self='1'), BaseModel)+    with pytest.raises(ValidationError):+        foo(self='x')tests/test_validate_call.pyindex 3d86e451..5c40031f 100644@@ -1339,3 +1339,32 @@ def test_pickle_validate_call_with_basemodel() -> None:      assert unpickled == {1: _PickleTestModel(number=1.0)}     assert unpickled[1].number == 1.0+++@pytest.mark.parametrize('config', [None, {'defer_build': True}])+def test_keyword_parameter_named_self(config) -> None:+    @validate_call(config=config)+    def kw_only(*, self: int) -> int:+        return self++    @validate_call(config=config)+    def pos_or_kw(self: int, other: int = 0) -> int:+        return self + other++    assert kw_only(self='1') == 1+    assert pos_or_kw(self='2', other=3) == 5+    assert pos_or_kw('2') == 2++    with pytest.raises(ValidationError) as exc_info:+        kw_only(self='not_an_int')+    assert exc_info.value.errors(include_url=False)[0]['loc'] == ('self',)+++def test_keyword_parameter_named_self_async() -> None:+    @validate_call+    async def foo(*, self: int) -> int:+        return self++    assert asyncio.run(foo(self='1')) == 1+    with pytest.raises(ValidationError):+        asyncio.run(foo(self='x')) 
Merged fix · PR #13773+11 −1
pydantic/_internal/_validate_call.pyindex 1559d0705..10f12cc80 100644@@ -130,7 +130,7 @@ class ValidateCallWrapper:          self.__pydantic_complete__ = True -    def __call__(self, *args: Any, **kwargs: Any) -> Any:+    def __call__(self, /, *args: Any, **kwargs: Any) -> Any:         if not self.__pydantic_complete__:             self._create_validators() tests/test_validate_call.pyindex 3d86e4517..6d97b1e80 100644@@ -1339,3 +1339,13 @@ def test_pickle_validate_call_with_basemodel() -> None:      assert unpickled == {1: _PickleTestModel(number=1.0)}     assert unpickled[1].number == 1.0+++def test_validate_call_self_parameter() -> None:+    """https://github.com/pydantic/pydantic/issues/13771"""++    @validate_call+    def func(self: int):+        return self++    assert func(self=1) == 1