Skip to content

Commit

Permalink
Add warning
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Aug 23, 2023
1 parent 4226cde commit 2cb4e9c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
4 changes: 4 additions & 0 deletions pydantic/_internal/_generate_schema.py
Expand Up @@ -593,6 +593,10 @@ def _generate_schema_from_property(self, obj: Any, source: Any) -> core_schema.C
validators = getattr(obj, '__get_validators__', None)
if validators is None:
return None
warn(
'`__get_validators__` is deprecated and will be removed, use `__get_pydantic_core_schema__` instead.',
PydanticDeprecatedSince20,
)
schema = core_schema.chain_schema([core_schema.general_plain_validator_function(v) for v in validators()])
else:
if len(inspect.signature(get_schema).parameters) == 1:
Expand Down
18 changes: 14 additions & 4 deletions tests/test_deprecated.py
Expand Up @@ -542,8 +542,13 @@ def validate1(cls, v, i):
def validate2(cls, v, i):
return date.today().replace(month=1, day=1)

class Model(BaseModel):
x: CustomDate
with pytest.warns(
PydanticDeprecatedSince20,
match='^`__get_validators__` is deprecated and will be removed, use `__get_pydantic_core_schema__` instead.',
):

class Model(BaseModel):
x: CustomDate

with pytest.raises(ValidationError, match='Value error, Invalid year'):
Model(x=date(1999, 1, 1))
Expand All @@ -562,8 +567,13 @@ def __get_validators__(cls):
def has_wrong_arguments(cls):
pass

class InvalidValidatorModel(BaseModel):
x: InvalidValidator
with pytest.warns(
PydanticDeprecatedSince20,
match='^`__get_validators__` is deprecated and will be removed, use `__get_pydantic_core_schema__` instead.',
):

class InvalidValidatorModel(BaseModel):
x: InvalidValidator

with pytest.raises(TypeError, match='takes 1 positional argument but 3 were given'):
InvalidValidatorModel(x=1)
Expand Down
7 changes: 0 additions & 7 deletions tests/test_edge_cases.py
Expand Up @@ -1504,13 +1504,6 @@ def __init__(self, t1: T1, t2: T2):
self.t1 = t1
self.t2 = t2

@classmethod
def __get_validators__(cls):
def validator(v):
return v

yield validator


@pytest.mark.parametrize(
'type_,expected',
Expand Down

0 comments on commit 2cb4e9c

Please sign in to comment.