Skip to content

Add ValidateFrom annotation helper #11942

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Add ValidateFrom annotation helper #11942

wants to merge 1 commit into from

Conversation

Viicos
Copy link
Member

@Viicos Viicos commented Jun 2, 2025

Change Summary

Fixes #11682.

Currently, the instantiation_hook is required. We could also make it optional and by default populate the arbitrary class by e.g. calling __new__(). However, by doing so, we need to make some assumptions on the from_type. It can currently be anything (supported by Pydantic: models, dataclasses, typeddicts, etc), but we will have to constrain it to some specific types (perhaps only supporting Pydantic models?) as we will most likely have to assume __dict__ is present to fetch the instance attributes and pass them to __new__().

Related issue number

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

@github-actions github-actions bot added the relnotes-fix Used for bugfixes. label Jun 2, 2025
Copy link
Contributor

hyperlint-ai bot commented Jun 2, 2025

PR Change Summary

Added the ValidateFrom annotation helper to enhance type validation in Pydantic.

  • Introduced the ValidateFrom annotation helper for custom type validation.
  • Provided an example demonstrating the usage of ValidateFrom with a custom class.
  • Updated documentation to reflect the new validation capabilities.

Modified Files

  • docs/concepts/validators.md

How can I customize these reviews?

Check out the Hyperlint AI Reviewer docs for more information on how to customize the review.

If you just want to ignore it on this PR, you can add the hyperlint-ignore label to the PR. Future changes won't trigger a Hyperlint review.

Note specifically for link checks, we only check the first 30 links in a file and we cache the results for several hours (for instance, if you just added a page, you might experience this). Our recommendation is to add hyperlint-ignore to the PR to ignore the link check for this PR.

Copy link
Contributor

github-actions bot commented Jun 2, 2025

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  pydantic
  functional_validators.py
Project Total  

This report was generated by python-coverage-comment-action

Copy link

codspeed-hq bot commented Jun 2, 2025

CodSpeed Performance Report

Merging #11942 will not alter performance

Comparing validate-from (ffab3d8) with main (88c75cd)

Summary

✅ 46 untouched benchmarks

@Viicos
Copy link
Member Author

Viicos commented Jun 5, 2025

@timkpaine @ptomecek this is an implementation of an helper that can validate from a type that is natively supported by Pydantic (your use case was using Pydantic models, but TypedDicts/dataclasses etc can also be used). Let me know if this is suitable for you (see my OP for more details on decisions that need to be made before merging).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes-fix Used for bugfixes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2.11 Regression - Nested annotation references lead to model never being fully buildable
1 participant