MCPcopy
hub / github.com/pydantic/pydantic / test_v1_get_validators

Function test_v1_get_validators

tests/test_deprecated.py:581–612  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

579
580
581def test_v1_get_validators():
582 class CustomDate(date):
583 @classmethod
584 def __get_validators__(cls):
585 yield cls.validate1
586 yield cls.validate2
587
588 @classmethod
589 def validate1(cls, v, i):
590 print(v)
591
592 if v.year < 2000:
593 raise ValueError('Invalid year')
594 return v
595
596 @classmethod
597 def validate2(cls, v, i):
598 return date.today().replace(month=1, day=1)
599
600 with pytest.warns(
601 PydanticDeprecatedSince20,
602 match='^`__get_validators__` is deprecated and will be removed, use `__get_pydantic_core_schema__` instead.',
603 ):
604
605 class Model(BaseModel):
606 x: CustomDate
607
608 with pytest.raises(ValidationError, match='Value error, Invalid year'):
609 Model(x=date(1999, 1, 1))
610
611 m = Model(x=date.today())
612 assert m.x.day == 1
613
614
615def test_v1_get_validators_invalid_validator():

Callers

nothing calls this directly

Calls 1

ModelClass · 0.70

Tested by

no test coverage detected