File: test_var_args.py

package info (click to toggle)
pydantic-core 2.41.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,828 kB
  • sloc: python: 35,564; javascript: 211; makefile: 128
file content (100 lines) | stat: -rw-r--r-- 2,618 bytes parent folder | download
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import pytest

from pydantic_core import ArgsKwargs, ValidationError
from pydantic_core import core_schema as cs

from ...conftest import PyAndJson


@pytest.mark.parametrize(
    ['input_value', 'expected'],
    (
        [ArgsKwargs(()), ((), {})],
        [ArgsKwargs((1, 2, 3)), ((1, 2, 3), {})],
        [{'args': ()}, ((), {})],
        [{'args': (1, 2, 3)}, ((1, 2, 3), {})],
        # Also validates against other sequence types, as long as it is
        # possible to validate it as a tuple:
        [{'args': [1, 2, 3]}, ((1, 2, 3), {})],
    ),
)
def test_var_args(py_and_json: PyAndJson, input_value, expected) -> None:
    """Test valid inputs against var-args parameters:

    ```python
    def func(*args: int):
        ...
    ```
    """
    v = py_and_json(
        cs.arguments_v3_schema(
            [
                cs.arguments_v3_parameter(name='args', schema=cs.int_schema(), mode='var_args'),
            ]
        )
    )

    assert v.validate_test(input_value) == expected


@pytest.mark.parametrize(
    ['input_value', 'err_loc'],
    (
        [ArgsKwargs(('not_an_int',)), (0,)],
        [
            ArgsKwargs(
                (
                    1,
                    'not_an_int',
                )
            ),
            (1,),
        ],
        [{'args': ['not_an_int']}, ('args', 0)],
        [{'args': [1, 'not_an_int']}, ('args', 1)],
    ),
)
def test_var_args_validation_error(py_and_json: PyAndJson, input_value, err_loc) -> None:
    """Test invalid inputs against var-args parameters:

    ```python
    def func(*args: int):
        ...

    func(1, 'not_an_int')
    ```
    """
    v = py_and_json(
        cs.arguments_v3_schema(
            [
                cs.arguments_v3_parameter(name='args', schema=cs.int_schema(), mode='var_args'),
            ]
        )
    )

    with pytest.raises(ValidationError) as exc_info:
        v.validate_test(input_value)

    error = exc_info.value.errors()[0]

    assert error['type'] == 'int_parsing'
    assert error['loc'] == err_loc


def test_var_args_invalid_tuple(py_and_json: PyAndJson) -> None:
    """Test invalid tuple-like input against var-args parameters in mapping validation mode."""
    v = py_and_json(
        cs.arguments_v3_schema(
            [
                cs.arguments_v3_parameter(name='args', schema=cs.int_schema(), mode='var_args'),
            ]
        )
    )

    with pytest.raises(ValidationError) as exc_info:
        v.validate_test({'args': 'not_a_tuple'})

    error = exc_info.value.errors()[0]

    assert error['type'] == 'tuple_type'
    assert error['loc'] == ('args',)