Skip to content

Support __get_validators__#7197

Merged
hramezani merged 2 commits intomainfrom
get_validators
Aug 23, 2023
Merged

Support __get_validators__#7197
hramezani merged 2 commits intomainfrom
get_validators

Conversation

@hramezani
Copy link
Copy Markdown
Member

Closes #6899

validators = getattr(obj, '__get_validators__', None)
if validators is None:
return None
schema = core_schema.chain_schema([core_schema.general_plain_validator_function(v) for v in validators()])
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know whether general_plain_validator_function is the right choice

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Aug 21, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2cb4e9c
Status: ✅  Deploy successful!
Preview URL: https://1ae086d5.pydantic-docs2.pages.dev
Branch Preview URL: https://get-validators.pydantic-docs2.pages.dev

View logs

Copy link
Copy Markdown
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a deprecation warning, otherwise LGTM.

Copy link
Copy Markdown
Member

@adriangb adriangb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we care how this interacts with Annotated, including with other classes that implement __get_pydantic_core_schema__? Might be worth testing

@hramezani
Copy link
Copy Markdown
Member Author

Do we care how this interacts with Annotated, including with other classes that implement __get_pydantic_core_schema__? Might be worth testing

@adriangb, I think we already have a test for this. Is that enough?

def test_v1_v2_custom_type_compatibility() -> None:
"""Create a custom type that works with V1 and V2"""
class MyType:
@classmethod
def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHandler) -> CoreSchema:
return core_schema.int_schema()
@classmethod
def __get_pydantic_json_schema__(
cls, core_schema: CoreSchema, handler: GetJsonSchemaHandler
) -> JsonSchemaValue:
return {'anyOf': [{'type': 'string'}, {'type': 'number'}]}
@classmethod
def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None:
raise NotImplementedError # not actually called, we just want to make sure the method can exist
@classmethod
def __get_validators__(cls) -> Iterable[Any]:
raise NotImplementedError # not actually called, we just want to make sure the method can exist
yield
ta = TypeAdapter(MyType)
assert ta.validate_python('123') == 123
assert ta.json_schema() == {'anyOf': [{'type': 'string'}, {'type': 'number'}]}

@adriangb
Copy link
Copy Markdown
Member

But that doesn't use this new / old __get_valdiators__ right?

@hramezani hramezani merged commit 8edc9ba into main Aug 23, 2023
@hramezani hramezani deleted the get_validators branch August 23, 2023 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support __get_validators__

3 participants