Skip to content

Commit

Permalink
Fix error message for staticmethod/classmethod order with validate_ca…
Browse files Browse the repository at this point in the history
…ll (#6686)

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
  • Loading branch information
dmontagu and samuelcolvin committed Jul 25, 2023
1 parent 81f37a3 commit 2db6248
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pydantic/validate_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def validate_call(
"""

def validate(function: AnyCallableT) -> AnyCallableT:
if isinstance(function, (classmethod, staticmethod)):
name = type(function).__name__
raise TypeError(f'The `@{name}` decorator should be applied after `@validate_call` (put `@{name}` on top)')
return _validate_call.ValidateCallWrapper(function, config, validate_return) # type: ignore

if __func:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_validate_call.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import inspect
import re
import sys
from datetime import datetime, timezone
from functools import partial
Expand Down Expand Up @@ -687,3 +688,18 @@ def test(self, x: int):

bar = Bar()
assert bar.test('1') == (bar, 1)


@pytest.mark.parametrize('decorator', [staticmethod, classmethod])
def test_classmethod_order_error(decorator):
name = decorator.__name__
with pytest.raises(
TypeError,
match=re.escape(f'The `@{name}` decorator should be applied after `@validate_call` (put `@{name}` on top)'),
):

class A:
@validate_call
@decorator
def method(self, x: int):
pass

0 comments on commit 2db6248

Please sign in to comment.