File: validate_call.py

package info (click to toggle)
pydantic 2.12.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,640 kB
  • sloc: python: 75,984; javascript: 181; makefile: 115; sh: 38
file content (27 lines) | stat: -rw-r--r-- 655 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from typing_extensions import assert_type

from pydantic import validate_call


@validate_call
def foo(a: int, *, c: str = 'x') -> str:
    return c * a


a = foo(1, c='a')
assert_type(a, str)

foo('', c=1)  # type: ignore[arg-type]  # pyright: ignore[reportArgumentType]

# Not possible to type check currently (see https://github.com/pydantic/pydantic/issues/9883):
foo.raw_function(1, c='a')  # type: ignore[attr-defined]  # pyright: ignore[reportFunctionMemberAccess]


# Should work even when not used as a bare decorator:
@validate_call(config={'arbitrary_types_allowed': True})
def bar(a: int) -> int:
    return a


b = bar(1)
assert_type(b, int)