mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
9e1f2a6f7c
* feat: add strict to Field * skip if not set * Update pydantic/fields.py * Move model into fixture
37 lines
843 B
Python
37 lines
843 B
Python
import sys
|
|
from typing import Any, Type
|
|
|
|
if sys.version_info < (3, 9):
|
|
from typing_extensions import Annotated
|
|
else:
|
|
from typing import Annotated
|
|
|
|
import pytest
|
|
|
|
from pydantic import BaseModel, Field, ValidationError
|
|
|
|
|
|
@pytest.fixture(scope='session', name='ModelWithStrictField')
|
|
def model_with_strict_field():
|
|
class ModelWithStrictField(BaseModel):
|
|
a: Annotated[int, Field(strict=True)]
|
|
|
|
return ModelWithStrictField
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
'value',
|
|
[
|
|
'1',
|
|
True,
|
|
1.0,
|
|
],
|
|
)
|
|
def test_parse_strict_mode_on_field_invalid(value: Any, ModelWithStrictField: Type[BaseModel]) -> None:
|
|
with pytest.raises(ValidationError):
|
|
ModelWithStrictField(a=value)
|
|
|
|
|
|
def test_parse_strict_mode_on_field_valid(ModelWithStrictField: Type[BaseModel]) -> None:
|
|
ModelWithStrictField(a=1)
|