Files
pydantic/tests/test_callable.py
T
Artem Malyshev 987449a922 Support typing.Callable validation. (#364)
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
2019-01-29 15:42:00 +00:00

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)