Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support __get_validators__ #7197

Merged
merged 2 commits into from Aug 23, 2023
Merged

Support __get_validators__ #7197

merged 2 commits into from Aug 23, 2023

Conversation

hramezani
Copy link
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
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-pages
Copy link

cloudflare-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
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
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
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
Member

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

@hramezani hramezani merged commit 8edc9ba into main Aug 23, 2023
50 checks passed
@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