mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
987449a922
fix #279 * Support typing.Callable validation. * Add myself to HISTORY. * Prove it works with just Callable. * Document callable validator behavior. * Support differences in typing module between py36 and py37. * Remove callable filed from JSON schema with warning. * Update pydantic/schema.py Co-Authored-By: proofit404 <proofit404@gmail.com> * Update tests/test_callable.py Co-Authored-By: proofit404 <proofit404@gmail.com> * Update pydantic/validators.py Co-Authored-By: proofit404 <proofit404@gmail.com> * Update tests/test_callable.py Co-Authored-By: proofit404 <proofit404@gmail.com> * Move callables to the exotic types. * Move Callable type choice to the import time. * Move is_callable_type to the utils module. * Raise warning at skip except. * Update pydantic/schema.py Co-Authored-By: proofit404 <proofit404@gmail.com> * Update docs/index.rst Co-Authored-By: proofit404 <proofit404@gmail.com> * Update pydantic/schema.py Co-Authored-By: proofit404 <proofit404@gmail.com> * uprev * Update index.rst (#370) * Update history.rst * Make the example a little more concise. * Use callable import from the utils. * Remove blank line. * Remove duplication comments. * fix history
24 lines
559 B
Python
24 lines
559 B
Python
from typing import Callable
|
|
|
|
import pytest
|
|
|
|
from pydantic import BaseModel, ValidationError
|
|
|
|
|
|
@pytest.mark.parametrize('annotation', [Callable, Callable[[int], int]])
|
|
def test_callable(annotation):
|
|
class Model(BaseModel):
|
|
callback: annotation
|
|
|
|
m = Model(callback=lambda x: x)
|
|
assert callable(m.callback)
|
|
|
|
|
|
@pytest.mark.parametrize('annotation', [Callable, Callable[[int], int]])
|
|
def test_non_callable(annotation):
|
|
class Model(BaseModel):
|
|
callback: annotation
|
|
|
|
with pytest.raises(ValidationError):
|
|
Model(callback=1)
|