Backtestspydanticrun-20261013-104308#13771
validate_call cannot call a function with a keyword parameter named self
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: `self` becomes positional-only in `ValidateCallWrapper.__call__`, which removes the root-cause name collision the issue describes. On correctness of the main path they are identical. Patch A also applies the same fix to `ValidatedFunction.init_model_instance` and `ValidatedFunction.call` in the deprecated `validate_arguments` decorator. That decorator has the same collision, and the change is low-risk and consistent. It counts as neutral-to-slightly-positive adjacent hardening, not churn. The deciding factor is tests. Patch A's tests fail before the fix and pass after it. They cover `validate_return` on and off, `defer_build` on and off (which exercises the lazy `_create_validators` path inside `__call__`), and coercion. They also check the exact error `loc` of `('self',)` on validation failure, confirm that `**kwargs` with a `self` key flows through, and cover async functions. Patch B's test is a single happy-path check, `func(self=1) == 1`. It does reproduce the bug, but it covers no coercion, errors, deferred build, or return validation. Neither patch adds risk. Patch A's broader, more meaningful test coverage makes it the better fix.
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
3m 51s
Wall-clock time in the container
Full test suite
No new failures
140 passed · 0 skipped · 140 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
55vs 12 merged
Changed lines, ours vs merged
The issue
View on GitHubInitial 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: unknownThe two patches
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..c996e618 100644@@ -389,3 +389,14 @@ 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_argument_named_self():+ @validate_arguments+ def foo(*, self: int):+ return self++ assert foo(self='1') == 1+ assert foo.validate(self='2').self == 2+ with pytest.raises(ValidationError):+ foo(self='x')tests/test_validate_call.pyindex 3d86e451..7ef9181e 100644@@ -17,6 +17,7 @@ from pydantic import ( AliasChoices, BaseModel, BeforeValidator,+ ConfigDict, Field, PydanticInvalidForJsonSchema, PydanticUserError,@@ -259,6 +260,43 @@ def test_kwargs(): ] +@pytest.mark.parametrize('validate_return', [False, True])+@pytest.mark.parametrize('defer_build', [False, True])+def test_keyword_argument_named_self(validate_return: bool, defer_build: bool) -> None:+ @validate_call(validate_return=validate_return, config=ConfigDict(defer_build=defer_build))+ def foo(*, self: int) -> int:+ return self++ assert foo(self=1) == 1+ assert foo(self='2') == 2+ with pytest.raises(ValidationError) as exc_info:+ foo(self='x')+ assert exc_info.value.errors(include_url=False) == [+ {+ 'type': 'int_parsing',+ 'loc': ('self',),+ 'msg': 'Input should be a valid integer, unable to parse string as an integer',+ 'input': 'x',+ }+ ]+++def test_var_kwargs_with_self_key() -> None:+ @validate_call+ def foo(**kwargs: int) -> dict[str, int]:+ return kwargs++ assert foo(self='1', other=2) == {'self': 1, 'other': 2}+++def test_async_keyword_argument_named_self() -> None:+ @validate_call(validate_return=True)+ async def foo(*, self: int) -> int:+ return self++ assert asyncio.run(foo(self='1')) == 1++ def test_untyped(): @validate_call def foo(a, b, c='x', *, d='y'):
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