MCPcopy
hub / github.com/pydantic/pydantic / test_predicates

Function test_predicates

tests/test_pipeline.py:292–306  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

290
291
292def test_predicates() -> None:
293 ta_int = TypeAdapter[int](Annotated[int, validate_as(int).predicate(lambda x: x % 2 == 0)])
294 assert ta_int.validate_python(2) == 2
295 with pytest.raises(ValidationError):
296 ta_int.validate_python(1)
297
298 ta_str = TypeAdapter[int](Annotated[str, validate_as(str).predicate(lambda x: x != 'potato')])
299 assert ta_str.validate_python('tomato') == 'tomato'
300 with pytest.raises(ValidationError):
301 ta_str.validate_python('potato')
302
303 ta_str_to_int = TypeAdapter[int](
304 Annotated[str, validate_as(str).transform(lambda x: int(float(x))).predicate(float)]
305 )
306 assert ta_str_to_int.validate_python('1.5') == 1
307
308
309@pytest.mark.parametrize(

Callers

nothing calls this directly

Calls 3

predicateMethod · 0.80
validate_pythonMethod · 0.45
transformMethod · 0.45

Tested by

no test coverage detected