| 153 | |
| 154 | |
| 155 | def test_args() -> None: |
| 156 | @validate_call |
| 157 | def foo(a: int, b: int): |
| 158 | return f'{a}, {b}' |
| 159 | |
| 160 | assert foo(1, 2) == '1, 2' |
| 161 | assert foo(*[1, 2]) == '1, 2' |
| 162 | assert foo(*(1, 2)) == '1, 2' |
| 163 | assert foo(*[1], 2) == '1, 2' |
| 164 | assert foo(a=1, b=2) == '1, 2' |
| 165 | assert foo(1, b=2) == '1, 2' |
| 166 | assert foo(b=2, a=1) == '1, 2' |
| 167 | |
| 168 | with pytest.raises(ValidationError) as exc_info: |
| 169 | foo() |
| 170 | # insert_assert(exc_info.value.errors(include_url=False)) |
| 171 | assert exc_info.value.errors(include_url=False) == [ |
| 172 | {'type': 'missing_argument', 'loc': ('a',), 'msg': 'Missing required argument', 'input': ArgsKwargs(())}, |
| 173 | {'type': 'missing_argument', 'loc': ('b',), 'msg': 'Missing required argument', 'input': ArgsKwargs(())}, |
| 174 | ] |
| 175 | |
| 176 | with pytest.raises(ValidationError) as exc_info: |
| 177 | foo(1, 'x') |
| 178 | # insert_assert(exc_info.value.errors(include_url=False)) |
| 179 | assert exc_info.value.errors(include_url=False) == [ |
| 180 | { |
| 181 | 'type': 'int_parsing', |
| 182 | 'loc': (1,), |
| 183 | 'msg': 'Input should be a valid integer, unable to parse string as an integer', |
| 184 | 'input': 'x', |
| 185 | } |
| 186 | ] |
| 187 | |
| 188 | with pytest.raises(ValidationError, match=r'2\s+Unexpected positional argument'): |
| 189 | foo(1, 2, 3) |
| 190 | |
| 191 | with pytest.raises(ValidationError, match=r'apple\s+Unexpected keyword argument'): |
| 192 | foo(1, 2, apple=3) |
| 193 | |
| 194 | with pytest.raises(ValidationError, match=r'a\s+Got multiple values for argument'): |
| 195 | foo(1, 2, a=3) |
| 196 | |
| 197 | with pytest.raises(ValidationError) as exc_info: |
| 198 | foo(1, 2, a=3, b=4) |
| 199 | # insert_assert(exc_info.value.errors(include_url=False)) |
| 200 | assert exc_info.value.errors(include_url=False) == [ |
| 201 | {'type': 'multiple_argument_values', 'loc': ('a',), 'msg': 'Got multiple values for argument', 'input': 3}, |
| 202 | {'type': 'multiple_argument_values', 'loc': ('b',), 'msg': 'Got multiple values for argument', 'input': 4}, |
| 203 | ] |
| 204 | |
| 205 | |
| 206 | def test_optional(): |