mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
3d287594e0
* Allow collections.abc.Callable to be used as type in python 3.9 * Add is_none_type as function to check none types by identity * Modify `typing.is_none_type` to work in python 3.6 and 3.7 * Add tests for none types in typing.py * Apply review comments on #2519 * Add different implementations depending on python version * Add tests for is_none_type * Add change entry * Fix field info repr * Remove unneeded try/except for python < 3.8 * Add comment explaining alternative is_none_type implementation * fix: typo Co-authored-by: PrettyWood <em.jolibois@gmail.com>
31 lines
802 B
Python
31 lines
802 B
Python
import sys
|
|
from typing import Callable
|
|
|
|
import pytest
|
|
|
|
from pydantic import BaseModel, ValidationError
|
|
|
|
collection_callable_types = [Callable, Callable[[int], int]]
|
|
if sys.version_info >= (3, 9):
|
|
from collections.abc import Callable as CollectionsCallable
|
|
|
|
collection_callable_types += [CollectionsCallable, CollectionsCallable[[int], int]]
|
|
|
|
|
|
@pytest.mark.parametrize('annotation', collection_callable_types)
|
|
def test_callable(annotation):
|
|
class Model(BaseModel):
|
|
callback: annotation
|
|
|
|
m = Model(callback=lambda x: x)
|
|
assert callable(m.callback)
|
|
|
|
|
|
@pytest.mark.parametrize('annotation', collection_callable_types)
|
|
def test_non_callable(annotation):
|
|
class Model(BaseModel):
|
|
callback: annotation
|
|
|
|
with pytest.raises(ValidationError):
|
|
Model(callback=1)
|